With great power comes the easy possibility of deleting all your files
Normally, when I want to use the find
command to delete files, I do this:
$ find . -name foo -exec rm {} \;
Turns out I could also do this:
$ find . -name foo -delete
However, while investigating the -delete
action, I read this in man find
:
-delete
Delete files; true if removal succeeded. If the removal failed, an error
message is issued. If -delete fails, find's exit status will be nonzero
(when it eventually exits). Use of -delete automatically turns on the
-depth option.
Warnings: Don't forget that the find command line is evaluated as an
expression, so putting -delete first will make find try to delete
everything below the starting points you specified.
Well, sure, that really does make sense. Putting the -delete
action where the expressions go would definitely cause a disaster. At the same time, you’d like to think that you might get warned first before find
went ahead and nuked all your files. Two takeaways from this:
-
UNIX doesn’t baby its users. If you tell your system to delete your files, by God, that’s what it’s going to do!
-
Always test your particular
find
construction first before unleashing its full fury.