I want to run a command to delete backups so that I can reduce disk usage. Not sure how, but *
represents all the directories under the sites
directory
sudo rm -r sites/*/private/backups/*
My directory structure would be as given below:
ls sites
sites/hello/private/backups/
sites/world/private/backups/
sites/hello_world.txt
This is not working
NOTE: This assumes that you use the convention of adding the
~
suffix to backups. If you use something else, you need to change thefind
argument accordingly. For example, if you use, If you instead of something like.bak
, the*~
should instead be.bak
.Try this:
First, run
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 inThis will cause the shell to execute
rm PATHNAME
on each PATHNAME in the list.The
*
character inbash
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 tofind
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 isAs 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.