[NTLUG:Discuss] rm doesn't recurse
Steve Baker
sjbaker1 at airmail.net
Fri May 7 07:39:36 CDT 2004
terry wrote:
> So, will both these scripts do the same?
>
> for file in `find . -name "*.zip"`; do
> rm $file
> done
>
> &
>
> find . -name \*.zip -exec rm -f {} \;
>
> In other words, it's just 2 different ways of doing the same thing?
Yes - there are dozens of ways to do anything using command line tools!
There are subtle distinctions between these two ways of doing things - but
essentially they are the same. The first one runs 'find' to find all
the zip files - then inserts that list in place of the back-quoted
command. Thus, it runs a 'for' loop to delete them all. If you had
a truly VAST number of zip files, you would generate a command line
that blew the limits on the shell and this command would fail.
The second version has the find program search for files named *.zip
and deletes each one as it finds it. That doesn't suffer from the
command line length problem that the first version could die from.
Overall, the second method is vastly preferable.
The shortest command I can think of would be:
rm `find . -name \*.zip`
> I would experiment but can't think of any types of files I want to
> delete right now. I'm thinking these commands would delete all files
> with extension "zip" throughout the whole partition.
Not the whole partition...
In each case, after the word 'find' there is a '.' - what that does is
to tell 'find' to start searching at the directory named '.' - which in
UNIX/Linux is the current working directory. So what this command
does is to delete all zip files WITHIN THE CURRENT DIRECTORY AND BELOW.
So it's only going to take out all the zip files in an entire partition if:
1) You run the command from the highest directory in that partition.
2) You run the command using an account that has sufficient privilage
to do that (eg 'root').
However, if you did a 'cd /' before you ran the command, it would
start at '/' (the top of the entire file system) and thus delete all
.zip files on your entire system (and any partitions mounted
from other computers on your network too) !!
So - yes - this can be a VERY dangerous command - but there are some
checks. It can only delete files that you have permission to delete.
So unless you are stupid enough to run as 'root', it'll only delete
files that you have permission to delete - which would at least limit
the damage.
NEVER experiment with your system whilst logged in as root. When you
have to be logged in as root, treat each command you type as if it had
the power to trash your system beyond repair...be VERY careful!
There is one really common error - which is to type '*' instead of '*.zip'
and then discover that your disk drive has been wiped completely clean!
> If so, that's very powerful.
Yes.
---------------------------- Steve Baker -------------------------
HomeEmail: <sjbaker1 at airmail.net> WorkEmail: <sjbaker at link.com>
HomePage : http://www.sjbaker.org
Projects : http://plib.sf.net http://tuxaqfh.sf.net
http://tuxkart.sf.net http://prettypoly.sf.net
-----BEGIN GEEK CODE BLOCK-----
GCS d-- s:+ a+ C++++$ UL+++$ P--- L++++$ E--- W+++ N o+ K? w--- !O M-
V-- PS++ PE- Y-- PGP-- t+ 5 X R+++ tv b++ DI++ D G+ e++ h--(-) r+++ y++++
-----END GEEK CODE BLOCK-----
More information about the Discuss
mailing list