Deleting all folders and files underneath it in linux with a certain name?

5

I have the following structure:

/.svn
/bla/.svn
/hello/.svn
/bla/bla/bla/.svn
... etc

I want to delete all the .svn folders. How do I do it?

It's NOT:

rm -rf .svn

In windows you use the /s trigger. How do you do it linux?

coderama

Posted 2010-05-25T09:46:15.377

Reputation: 699

Answers

11

Use the command find:

find . -type d -name .svn -exec rm -rf {} \;

{} is the filename, -type d means directories.

Warning: use find and rm together with caution!

Peter Jaric

Posted 2010-05-25T09:46:15.377

Reputation: 1 756

+1 for the warning, find and rm is potentially a very dangerous combination. – Dom – 2010-05-25T10:03:09.907

1Will fill your terminal with lots of warnings of "find: '.../.svn': No such file or directory". The directories get removed, but you can avoid the warnings with -depth (see my answer). – DevSolar – 2010-05-25T11:37:48.587

9

find . -depth -type d -name .svn -exec rm -rf {} \;

Differences to other solutions already presented:

  • without -depth, the command will attempt to recurse into the .svn directory after it has deleted it, resulting in error messages.

  • without -type d (which limits your search to directories), you would also delete files of the given name. (Most likely you won't have any, but you shouldn't take chances when doing a combination of "find" and "rm".)

DevSolar

Posted 2010-05-25T09:46:15.377

Reputation: 3 860

While your -depth addition is very good (+1 from me), I did have the -type d option in my solution. – Peter Jaric – 2010-05-25T15:00:11.113

5

If your exact need is to remove .svn directories and not a more general solution for removing specifically named directories, you should consider using svn export:

$ svn export . /tmp/new-dir

This will create a copy of the svn work area in the current directory into a new directory at /tmp/new-dir. You don't need to create the new directory first. Subversion will take care of that for you.

Doug Harris

Posted 2010-05-25T09:46:15.377

Reputation: 23 578

2

find . -name .svn -exec rm -rf {}\;

Should work for you.
It first finds all folders named .svn Then executing rm -rf for each folder found.

Result:
No more .svn folders

S.Hoekstra

Posted 2010-05-25T09:46:15.377

Reputation: 2 231

find: missing argument to `-exec' – Paused until further notice. – 2010-05-25T13:45:39.700

That's what happens when you forget the space before \;. – DevSolar – 2010-05-26T09:51:29.803