Recursively remove files

Recursively remove files

Post by Bob Hockn » Thu, 17 Jan 2002 08:00:17



Hi there,

Really silly question: how can I recursively remove all
files in or below a given directory that match a patttern,
such as "*.old".  Am I missing something, or is this not
as easy as "rm -r *.old" ?

-Bob

 
 
 

Recursively remove files

Post by K. Banerj » Thu, 17 Jan 2002 08:18:41


I believe that

rm -r *.old

will only remove file ending in ".old" from the current directory. The
following, however will work:

find . -name \*.old -type f -exec rm {} \;



>Hi there,

>Really silly question: how can I recursively remove all
>files in or below a given directory that match a patttern,
>such as "*.old".  Am I missing something, or is this not
>as easy as "rm -r *.old" ?

>-Bob


 
 
 

Recursively remove files

Post by Chris F.A. Johnso » Thu, 17 Jan 2002 08:29:48



Quote:> Hi there,

> Really silly question: how can I recursively remove all
> files in or below a given directory that match a patttern,
> such as "*.old".  Am I missing something, or is this not
> as easy as "rm -r *.old" ?

        find $DIR -type f -name '*.old' | xargs rm

If you may encounter files with spaces in their names:

        find $DIR -type f -name '*.old' -exec rm {} \;

Or, with GNU find:

        find $DIR -type f -name '*.old' -printf "\"%p\"\n" | xargs rm

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Recursively remove files

Post by John DuBo » Thu, 17 Jan 2002 12:43:46




Quote:>If you may encounter files with spaces in their names:
>...
>Or, with GNU find:

>    find $DIR -type f -name '*.old' -printf "\"%p\"\n" | xargs rm

That fails if filenames have double-quotes or backslashes in them.
If you have GNU find, you probably have GNU xargs.  I'm very much a fan of null
filename separation, enough so that I make sure my own utilities support it.

        gfind $DIR -type f -name '*.old' -print0 | gxargs -0 rm --

    John
--

 
 
 

Recursively remove files

Post by Chris F.A. Johnso » Thu, 17 Jan 2002 14:57:57





> >If you may encounter files with spaces in their names:
> >...
> >Or, with GNU find:

> >       find $DIR -type f -name '*.old' -printf "\"%p\"\n" | xargs rm

> That fails if filenames have double-quotes or backslashes in them.

Quite true; but I consider such things aberrations, and I do not
support them unless the specific situation calls for it. (And what it
usually calls for is a script to convert the names to sane ones as
quickly as possible.)

Spaces in file names are bad enough, but are, generally, easily
handled.

Quote:> If you have GNU find, you probably have GNU xargs.  I'm very much a fan of null
> filename separation, enough so that I make sure my own utilities support it.

>    gfind $DIR -type f -name '*.old' -print0 | gxargs -0 rm --

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
 
 
 

Recursively remove files

Post by Tapani Tarvaine » Thu, 17 Jan 2002 16:15:30






> > >If you may encounter files with spaces in their names:
> > >...
> > >Or, with GNU find:

> > >  find $DIR -type f -name '*.old' -printf "\"%p\"\n" | xargs rm

> > That fails if filenames have double-quotes or backslashes in them.

> Quite true; but I consider such things aberrations, and I do not
> support them unless the specific situation calls for it. (And what it
> usually calls for is a script to convert the names to sane ones as
> quickly as possible.)

One situation where they are a real problem is cron jobs ran as root
for clearing /tmp and the like in systems where you can't trust local
users (like, any educational institute...).
In any case you should take care that no filename can cause catastrophic
results,?like removal of a critical file; failure to remove a file is
less likely to be a problem.

Of course aberrant filenames aren't the only issue, some race
conditions involved are really hard to fix if other people
(processes) can mess with the directory tree at the same time
(like in the case of clearing /tmp).

--
Tapani Tarvainen

 
 
 

Recursively remove files

Post by Bob Hockn » Fri, 18 Jan 2002 01:52:05



> I believe that

> rm -r *.old

> will only remove file ending in ".old" from the current directory. The
> following, however will work:

> find . -name \*.old -type f -exec rm {} \;

Thanks to all who replied.  This works great.

-Bob