[NTLUG:Discuss] rm doesn't recurse

Robert Citek rwcitek at alum.calberkeley.org
Fri May 7 12:37:56 CDT 2004


On Friday, May 7, 2004, at 09:35 US/Central, Steve Baker wrote:
> 2) You could redirect the results of find into another command's 
> standard input.
>    However, 'rm' doesn't have an option to take the list of files from 
> the command
>    line.  There is a program (xargs) that can take a list of files on 
> standard input
>    and convert it to a list.
>
>    eg:
>       find . -name \*.zip | xargs rm {}
>
>    This is a perfectly good solution - as 'find' produces matching 
> filenames, it
>    passes them to xargs which runs the 'rm' command with that file as 
> it's argument.

And if your filenames happen to have spaces:

   $ find . -type f -name \*.zip -print0 | xargs -0 rm -f

Using xargs is often significantly faster than find's -exec, especially 
for large numbers of files.  Personally, I prefer to check the results 
before I trash large numbers of files:

   $ mkdir /tmp/old.zips
   $ find . -type f -name \*.zip -print0 | xargs -0 mv 
--target-directory=/tmp/old.zips

After all, in linux, when a file is removed, it's gone for good.

Regards,
- Robert




More information about the Discuss mailing list