recusive ls/search/replace function!!

Deron Meranda deron.meranda at gmail.com
Wed Apr 27 02:37:31 UTC 2005


On 4/26/05, Johnathan Bailes <johnathan.bailes at gmail.com> wrote:
> > any idea how to delete all files in a subdirectory with a certain
> > extension...??

> find /home/enduser/stuff/* -name "*.blah" -exec rm {} \;

It's good practice to use a couple more options to make it
safer:

  find /some/dir -type f -name "*.blah" -exec rm -f -- {} \;

The options, -type f, will insure that only regular *files* are
deleted; not directories, sockets, etc.

And the "--" in the rm command will insure that any files that
happen to have names starting with a hyphen are not interpreted
as options to the rm command rather than the filename to
delete.  The -f option forces the delete even if the file is marked
as read-only (of course use with caution!).


If you have a vary large number of files to delete, you can
speed things up significantly with the following:

  find /some/dir -type f -name "*.blah" -print0 | xargs -0 rm -f --

The xargs(1) command will bundle up a whole bunch of filenames
and then call the rm command just once on that set (probably
around 100 at a time).  This is much faster than calling rm once
for every file.   The -print0 and -0 options insure that any strange
characters in the filenames are not interpreted incorrectly (it is
possible for filename to contain newlines and spaces for instance).

One final word of caution; always use the user with the least
permissions---try not to do this sort of thing as root unless you
have no other choice.
-- 
Deron Meranda




More information about the fedora-list mailing list