How to delete directories based on `find` output?
Find can execute arguments with the -exec
option for each match it finds. It is a recommended mechanism because you can handle paths with spaces/newlines and other characters in them correctly. You will have to delete the contents of the directory before you can remove the directory itself, so use -r
with the rm
command to achieve this.
For your example you can issue:
find . -name ".svn" -exec rm -r "{}" \;
You can also tell find to just find directories named .svn by adding a -type d
check:
find . -name ".svn" -type d -exec rm -r "{}" \;
Warning Use rm -r
with caution it deletes the folder and all its contents.
If you want to delete just empty directories as well as directories that contain only empty directories, find can do that itself with -delete
and -empty
:
find . -name ".svn" -type d -empty -delete
Here is a portable still faster than the accepted answer way.
Using a +
instead of a semicolon as find
command terminator is optimizing the CPU usage. That can be significant if you have a lot of .svn
sub-directories:
find . -name .svn -type d -exec rm -rf {} +
Note also that you never1 need to quote the curly braces here.
1 Unless you use the fish
shell (this might have been fixed since I wrote this reply).
Assume you are using gnu find, you can use the -delete
option:
find . -name test -delete
which is easier to remember.