Score:0

Run a command against multiple directories with slightly different names

tr flag

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

pLumo avatar
in flag
"This is not working" is not a good error description. What happens and what would you expect to happen?
Score:0
it flag

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.

Nate T avatar
it flag
@steeldriver thx... again. I was thinking it read from stdin when with no arg. I checked everything up to there on my terminal, just because I didn't want anyone deleting their filesystems on my account. Now I wosh Id have made a dummy file or two. My mistake. Fixing it now. thanks again.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.