Guy Rutenberg

Keeping track of what I do

Delete Unversioned Files Under SVN

with 27 comments

Sometimes svn switch fails because unversion files exist in the working copy, and the need to erase them comes up. This is also true when updating to an old revision (usually one that misses directories that exist in the head revision), doing some work (like compiling), and then trying to update back to the head revision. Subversion (SVN) will fail complaining that directories/files already exist.

The cause of the problem is that the working-copy isn’t clean, it has some (or a lot) of unversioned files. The best solution will be to delete all of them. While this can be a tedious task when done manually for each file, it can be achieved with single shell command.

svn status --no-ignore | grep '^\?' | sed 's/^\?      //'
svn status --no-ignore | grep '^\?' | sed 's/^\?      //'  | xargs -Ixx rm -rf xx

The first command will list all unversioned files and directories. Use this to review the files to be deleted. After you review the files, use the second command to delete them easily.

UPDATE 2010-12-29: Fixed the command, so it will handle spaces in filenames correctly.

Written by Guy

January 18th, 2008 at 1:20 pm

Posted in Linux,Tips

Tagged with

27 Responses to 'Delete Unversioned Files Under SVN'

Subscribe to comments with RSS or TrackBack to 'Delete Unversioned Files Under SVN'.

  1. Thanks!

    gilesc

    9 Feb 08 at 08:46

  2. jeez men, thanks !

    gibffe

    15 Aug 08 at 15:26

  3. sweet. you win one free internets

    DWC

    1 Apr 09 at 18:38

  4. Thanks man. Helped alot

    Ettiene

    4 May 09 at 15:06

  5. Good.. Thanks a lot :)

    sridharkannan

    24 Jun 09 at 17:14

  6. You can do a similar thing on Windows using PowerShell:

    (svn stat “–no-ignore”) -match ‘^[I?]‘ -replace ‘^.\s+’,” | rm

    Damian Powell

    4 Jul 09 at 15:47

  7. RE: Damian Powell

    Tried (svn stat “–no-ignore”) -match ‘^[I?]‘ -replace ‘^.\s+’,” | rm

    but it didn’t quite work for me

    (svn status –no-ignore) -match ‘^[?]‘ -replace ‘^.\s+’ | rm

    works fine for me though.

    Yumi Nanako

    14 Jul 09 at 08:58

  8. http://pastebin.ca/1494457

    *sighs* the blog removes some formatting, does it…

    Yumi Nanako

    14 Jul 09 at 11:18

  9. Yes it does, and unfortunately I still don’t know of a way around it in the comments that works perfectly. However in most occasions putting the code inside pre tags (and setting lang attribute to whatever you want) gives you preformatted syntax highlighted code. For example:

    (svn status --no-ignore) -match '[?]' -replace '^.\s+' | rm

    Guy

    16 Jul 09 at 06:56

  10. Don’t forget to escape those spaces!

    svn status --no-ignore | grep '^\?' | sed 's/^\?      //' | sed 's/ /\\ /g' | xargs rm -rf

    Ivan

    23 Jul 09 at 14:32

  11. You can do it with sed only:

    svn status –no-ignore | sed -n ‘s/^\?\s*\(.*\)/\1/p’ | xargs rm -rf

    DEBO Jurgen

    12 Nov 09 at 23:37

  12. To properly deal with spaces one has to do:

    svn status --no-ignore | grep '^\?' | sed 's/^\? //' | sed 's/\([^^]\) /\1\\ /g' | xargs rm

    in mac osx at least…

    joao

    25 Dec 09 at 19:09

  13. scratch my last comment, it’s Ivan’s way and should read


    svn status --no-ignore | grep '^\?' | sed 's/^\? //' | sed 's/ /\\ /g' | xargs rm

    or something like that… Jeez this isn’t easy…

    joao

    25 Dec 09 at 19:43

  14. How about just cutting the 8 first characters?

    svn stat --no-ignore | grep ^\? | cut -b 8- | xargs rm
    
    
    	

    Lionel

    1 Feb 10 at 10:51

  15. @Lionel, that indeed seems to be a very nice solution. But I think it should be like this

    svn stat --no-ignore | grep ^\? | cut -b 9- | xargs rm

    I’ve replaced the 8 with a 9 as you want to start printing from the 9th byte to the eol.

    Guy

    1 Feb 10 at 21:28

  16. why not just using awk?

    svn status –no-ignore|awk ‘/^\?/ {print $2}’|xargs rm

    tobi

    3 Mar 10 at 14:37

  17. awk is a great tool, but I lack comprehensive knowledge of it, thus I usually prefer other tools.

    Guy

    4 Mar 10 at 19:05

  18. Piping lines into xargs rm here *will* give you grief if you have spaces in your file names!

    This is safer:
    (svn status | grep ‘^[\?~]‘| cut -c 9- )| while read VICTIM; do
    rm -fr “$VICTIM”
    echo “Deleted $VICTIM”
    done

    Andy

    18 Jun 10 at 18:19

  19. [...] to Guy Rutenberg for this, your definitely in my list of top 5 [...]

  20. Great! Thanks a lot!

  21. Late to the party but… Andy, you’re right that spaces in filenames will cause problems if you pass them to xargs, but they’re not the only problematic character. The usual way to fix this is to use tr and the -0 flag on xargs, eg:

    ls|tr ‘\n’ ”|xargs -0 rm

    It’s what -0 is there for.

    Baz

    28 Oct 10 at 15:19

  22. Ugh, the blog mangled my command. The tr command was

    tr space single-quote backslash n single-quote space single-quote backslash zero single-quote

    Baz

    28 Oct 10 at 15:21

  23. Thanks,

    Worked out of the box…..

    Japan Shah

    6 Apr 11 at 13:11

  24. [...] Delete Unversioned Files Under SVN by Guy Rutenberg [...]

  25. thank u very much man

    Harsh kumar

    6 Sep 11 at 13:35

  26. Thank you very much for this. I’ve been doing it manually, one file at a time for a very long time now and it only just occurred to me to search for an automated solution.

    Seriously, you have saved me so much time. Thank you.

    Josh Greco

    25 Sep 12 at 17:00

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>