[NTLUG:Discuss] rm doesn't recurse
David White
davidnwhite at optusnet.com.au
Thu May 6 08:57:45 CDT 2004
> rm man page says -r option will make rm recurse through directories. It
> doesn't.
>
> I'm trying to remove *.zip from all sub-directories below the current.
> If rm won't do it, what will?
When it says it will 'recurse', it means if you give it the name of a
directory, it will remove all files and directories beneath that directory
and then delete the directory.
The way shells in Linux work is to expand filename patterns and pass the
expanded list of filenames to the command. E.g. if you have the files
'a.zip' and 'b.zip' in your cwd and 'c.zip' and 'd.zip' in a directory
called 'subdir', and you type 'rm -r *.zip', as far as rm is concerned, the
command you typed is,
rm -r a.zip b.zip
It doesn't 'know' that you typed a wildcard in there at all.
The way you can do what you want to do is by using 'find' to list all the
zip files and then delete each one:
for file in `find . -name "*.zip"`; do
rm $file
done
Note that using quotes around the *.zip prevents the shell from expanding it
before passing it to the 'find' command.
David
More information about the Discuss
mailing list