Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I issue the following command to find the .svn directories:

find . -name ".svn"

That gives me the following results:


How could I process all these lines with rm -fr in order to delete the directories and their content?

share|improve this question
GNU find has the -delete option. – Marco Sep 9 '13 at 8:49
Or you can add -exec rm -r "{}" \; to the end of the find - be careful when using rm -r! :) – Drav Sloan Sep 9 '13 at 8:52
@Marco The delete option does not seem to work on directories. – Super Chafouin Sep 9 '13 at 9:01
@Drav It works, thanks ! Maybe you could add this as an answer so I would accept it ? – Super Chafouin Sep 9 '13 at 9:07
@SuperChafouin but will not work for paths with spaces in them (hence using -exec with quoted "{}"). – Drav Sloan Sep 9 '13 at 9:55
up vote 26 down vote accepted

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, a way to hack around that is to use rmdir instead of rm -r:

find . -name ".svn" -type d -exec rmdir "{}" \;

This will delete empty directories and give errors for directories with contents. If you do not want to see the errors, redirect the STDERR to /dev/null

find . -name ".svn" -type d -exec rmdir "{}" \;  2> /dev/null
share|improve this answer
I have seen advice to always run -type after -name in find commands, since calls to stat to get the type are expensive. I just tried it myself on a fairly large bunch of files, and it seems to be true: running find . -name 'foo' -type d took 19 secs, while find . -type d -name 'foo' took 32 secs. So about 50% longer time to run -type first. – spinup Mar 16 '15 at 16:37
i've been using this command for years, but on Mac now i get errors saying those directories do not exist. Even though it does delete them. I never saw messages before. – chovy Dec 23 '15 at 8:51

Assume you are using gnu find, you can use the -delete option:

find . -name test -delete

which is easier to remember.

share|improve this answer
Consider expanding your post with an explanation of the command (or documentation to back up your solution). Often one (or two) line answers are not the most illuminating. – HalosGhost Aug 10 '14 at 23:02
This doesn't work on non-empty directories. – belacqua Dec 16 '14 at 20:47

Here is a portable way to do it, i.e. here something that doesn't require GNU find.

Especially if you have a lot of .svn directories, using a + instead of a semicolon as find command terminator is slightly more optimized:

find . -name ".svn" -type d -exec rm -rf {} +

Note also that you never1 need to quote the curly braces when used that way.

1 Unless you use the fish shell.

share|improve this answer

Bash specific solution:

shopt -s globstar
rm -r **/.svn
shopt -u globstar #optional. this will disable globstar expansion
share|improve this answer
Note for command line expansion of globs that match many files, there is a limit to the number of files you can match with this mechanism. Going over this limit will result in bash: /bin/rm: Argument list too long – Drav Sloan Sep 9 '13 at 10:06
@DravSloan is correct, but that limit is in the hundreds of thousands of files. It's something to bear in mind, but probably won't be a problem for most people. – evilsoup Sep 9 '13 at 10:26

On my computer when I use:

find . \( -name dirname -type d \) -exec rm -r '{}' ';'

The directories are deleted but I get the error:

find: ‘./dirname’: No such file or directory

for each directory.

My directories aren't empty, so the -delete option won't work for me. I found the reason for this behavior here:

  1. find takes (not necessarily) the first entry in the ./ directory. this would be e.g. dir.1/
  2. it compares it to the pattern 'dir.?'. does it match? yes.
  3. find executes "rm -r dir.1".
  4. find tries to enter dir.1/ to find the pattern within the directory. it doesn't know anything about the exec command.
  5. it doesn't find dir.1/ anymore. returns ENOENT (look at the strace output)

I used this instead to work around:

rm -r `find . -name dirname -type d`

Keep in mind that find will still try to recurse into directories named dirname, which isn't really necessary and will take some additional time. Depending on your directory structure, you might be able to work around this with the --depth find option. In addition, if you have a directory structure like dirname/foo/dirname you will get "No such file or directory" errors from rm. To suppress the errors you can redirect stderr to /dev/null or use the -f (force) flag with rm.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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