Geek Pop Quiz!
Aug. 11th, 2008 03:02 pmIn bash, using only standard easily-available commands, how do I:
1) search an arbitrarily deep folder tree for all directories with a name matching a certain set pattern
2) delete those directories and all their contents, but *only* those directories and their contents.
There's gotta be a single command to do this. But it's also on a remote web server, meaning I can't just cheat and use the gui file system's search and delete commands.
EDIT:
hazmat provides me with
1) search an arbitrarily deep folder tree for all directories with a name matching a certain set pattern
2) delete those directories and all their contents, but *only* those directories and their contents.
There's gotta be a single command to do this. But it's also on a remote web server, meaning I can't just cheat and use the gui file system's search and delete commands.
EDIT:
find /top/of/tree -type d -name your-glob-goes-here -exec rm -r {} \;Which works perfectly!
(no subject)
Date: 2008-08-11 07:07 pm (UTC)So if it's directories with the substring "frederick" you're trying to delete, that'd be:
find /top/of/tree -type d -name "*frederick*" -exec rm -r {} \;
(no subject)
Date: 2008-08-11 07:11 pm (UTC)(no subject)
Date: 2008-08-11 07:19 pm (UTC)(no subject)
Date: 2008-08-11 07:09 pm (UTC)-r is recursive, -f is forcing, so it'll nail directories, too.
You are totally fucked if you typo something, though. Once I deleted everything in my user directory with that command, because I was in my home directory instead of the one I thought I was in. Changed my .login after that....
ETA: Maybe I misunderstood what you're asking, 'cause that seems too easy.
(no subject)
Date: 2008-08-11 07:11 pm (UTC)If I could get rm to pattern-match while parsing subdirectories, sure. But it doesn't.
(no subject)
Date: 2008-08-11 07:12 pm (UTC)(no subject)
Date: 2008-08-11 07:13 pm (UTC)(no subject)
Date: 2008-08-11 07:19 pm (UTC)I knew it was possible. Just not how.
(no subject)
Date: 2008-08-11 07:23 pm (UTC)(no subject)
Date: 2008-08-11 07:26 pm (UTC)So you can check the output and make sure it's only going to apply to the folders you want.
And the "-exec" part is really simple:
-exec means "For each result, run everything between here and the escaped semicolon as a command"
- which is, in this case "rm -r {result}"
(no subject)
Date: 2008-08-11 07:27 pm (UTC)A) triple-check ALL of your logic before pressing enter
B) spell out anything confusing instead of using a "simpler" regex
C) run a "does nothing" test run
D) do it on somebody else's server where they're contractually obligated to restore your changes from backup if you screw it up.
(no subject)
Date: 2008-08-11 07:43 pm (UTC)(no subject)
Date: 2008-08-11 07:49 pm (UTC)In this case, I knew in advance that none of the directories had spaces in them.
(no subject)
Date: 2008-08-11 07:56 pm (UTC)I more often do find ... | xargs command, which does trip me up on embedded-space filenames.
(no subject)
Date: 2008-08-11 08:01 pm (UTC)(no subject)
Date: 2008-08-11 11:17 pm (UTC)(no subject)
Date: 2008-08-13 07:37 am (UTC)