2

No easy way to explain but this is my question in demonstration. I have a file server with home directory backups. They read like:

/home/ron/Downloads
/home/ron/Documents
/home/rob/Downloads
/home/rob/Documents
/home/tom/Downloads
/home/tom/Documents
/home/don/Downloads
/home/don/Documents

How would i delete all the folders called Downloads with one command, not invoking the names of the users, but with a shorthand?

I was thinking:

rm -r /home/*/Downloads

What would be the best way to do this?

Note: I have a few hundred folders to purge like this on more than one server.

Thanks.

6

You can use find:

find /home -maxdepth 2 -type d -name Downloads -exec rm -r {} + 
| improve this answer | |
  • 1
    Might wanna also specify maxdepth parameter – Sergiy Kolodyazhnyy Jul 11 '17 at 19:05
  • @SergiyKolodyazhnyy Yeah ... done :) – Ravexina Jul 11 '17 at 19:06
  • Thanks. So the find command essentially scans all subdirectories for anything named Downloads? – dns dave Jul 11 '17 at 19:10
  • With -maxdepth 1 it only looks under the /home not deeper ... – Ravexina Jul 11 '17 at 19:18
  • 2
    @Ravexina "play with the number when you was happy with the result" shouldn't you have done that before posting an answer? As far as I can see it needs to be -maxdepth 2. To be strictly equivalent to the OP's /home/*/Downloads you'd need to add -mindepth 2 as well (in case there is a /home/Downloads that should be preserved). And why -iname? IMHO you should at least explain the difference between -iname and -name in case the OP doesn't want to delete /home/ron/downloads for example. – steeldriver Jul 11 '17 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.