How to delete a lot of files from a directory.

If you have more than can be passed directly to a command.

v4u-3500f-gmp03 135 # rm test/test_dir/\*/\*                       
/usr/bin/ksh: /usr/bin/rm: arg list too long
v4u-3500f-gmp03 136 # echo test/test_dir/\*/\* | xargs rm
v4u-3500f-gmp03 137 # 

There are lots of other ways to skin this cat.

Tags:

Comments:

What's wrong with rm -rf test/test_dir ?

Posted by Andrew on October 24, 2006 at 11:38 AM BST #

I only whated to delete the files. The directories in there were spared.

Posted by Chris Gerhard on October 24, 2006 at 12:03 PM BST #

Also in the running find <dir> -type f -print | xargs rm And for people with spaces (and other rubbish) in filenames, the GNU versions (and maybe others) try gfind <dir> -type f -print0 | gxargs -0 rm Have Fun, Peter

Posted by Peter on October 24, 2006 at 05:37 PM BST #

While I did not want to descend into directories the find was not what I wanted.

With the solaris find there is a better way if you did want to though:

find <dir> -type -f exec rm {} +

It aviods all the issues with xargs and still performs the minimum mumber of execs

Posted by Chris Gerhard on October 25, 2006 at 12:04 AM BST #

Using recent gfind (or maybe sfind now) you can do the same as with solaris find, and use mindepth/maxdepth to do exactly what you wanted to originally.

Posted by Marc on October 25, 2006 at 03:49 AM BST #

Or something like:
 find <dir> -type f -exec rm '{}' \\; -type d -prune

Which won't run up against shell max arglist limit and is okay with spaces in filenames (even though they are evil), but could fork/exec a lot of rm's.

Like you said, lots of ways of skinning a cat.

Posted by Stuart Remphrey on November 02, 2006 at 09:34 PM GMT #

Ohh, and whenever using "find...rm..." test with "find...echo rm..." first!

Posted by Stuart Remphrey on November 02, 2006 at 09:36 PM GMT #

Post a Comment:
Comments are closed for this entry.
About

This is the old blog of Chris Gerhard. It has mostly moved to http://chrisgerhard.wordpress.com

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today