Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
3  
This 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 Oct 30 '10 at 13:38
    
@Gilles Can you please explain the command you've proposed? Thank you. –  Eugene S Jun 28 '12 at 17:17
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 Jun 28 '12 at 17:29
    
@Arjan Thank you for your comment. I believe than under ellipsis you mean the underline "_", right? –  Eugene S Jun 28 '12 at 17:31
1  
Yes, @Eugene, these are actually three little dots! –  Arjan Jun 28 '12 at 17:32

1 Answer 1

up vote 7 down vote accepted

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 {} \;
share|improve this answer
    
Yes just noticed that :) Thanks. –  ziggy Oct 30 '10 at 12:24
    
@ziggy, in case you missed my edit: so it was looking for paths named name and CVS. –  Arjan Oct 30 '10 at 12:27
    
Yes that makes sense. I have to be very carefull with this command. Thanks –  ziggy Oct 30 '10 at 12:33
    
Paths named name CVS and obviously ., which it found. –  Rob Jun 28 '12 at 17:13
    
You're right, @Rob, I guess I didn't even realize that when I wrote the above. –  Arjan Jun 28 '12 at 17:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.