NOTE: This assumes that you use the convention of adding the ~
suffix to backups. If you use something else, you need to change the find
argument accordingly. For example, if you use, If you instead of something like .bak
, the *~
should instead be .bak
.
Try this:
First, run
sudo find /sites -type f -name '*~'
and look through the output. If you are comfortable with deleting the files in the listing, reenter the command, but pipe the output to rm
as in
sudo find /sites -type f -name '*~' -print0 | xargs -0 /usr/bin/rm
This will cause the shell to execute rm PATHNAME
on each PATHNAME in the list.
The *
character in bash
is a wildcard character. It will match one or more instances of almost any character. The most notable exception to this rule is the /
character, which is why it only matches one directory at a time in your example. Use **
to match any number of directories in a pathname.
EDIT:
Just noticed that you listed your directory as sites/...
and not /sites/...
. You need to be sure that the first parameter passed to find
is the full path to the directory. Otherwise, it will just return an error.
EDIT 2:
While I was fixing the second command, I realized that that find
itself can do what rm is doing but without the need to make any extra calls. This should work, but I still wouldn't advise doing it without first running the top version to see exactly what will be deleted. The command is
sudo find /sites -type f -name '*~' -delete
As a final tip, if you use find
a lot, and have system backups, you see your output overtaken by duplicates. If so, look up the -prune
option. It will keep anything matching its associated argument out of the output.