Page 1 of 1

Script Request

Posted: Tue 02 Jul 2013, 14:07
by tlchost
This requirement is way over my head....hopefully someone can give me a hand.

I need a script where I can specify a particular path, i.e, nytimes/fiction and have the script delete all file extensions of a specified type

Something like

scriptname /nytimes/fiction mobi
or if I wanted to deleted the epup files
scriptname /nytimes/ epup

The variables are the directory path and the file extension

Thanks in advance for any help.

Thom

Posted: Tue 02 Jul 2013, 14:18
by akash_rawal

Code: Select all

#!/bin/sh

dirname="$1"
extension="$2"

find "$dirname" -name "*.$extension" -print |
while read file; do
    rm "$file"
done
Does this work?

Posted: Tue 02 Jul 2013, 14:50
by tlchost
Ah....my error

The directory structure is
nytimes/fiction/variable authorname/extension

it would need to look in all many authornames and perform the rm
akash_rawal wrote:

Code: Select all

#!/bin/sh

dirname="$1"
extension="$2"

find "$dirname" -name "*.$extension" -print |
while read file; do
    rm "$file"
done
Does this work?

Posted: Tue 02 Jul 2013, 17:08
by Ibidem
tlchost wrote:Ah....my error

The directory structure is
nytimes/fiction/variable authorname/extension

it would need to look in all many authornames and perform the rm
akash_rawal wrote:

Code: Select all

#!/bin/sh

dirname="$1"
extension="$2"

find "$dirname" -name "*.$extension" -print |
while read file; do
    rm "$file"
done
Does this work?
That's what find is for. It will look in all subdirectories, recursively.

I presume that the files are (for example) *.mobi, not <author>/mobi (which a pedantic reading of your post would suggest)?

Posted: Tue 02 Jul 2013, 23:27
by tlchost
Ibidem wrote: I presume that the files are (for example) *.mobi, not <author>/mobi (which a pedantic reading of your post would suggest)?
Pedantic wins:

Examples:

/nytimes/fiction/authorname1/*.mobi
/nytimes/fiction/authorname2/*.mobi

So one might think of it as
/nytimes/fiction/authorname/*.extension
/nytimes/nonfiction/authorname/*.extension
where fiction/nonfiction are variables
authorname is a variable
extension is a variable

Thanks,

Thom

Posted: Wed 03 Jul 2013, 15:34
by akash_rawal
tlchost wrote:
Ibidem wrote: I presume that the files are (for example) *.mobi, not <author>/mobi (which a pedantic reading of your post would suggest)?
Pedantic wins:

Examples:

/nytimes/fiction/authorname1/*.mobi
/nytimes/fiction/authorname2/*.mobi

So one might think of it as
/nytimes/fiction/authorname/*.extension
/nytimes/nonfiction/authorname/*.extension
where fiction/nonfiction are variables
authorname is a variable
extension is a variable

Thanks,

Thom
Means you want to delete files at depth 2?

In your example you want to delete files named as "/nytimes/fiction/*/*.extension" and not "/nytimes/fiction/*.extension" or "/nytimes/fiction/*/*/*.extension"?

Then this might work:

Code: Select all

#!/bin/sh

dirname="$1"
extension="$2"

find "$dirname" -mindepth 2 -maxdepth 2 -name "*.$extension" -print |
while read file; do
    rm "$file"
done 

Posted: Wed 03 Jul 2013, 15:35
by jamesbond
Check whether listing is correct:

Code: Select all

find /nytimes/fiction -path '*/authorname/*.mobi'
If correct,

Code: Select all

find /nytimes/fiction -path '*/authorname/*.mobi' -delete
will finish it.

Thanks

Posted: Thu 04 Jul 2013, 01:39
by tlchost
It seems that the directories created with the ebooks are all differnent in structure..so there may not be a universal script.

Thanks for all the responses.

I ended up with a kludgy solution..

looked at the directory nytimes, noted the subdirectories and based on that:

rm /mnt/home/nytimes/Books/Fiction/*/*.mobi
and
rm /mnt/home/nytimes/Books/Non-fiction/*/*.mobi

Since all the sub-directories can have different names...it's beyond me on how to do it, unless I did
rm /mnt/home/nytimes/*/*/*.mobi