Take the 2-minute tour ×
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.

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

find . -name ".svn"

That gives me the following results:

./toto/.svn
./toto/titi/.svn
./toto/tata/.svn

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

share|improve this question
2  
GNU find has the -delete option. –  Marco Sep 9 '13 at 8:49
3  
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
1  
@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
1  
@SuperChafouin but will not work for paths with spaces in them (hence using -exec with quoted "{}"). –  Drav Sloan Sep 9 '13 at 9:55

4 Answers 4

up vote 7 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 . -type d -name ".svn" -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 . -type d -name ".svn" -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 . -type d -name ".svn" -exec rmdir "{}" \;  2> /dev/null
share|improve this answer

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 is slightly more optimized:

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

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

share|improve this answer

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 at 23:02
    
This doesn't work on non-empty directories. –  belacqua 2 days ago

Bash specific solution:

shopt -s globstar
rm -r **/.svn
shopt -u globstar #optional. this will disable globstar expansion
share|improve this answer
1  
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
1  
@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

Your Answer

 
discard

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.