Tuesday, July 1, 2008

Clear all .svn files from a subversion working copy (UPDATE)

To detach a checked-out subversion working copy and therefore clean all subversion related files (e.g. those nested .svn directories) you can type:

rm -rf `find . -type d -name .svn`

You can also make yourself a little bash script (called e.g. svnclear):

echo "recursively removing .svn folders from"

rm -rf `find . -type d -name .svn`

via AnyExample

Update 04.05.2009:

As Isimonis pointed out in the comments, it is better to use a different approach if you have many files:

find $DIR -type d -name ".svn" -print0|xargs -0 rm -rf

Or you can use the new options of newer "find" versions:

find $DIR -type d -name ".svn" -delete


Greg said...

Hi Marc,

You might also want to have a look at the "svn export" command. This command creates a clean version (i.e. no .svn directories) of your repository, suitable for archiving or using as an installation tarball.

See also: http://svnbook.red-bean.com/en/1.4/svn.ref.svn.c.export.html

Hope that helps,
Greg Larkin
SourceHosting.net, LLC

GTD Marc said...

Hi Greg,

Thanks for the hint. That is of course the preferred method to get a "clean" version of a Subversion repository content.

But if you don't have anymore connectivity to the Subversion repository location (e.g. does not exist anymore or is not accessible) you might need to use the brute force cleaning method. :-)



Jamie said...

Thanks, this is great if you've completely screwed up your working copy (e.g. by making new subdirectories using "cp" instead of "svn cp" and only realising it several revisions later) and you want to clean all the .svns so you can start a new repository. In that case svn export doesn't help!

lsimonis said...

That would fail if there are a huge number of .svn directories (which can happen pretty easily for large projects). You would get an error like "argument too long"

You could do this:

find $DIR -type d -name ".svn" -exec rm -rf {} \;. . . to run "rm" once for each .svn directory it finds.

This isn't very efficient, though, since it will run "rm" as many times as there are results from "find"

A more efficient way to do it is to pipe to "xargs":

find $DIR -type d -name ".svn" -print0|xargs -0 rm -rfThis passes results from "find" to "rm" in discrete chunks. (the "-print0" and "-0" flags are only necessary to handle filenames with spaces, so not relevent to this example, but good practice nonetheless)

New GNU versions of "find" make all of this obsolete, though, with the introduction of the "-delete" option:

find $DIR -type d -name ".svn" -delete;-)

GTD Marc said...

Hi Isimonis

Thanks for the hints, I will update the post to include the xargs version and the tip with the new verison of find.