Skip to content

How to delete a lot of files from a directory.

October 24, 2006

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: topic:[solaris] topic:[unix]

Advertisements

From → Solaris

8 Comments
  1. Andrew permalink

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

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

  3. Peter permalink

    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

  4. 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

  5. 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

  6. Marc permalink

    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.

  7. Stuart Remphrey permalink

    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.

  8. Stuart Remphrey permalink

    Ohh, and whenever using “find…rm…” test with “find…echo rm…” first!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: