basic command pipe question

Roberto Ragusa mail at robertoragusa.it
Fri Jun 23 08:05:51 UTC 2006


Steven W. Orr wrote:

> You have gotten three different suggestions so far.
> 
> 1. Use xargs
> 
> ls | grep -i '\.zip$' | xargs rm
> 
> 2. Use find with -exec
> 
> find . -iname \*.zip -exec 'rm {]' \;
> 
> 3. Use process substitution.
> 
> for i in $(find -iname "*.zip"); do rm  $i; done;
> 

I'd add a variant of 2:

4. Use find to write a script

find . -iname \*.zip -printf "rm \"%p\"\n"

this will output a script with a rm command for each file to be deleted.
You can easily check the files that will be deleted (which is good,
because errors involving wildcards can be dangerous) and if all is OK,
you just rerun the command in this way

find . -iname \*.zip -printf "rm \"%p\"\n"|sh

If instead of rm you want to do something more complicated this
printf trick is quite useful.
Moreover, creative addons may be:
- |wc -l         to count how many file are affected
- |split -l 1000 to have many scripts which can be run in parallel
...

Another tip: if rm asks you confirmation because is aliased to "rm -i",
you can just use /bin/rm.

Best regards.
-- 
   Roberto Ragusa    mail at robertoragusa.it




More information about the fedora-list mailing list