theweaselking: (Work now)
[personal profile] theweaselking
In 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:
[livejournal.com profile] hazmat provides me with
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)
From: [identity profile] hazmat.livejournal.com
find /top/of/tree -type d -name your-glob-goes-here -exec rm -r {} \;

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:09 pm (UTC)
From: [identity profile] kadath.livejournal.com
rm -rf

-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.
Edited Date: 2008-08-11 07:10 pm (UTC)

(no subject)

Date: 2008-08-11 07:11 pm (UTC)
From: [identity profile] theweaselking.livejournal.com
That doesn't work, because I want to keep all the other files and folders under there.

If I could get rm to pattern-match while parsing subdirectories, sure. But it doesn't.

(no subject)

Date: 2008-08-11 07:11 pm (UTC)
From: [identity profile] theweaselking.livejournal.com
I knew there was a Find command that would work. Thanks, testing that now.

(no subject)

Date: 2008-08-11 07:12 pm (UTC)
From: [identity profile] kadath.livejournal.com
Right then, I did misunderstand.

(no subject)

Date: 2008-08-11 07:13 pm (UTC)
From: [identity profile] kadath.livejournal.com
Also, I saw the Pearls Before Swine icon and assumed you were [livejournal.com profile] fengi, which is why I gave such an obvious answer. ;)

(no subject)

Date: 2008-08-11 07:19 pm (UTC)
From: [identity profile] theweaselking.livejournal.com
Hazmat found me a working solution - use find to locate the directories, then -exec to run a shell command on each directory you find.

I knew it was possible. Just not how.

(no subject)

Date: 2008-08-11 07:19 pm (UTC)
From: [identity profile] theweaselking.livejournal.com
Perfect! Thanks.

(no subject)

Date: 2008-08-11 07:23 pm (UTC)
From: [identity profile] kadath.livejournal.com
I am terrified of recursing anything, because I always tyop something important and delete my mail folder or overwrite my entire public_html directory with gibberish piped from rand or something. I'm really good at fucking up from the command line.

(no subject)

Date: 2008-08-11 07:26 pm (UTC)
From: [identity profile] theweaselking.livejournal.com
Conveniently, you can eliminate the "-exec" part and it will simply output everything it finds.

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)
From: [identity profile] theweaselking.livejournal.com
PS: That's why you
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)
ext_8707: Taken in front of Carnegie Hall (bofh)
From: [identity profile] ronebofh.livejournal.com
I would recommend making that rm -r "{}" \; in case there are any directories that have spaces in their name.

(no subject)

Date: 2008-08-11 07:49 pm (UTC)
From: [identity profile] theweaselking.livejournal.com
Oooh. Could be important, that.

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)
From: [identity profile] hazmat.livejournal.com
All the finds I've used have composed their own arglists on -exec without invoking a shell, so the quoting isn't necessary. There may be some implementations which run a shell, though, which would make it a good idea to quote the {}.

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)
ext_8707: Taken in front of Carnegie Hall (clue jar - take two)
From: [identity profile] ronebofh.livejournal.com
Yep, you are correct. I was thinking of find|xargs.

(no subject)

Date: 2008-08-11 11:17 pm (UTC)
From: [identity profile] skington.livejournal.com
That's why you use find .... -print0 | xargs --null instead.

(no subject)

Date: 2008-08-13 07:37 am (UTC)
From: [identity profile] rbarclay.livejournal.com
Sometimes, -iname is useful, too.

Profile

theweaselking: (Default)theweaselking
Page generated Feb. 5th, 2026 07:45 pm