views:

987

answers:

10

One of the mission of an export tool I have in my application, is to clean all .svn directories from my app directory tree.
I am looking for a recursive command in linux shell that will traverse the entire tree and delete the .svn files.
I am not using export, as this script will be used for some other file/directory names which are not related to SVN.
I tried something like:

find . -name .svn | rm -fr

Didn't work...

+2  A: 

You almost had it. If you want to pass the output of a command as parameters to another one, you'll need to use xargs. Adding -print0 makes sure the script can handle paths with whitespace:

find . -type d -name .svn -print0|xargs -0 rm -rf
soulmerge
for some reson what worked eventualy was find . -name .svn |xargs rm -rf Your version gave me the direction though, thanks
Itay Moav
+1  A: 

Try this:

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

Read more about the find command at developerWorks.

scompt.com
+6  A: 

Try this:

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

Before running a command like that, I often like to run this first:

find . -name .svn -exec ls {} \;
Greg
A: 

If you don't like to see a lot of

find: `./.svn': No such file or directory

warnings, then use the -depth switch:

find . -depth -name .svn -exec rm -fr {} \;
lazy1
A: 
find . -name .svn |xargs rm -rf
Itay Moav
A: 

Alternatively, if you want to export a copy without modifying the working copy, you can use rsync:

rsync -a --exclude .svn path/to/working/copy path/to/export
pimlottc
Or use svn export /path/to/repository, which essentially does the same thing.
Kaleb Pederson
The author specifically said he didn't want to use svn; perhaps his target environment doesn't have svn installed.
pimlottc
A: 

What you wrote sends a list of newline separated file names (and paths) to rm, but rm doesn't know what to do with that input. It's only expecting command line parameters.

xargs takes input, usually separated by newlines, and places them on the command line, so adding xargs makes what you had work:

find . -name .svn | xargs rm -fr

xargs is intelligent enough that it will only pass as many arguments to rm as it can accept. Thus, if you had a million files, it might run rm 1,000,000/65,000 times (if your shell could accept 65,002 arguments on the command line {65k files + 1 for rm + 1 for -fr}).

As persons have adeptly pointed out, the following also work:

find . -name .svn -exec rm -rf {} \;
find . -depth -name .svn -exec rm -fr {} \;
find . -type d -name .svn -print0|xargs -0 rm -rf

The first two -exec forms both call rm for each folder being deleted, so if you had 1,000,000 folders, rm would be invoked 1,000,000 times. This is certainly less than ideal. Newer implementations of rm allow you to conclude the command with a + indicating that rm will accept as many arguments as possible:

find . -name .svn -exec rm -rf {} +

The last find/xargs version uses print0, which makes find generate output that uses \0 as a terminator rather than a newline. Since POSIX systems allow any character but \0 in the filename, this is truly the safest way to make sure that the arguments are correctly passed to rm or the application being executed.

In addition, there's a -execdir that will execute rm from the directory in which the file was found, rather than at the base directory and a -depth that will start depth first.

Kaleb Pederson
+3  A: 

EDIT

I see now you're in Linux... Gonna leave this here any way because of its usefulness to Windows users.


I've got something that should make your day. Original source is here.

This is a (perfectly safe) Shell Extension that will add "Delete SVN Folders" to your right click menu in Windows. Run it on any directory containing those pesky files.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\DeleteSVN]
@="Delete SVN Folders"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\DeleteSVN\command]
@="cmd.exe /c \"TITLE Removing SVN Folders in %1 && COLOR 9A && FOR /r \"%1\" %%f IN (.svn) DO RD /s /q \"%%f\" \""
Nathan Taylor
+2  A: 

No need for pipes, xargs, exec, or anything:

find . -name .svn -delete
alberge
nice. thanks. wasn't aware of the `-delete` argument
KennyCason
+1  A: 

There are already many answers provided for deleting the .svn-directory. But I want to add, that you can avoid these directories from the beginning, if you do an export instead of a checkout:

svn export <url>
Mnementh
+1, there's no need to redo this when it's already built-in to svn.
R. Bemrose