Operating System - Linux
1753779 Members
7645 Online
108799 Solutions
New Discussion юеВ

how to rm files in all subdirs,

 
SOLVED
Go to solution
JianW
Occasional Advisor

how to rm files in all subdirs,

Hi,
Is it possible to do with rm -r, I need to delete all .zip files in number of subdir.
Only files need to be deleted, subdirs should stay as is.

Will be good if no need for any looping.

Tx to all
D
6 REPLIES 6
Steven Schweda
Honored Contributor
Solution

Re: how to rm files in all subdirs,

> [...] I need to delete all .zip files [...]

> Is it possible to do with rm -r, [...]

I don't see how. The usual tool for a task
like this would be "find".

man find

Look for "-exec".

Perhaps something like:

find dir -name '*.zip' -exec ls {} \;

If you like the list, then change "ls" to
"rm".
James R. Ferguson
Acclaimed Contributor

Re: how to rm files in all subdirs,

Hi:

As Steven noted, the manpages are your friend. That said, you could do:

# find /path -xdev -type f -name "*.zip -exec rm {} +

The '-xdev' option prevents 'find()' from crossing mountpoints. The '-type f' limits things to files (and not directories). The '-name "*.zip"' looks for the files with the '.zip' extension. You must quote the name so that the shell doesn't expand the "*" wildcard character. The quotes allow the expression to be passed intact to 'find()'.

The '-exec rm {} +' says to bundle up multiple arguments and pass them to 'rm'. This is very efficient. If you use the '-exec rm {} \;' form then a 'rm' process is spawned for *every* file that needs to be removed. This can really warm your processor :-)

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: how to rm files in all subdirs,

> # find /path -xdev -type f -name "*.zip -exec rm {} +

Missed a quotation mark (after "*.zip)?

> [...] The '-type f' limits things to files
> (and not directories). [...]

Although, if you're foolish enough to have a
directory named "*.zip", it might be useful
to get a nice error message back from "rm".
JianW
Occasional Advisor

Re: how to rm files in all subdirs,

Thanks all, Steven, James and one more time Steven.

It's simple now, I"m new in unix, so it bit hard to get used all this ksh, for me it's look to compact ant too powerful. Already rm'ed some good stuff0).

I also was looking how to extract just file name from the path, something like perl's split command.

Tx again to all.
J
James R. Ferguson
Acclaimed Contributor

Re: how to rm files in all subdirs,

Hi (again):

> I also was looking how to extract just file name from the path, something like perl's split command.

You have that as 'basename' [ and its complementary 'dirname' ]:

# FILE=/usr/bin/perl
# echo $(basename $FILE)
perl

...or if you prefer, use the shell's parameter substitution:

# FILE=/usr/bin/perl
# echo ${FILE##*/}
perl

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: how to rm files in all subdirs,

> [...] Already rm'ed some good stuff [...]

I started with "ls" instead of "rm" for a
reason. Trust no one, especially yourself,
I always say.