Reputation: 23553
How can I delete all subdirectories of a certain name via the command line on mac?
Eg if I have
/parent/foo
/parent/child/foo
/parent/child/child2/foo
How can I run a command from /parent
that will delete any folder called foo
and all of it's contense?
Ive tried this command but no luck:
find . -type d -name foo -delete
Upvotes: 1
Views: 2514
Reputation: 53000
Using find
you can do:
find . -d -type d -name a -exec rm -r {} \;
As you have discovered -delete
does not appear to delete non-empty directories (so its equivalent to rmdir
not rm -r
), though this may not be documented.
The -exec
executes a command, {}
is replaced by the matching pathname, and \;
marks the end of the command. So this does a recursive removal of every matching path.
The -d
sets depth first traversal which means directories are processed after their contents. If you omit this find
will first remove the directory and then try to recurse into it – this results in error messages but still works.
There is a shorter way using zsh
:
rm -r **/a
The pattern **
does a search an matches a
at any depth.
If zsh
is not your default shell you can use its -c
argument to run a command from another shell:
zsh -c 'rm -r **/a'
HTH
Upvotes: 3
Reputation: 9
The name of the directory has to be in quotes. Do the following:
find . -type d -name "foo" -delete
Upvotes: -1