Why did this command delete everything?

4

I run this command on a Unix box:

find . name CVS -exec rm -fr {} \;

I wanted to delete any file called CVS within any directory from the current directory and it deleted everything.

Fortunately all I had to do to recover was check out again from CVS. Imagine if I specified / as the starting directory!

I think the reason is that I used name instead of -name. I just rerun it as

find . -name CVS -exec rm -fr {} \;

And it seem to work fine. What exactly happens if name is used as opposed to -name?

ziggy

Posted 2010-10-30T12:17:11.733

Reputation: 333

3This why you should always be extremely careful around commands that might overwrite or delete files. Here a handy way to test would be find … -exec echo rm -fr {} \;. – Gilles 'SO- stop being evil' – 2010-10-30T13:38:23.343

@Gilles Can you please explain the command you've proposed? Thank you. – Eugene S – 2012-06-28T17:17:02.440

2@Eugene, the difference is the echo part, which just prints the command that would otherwise have been executed. The elipsis are just Gilles' way to not repeat the other parameters in the example. So, the full version would be find . name CVS -exec echo rm -fr {} \; – Arjan – 2012-06-28T17:29:13.030

@Arjan Thank you for your comment. I believe than under ellipsis you mean the underline "_", right? – Eugene S – 2012-06-28T17:31:47.150

1

Yes, @Eugene, these are actually three little dots!

– Arjan – 2012-06-28T17:32:33.540

Answers

7

You're missing the dash before -name, hence it was looking for paths named ., name and CVS, where the dot references the current folder, hence deleting all.

The find utility recursively descends the directory tree for each path listed.

You can easily test by using echo before the command you want to run:

find . name CVS -exec echo rm -fr {} \;

Arjan

Posted 2010-10-30T12:17:11.733

Reputation: 29 084

@ziggy, in case you missed my edit: so it was looking for paths named name and CVS. – Arjan – 2010-10-30T12:27:10.100

Yes that makes sense. I have to be very carefull with this command. Thanks – ziggy – 2010-10-30T12:33:51.177

Paths named name CVS and obviously ., which it found. – Rob – 2012-06-28T17:13:46.157