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.
-delete
switch before-name
deletes the specified file tree, so I guess I have to be careful. – Onion Nov 13 '14 at 18:56find
you can use-exec rm {} +
to remove all matched files with singlerm
command. – jimmij Nov 13 '14 at 18:59.DS_Store
doesn't contain any special characters at all, so the quotes are unnecessary and change nothing in this case. – Celada Nov 13 '14 at 19:09;
or|
or>
or<
and\
and many others which have special meaning to the shell unless quoted. – Celada Nov 13 '14 at 21:41xargs
takes care of the limited-size argument list problem transparently by breaking it up into multiple invocations of the command. – Celada Oct 22 '15 at 19:36/
afterfind
? – Valerio Mar 04 '21 at 09:07