Recursively Delete .svn Directories

By  on  

Version control systems are hugely important when it comes to development of all kinds.  I'm a huge fan of git but sometimes I'm required to use SVN.  Unfortunately I tend to forget to remove .svn folders when I copy and paste from a directory structure template.  This, of course, leads to me overwriting files and results in mass confusion.  I recently stumbled up an awesome command line script which recursively removes .svn directories from parent directories.

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

The script starts within the current directory, so be sure you navigate to the directory you want to start searching from first. This script would work on any search type; simply replace ".svn" with the search of your choosing but be careful:  if your search term is too general, you may end up deleting a bunch of stuff you don't want to!

Recent Features

  • By
    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

  • By
    How to Create a Twitter Card

    One of my favorite social APIs was the Open Graph API adopted by Facebook.  Adding just a few META tags to each page allowed links to my article to be styled and presented the way I wanted them to, giving me a bit of control...

Incredible Demos

  • By
    Element Position Swapping Using MooTools 1.2

    We all know that MooTools 1.2 can do some pretty awesome animations. What if we want to quickly make two element swap positions without a lot of fuss? Now you can by implementing a MooTools swap() method. MooTools 1.2 Implementation MooTools 1.2 Usage To call the swap...

  • By
    Introducing MooTools Dotter

    It's best practice to provide an indicator of some sort when performing an AJAX request or processing that takes place in the background. Since the dawn of AJAX, we've been using colorful spinners and imagery as indicators. While I enjoy those images, I am...

Discussion

  1. Buri

    IMHO its better to use

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

    since it saves system resources. And IFAIK all todays bash support + operator.

    • Toni Lähdekorpi

      Does bash really matter here? I thought the + operator was just an option of the latest find versions.

  2. Nick G

    Here’s a registry file to add the same to a windows folder right-click that comes in handy now and then;

    (Code highlight that! lol)


    Windows Registry Editor Version 5.00

    [HKEY_CLASSES_ROOT\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\" \""

  3. goyote

    What does {} \; do?

    • The ‘{}’ references the file being acted upon by ‘-exec’ and ‘\;’ terminates the exec command

  4. Will

    You might also consider using the svn export command; it was made for this.

    • Matthew F.

      What I came to say – I always copy files from my repository using:

      svn export –force RepositoryURL /path/to/target

      (–force tells it to continue even if it is overwriting files – e.g. if you’re exporting the latest revision to a directory where the files already exist)

      That also guarantees that only version controlled files get exported – for example, if you have a file with sensitive data in your development directory that is not under version control, you won’t accidentally make it world readable. Don’t have to do that more than once before you get very careful about what you publish :(

    • I have an old backup of a bunch of SVN repos, and the source repos no longer exist, but I have backups I would like to commit into an new GIT repo, so I need to cleanse them of the .svn folders that were created when I checked the code out the first time.

  5. @goyote, {} is a placeholder for the argument to the -exec clause. The main find , obviously enough , just finds the files, and plugs them in to the -exec clause where it finds the {}.

    For more refer to the man page ;) (man 1 find). Or on the web @ http://unixhelp.ed.ac.uk/CGI/man-cgi?find

  6. Funnily enough I posted the same sort of article a week ago. From my own testing I discovered adding the -depth flag was useful for preventing error messages like “find: ./.svn: No such file or directory”

    @Will – yes it is useful using svn export to get a clean tree but it is potentially possible that a) the svn repository is no longer available or b) you want to ensure you don’t lose any files that are not actually in svn.

  7. Beau

    I’ve always used this and it works great, but always test before executing the rm command.


    find . | grep .svn | xargs rm -rf

  8. eclipxe

    You better use -delete parameter of find instead of -exec rm -rf {} \;
    like:

    find -type d -name .svn -delete

    check the man page of find command

  9. Also sounds like you should import from subversion to git.
    http://help.github.com/svn-importing/

    • If only I could use git at work… .. ..

  10. craig

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

    • dror

      This find... approach has the deficiency that if you have many folders, it may become too large for the command line. Using xargs is an option but what eclipxe proposed: ‘find -type d -name .svn -delete’ is simple, short and effective.

  11. Sheldon

    I have always used this; rm -rf `find . -name .svn`

    http://sheldon.lendrum.co.nz/removing-svn-directories-recursively_386/04/

  12. wallace

    I’d be inclined to enclose .svn in quotes (or at least escape with \.svn), as the . will match any character (regex). If you’re random enough to have a directory called nsvn, it would also be deleted.

  13. On win7 I just tend to type *.svn in the explorer’s search box and after that ctrl+A => delete :)

  14. It can be useful to remove sass-cache folders before an initial commit to svn, thanks.

    rm -rfv find . -type d -name .sass-cache

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!