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?

5
  • 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 {} \;. Commented Oct 30, 2010 at 13:38
  • @Gilles Can you please explain the command you've proposed? Thank you.
    – Eugene S
    Commented Jun 28, 2012 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
    Commented Jun 28, 2012 at 17:29
  • @Arjan Thank you for your comment. I believe than under ellipsis you mean the underline "_", right?
    – Eugene S
    Commented Jun 28, 2012 at 17:31
  • 1
    Yes, @Eugene, these are actually three little dots!
    – Arjan
    Commented Jun 28, 2012 at 17:32

1 Answer 1

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 {} \;
3
  • @ziggy, in case you missed my edit: so it was looking for paths named name and CVS.
    – Arjan
    Commented Oct 30, 2010 at 12:27
  • Yes that makes sense. I have to be very carefull with this command. Thanks
    – ziggy
    Commented Oct 30, 2010 at 12:33
  • Paths named name CVS and obviously ., which it found.
    – Rob
    Commented Jun 28, 2012 at 17:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .