Question : Using "find" with "rm to delete files

Greetings,

While testing possible ways to use a shell script to delete files, I typed the following at the command prompt in a directory called /tmp/test and pressed :

find / \ -name foo -exec \ rm {} \;

As the command was running I got a lot of scary messages about "such and such is a directory", etc.   I quickly pressed Cntl-C to stop it.  

What I was trying to do was search the machine for all files named "foo" and delete them.  Does anyone know if the above may have deleted other things?

Thanks!

Answer : Using "find" with "rm to delete files

You are ok but you have learned not to do this now, haven't you? Remember UNIX will do *exactly* what you tell it to do.

In this case, you were saved by the slash in " -exec \ rm". This will have passed the string "rm" to the code to find the command to "exec" and will have failed. I checked.

The error mesages were probably like this:

find: 0652-081 cannot change directory to :
  : The file access permissions do not allow the specified action.

That's ok - the security was preventing you from doing damage. Unless you "got root" :-)

The bigger mistake was to run "find / ..." which finds from the root down when you should have run "find ." to find from the current directory down. I strongly recommend against that. If you need to do it, user fimd to create the list and save it to a file first, then use the list in a script.
Random Solutions  
 
programming4us programming4us