Quick SVN script for automatically adding and deleting files

Here’s a quick bash script for automatically adding and deleting files in subversion. Don’t you hate having to list each one individually? Or getting all those messages that say, “the file has already been added” when you just do svn add my/directory/*. Even more of a pain if you selectively delete a bunch of files.

I use this small little script. It’s a huge time saver.


svn status |grep '^?' |sed 's/^?      /svn add "/g' |sed 's/$/"/g' |sh
svn status |grep '^!' |sed 's/^!      /svn delete "/g' |sed 's/$/"/g' |sh

It’s especially handy for adding and deleting changes made to the Drupal files directory.


Also doing ‘svn add –force /your-directory/*’ will do the trick…


The second pipe for sed invocation is superfluous. You can do all the edits in a single sed invocation: use the ‘-e’ flag. In your example, would be:

svn status | grep '^?' | sed -e 's/^?      /svn add "/g' -e 's/$/"/g' | sh
svn status | grep '^!' | sed -e 's/^!      /svn delete "/g' -e 's/$/"/g' | sh
The second pipe for sed invocation is superfluous. You can do all the edits in a single sed invocation: use the ‘-e’ flag.

Thanks! I knew it was possible, but I never got around to figuring out what the correct syntax would be to do that. Hopefully now I’ll remember :)

Sed is an extremely powerful tool, and as such, the documentation is somewhat enormous.

you are making life way too hard for yourself, just try this one :D

svn add svn status .|grep "^?"|awk '{print $2}'

I used it for developing the worlds biggest metal tshirt gallery


this site doesnt like forward tick’s (shift ~) being posted..

svn add fticksvn status .|grep “^?”|awk ‘{print $2}’ftick

Hmm… maybe it’s getting cleaned. Putting it in code could work. Is this what you were intending?

svn add `svn status .|grep "^?"|awk '{print $2}'`

Thanks for the heads up, though :) I enjoy seeing methods optimized.

Thanks for this snippet! Pretty handy. In fact, I turned it into a function (put it in your .bashrc or .bash_profile) which allows me to perform a variety of svn functions based on extended grep (egrep) filtering.

# Modified from
# http://ceardach.com/
  if test -z "$2"; then
    svn status | egrep "${1}" | awk '{print $2}'
    svn ${2} `svn status | egrep "${1}" | awk '{print $2}'`

The above code allows you to do some cool stuff.

svngrep '^\?.*filename'

Will give you your filtered list of files from your expression.

Adding the svn operation as a second parameter will apply to each filtered file.

svngrep '^!.*filename' 'rm --force'

The above would force remove any file that is in the repository (but not in your local) which includes the name ‘filename’.

Thanks for the condensed code!

I’ve also provide a video about this cool little bit of code and given you credit.



I couldn’t find any useful scripts, probably because I didn’t come up with the term “svn filtering”. That’s why I wrote my own script that does the same thing, but completely automated. You won’t have to pass any secondary commands anymore. I think that in most use cases, that’s desired. The script you use allows for more power of course :)

You can find it at http://wimleers.com/blog/syncvsvn-synchronize-cvs-working-copy-with-svn-working-copy.

(I posted this same comment over at Matt Petrowsky’s gotdrupal.com, I ended up here through his video on this script.)