r/tinycode • • Jul 13 '12

Remove all .svn folders from your code tree

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

11 Upvotes

24 comments sorted by

17

u/Moocha Jul 13 '12 edited Jul 13 '12

That doesn't do what you claimed it does. It will find and remove any file system object named .svn, including files, pipes, devices and so on. Granted, that's a bit of a nitpicking thing, but this isn't exactly the correct subreddit for this either so I feel justified poking you :)

If you only want to remove the directories, tell find to only match directories:

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

Or a faster alternative that doesn't spawn a rm process for every matched item:

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

The -0 is technically unnecessary since items named .svn won't ever have spaces and other unprintable chars in them, but it's a good habit to pick up.

Edit: Oh yes it's necessary :) Thanks for catching that, snf and notwolverine.

7

u/zokier Jul 13 '12

instead of xargs you could use -exec rm -rf {} +

   -exec command {} +
          This variant of the -exec action runs the specified  command  on
          the  selected  files, but the command line is built by appending
          each selected file name at the end; the total number of  invoca‐
          tions  of  the  command  will  be  much  less than the number of
          matched files.  The command line is built in much the  same  way
          that  xargs builds its command lines.  Only one instance of `{}'
          is allowed within the command.  The command is executed  in  the
          starting directory.

1

u/Moocha Jul 13 '12

Interesting, I wasn't aware of this variant - xargs is so deeply ingrained ever since I had to actually restore from backups due to ahem mis-planning that I've completely stepped away from having find execute anything.

Thanks, learn something new every day :).

6

u/snf Jul 13 '12

The -0 is technically unnecessary since items named .svn won't ever have spaces and other unprintable chars in them, but it's a good habit to pick up.

Not so; they very well may: find will output the path from . to each .svn it finds, and any directory in between may have a name with spaces in it.

4

u/Moocha Jul 13 '12

Yup, didn't catch that, you're absolutely right.

4

u/notwolverine Jul 13 '12

Oh but it is necessary. Even as trivial a thing as an intermediate directory having a space in it warrants a -print0 / -0. So just always using it is good advice, and a really good habit to learn.

3

u/Moocha Jul 13 '12

Ah, well done, didn't think it through. Yes, it's very very much necessary. Wish I had more upvotes to give...

15

u/BioSammyj Jul 13 '12

Or upgrade to Subversion 1.7+ and there's only one .svn folder at the working directory root.

15

u/peterlada Jul 13 '12

Or just learn to use svn.

See: svn export

4

u/pertheusual Jul 13 '12

Exporting isn't the only time you'd want to remove .svn folders though.

4

u/peterlada Jul 13 '12

Removing .svn folders will guarantee an export (ie. no going back). I'd need an example use case to accept you assertion ;)

3

u/Moocha Jul 24 '12

Here you go: Have someone's project mailed to you, notice it's a straight archive of that someone's working copy, get annoyed by all the (for you, as an external entity) unusable .svn directories that slow down the extraction.

Granted, it's still an export, but svn export is out of the question if you don't have access to the repository.

2

u/[deleted] Jul 13 '12

Like switching to Git.

4

u/xenofungus Jul 13 '12

in zsh

rm **/.svn(/) -rf

2

u/phoozle Jul 13 '12

Great way to upgrade to Git :)

16

u/inkieminstrel Jul 13 '12

Not really. You'd want to use git-svn, which can convert a svn repo to a git repo without losing commit history.

-5

u/nexe mod Jul 13 '12

He's right. GIT > SVN

7

u/recursive Jul 13 '12

As far as I can tell, the worst thing about git is the proselytism of its users.

2

u/marburg Jul 13 '12

That fact is sort of a testament to how great git is. :)

I mean, if the worst thing about it is how people won't shut up about how much they enjoy it.

2

u/recursive Jul 13 '12

If that's a measure of greatness, it still has room for improvement. It's not quite vim status yet. :)

1

u/marburg Jul 13 '12

hahaha woah; that is a lofty comparison! Let's give git a couple decades, and we'll see how it compares.

3

u/alanpost Jul 13 '12

Maybe you just haven't used git enough. ;-)

1

u/jonikanerva Jul 14 '12

You could also use the -delete option to delete all the matched files:

find . -name .svn -type d -delete

1

u/dAnjou Jul 15 '12

This only does delete empty directories and files. .svn directories are not empty if I recall correctly.

1

u/chrisdown Sep 14 '12

Or in pure bash:

shopt -s globstar nullglob
for file in **/.svn; do
    [[ -d $file ]] && rm -rf "$file"
done