For a machine such as your macbook you won't find much difference in performance between the two commands. However, if you look at the -exec
version you can see a subtle difference:
sudo find / -iname ".file-to-delete" -exec rm {} \;
This means that you will find all those files with name .file-to-delete
. However this search might return some unwanted false positives. When doing something with sudo
you should be a bit more careful. The advantage of using -exec rm {}
is that you can pass arguments to rm
like this:
sudo find / -iname "*~" -exec rm -i {} \;
In this example I want to remove those backup files that emacs
makes. However that tilde could be in some obscure file that I don't know about and could be important. Plus I want to confirm the delete. So I put the option -i
on the rm
command. This will give me an interactive delete.
Also you can refine the usage of rm
to delete directories as well as files:
find /usr/local/share/ -iname "useless" -exec rm -r {} \;
In brief, the -exec
gives you a bit more control over the actual command that removes the found item. The advantage is that you use one tool to find the files, another tool to remove them. Also not every version of the find utility has the -delete
option. So better to use each tool for its proper job. This is the unix philosophy - one tool, one job, use them together to do what you need to do.