Re: deleting specific files (a litle note about "de nada" )
On Tue, 27 Mar 2001, Miguel S. Filipe wrote:
> >>>>> I need to delete a bunch of files, all of them of the form
> >>>>> *.doc, scattered into several subdirectories inside a given
> >>>>> directory. What should I do?
> >>>>
> >> <snip>
> >>
> >>> Several options:
> >>> - Create a script. This *is* my preferred method.
> >>>
> >>> $ find . -type f -name \*.doc | sed -e '/.*/s//rm &/' > rmscript
> >>> # Edit the script to make sure it's got The Right Stuff
> >>> $ vi rmscript
> >>> # run it
> >>> $ chmod +x rmscript; ./rmscript
I missed the original question, but I'd like to point out that this is a
great deal of unnecessary fuss.
You can do this all with one invocation of find, skipping the script, the
sed, and all that entirely. Here we go:
find . -name '*doc' -exec rm {} \; -print
will remove all files ending in 'doc' from the current directory and
subdirectories. The arguments following -exec are executed once for every
file matching the criteria, and {} is replaced with the filename. The
\; is required at the end of the -exec clause to indicate that you are
done with it. -print tells find to show what files were removed.
If you need to preserve a particular file:
find . -name '*doc' -not -name 'thesis.doc' -not -name 'budget.doc' -exec
rm {} \;
This will cause find to remove every file except thesis.doc and
budget.doc.
You can use any shell command with find - it doesn't have to be rm:
find . -name '*doc' -exec chmod 666 {} \; -print
will make every file ending with 'doc' world-writable and tell you what it
did.
You can even use multiple instances of exec:
find . -name '*doc' -not -name 'thesis.doc' -exec chmod 666 {} \; -exec
touch {} \; -print
which would make every file ending in 'doc' world-writable and update its
timestamp, except thesis.doc. (No, I have no idea why you would want to
do this :} )
Anyway, I think find is about the most powerful command in all of
Unix. Find is your friend :}
Reply to: