Add all new files and folders to subversion with one line in terminal

This is basicly simple oneliner:
svn status | grep "^?" | awk '{print $2}' | xargs svn add
I'll try to explain it...

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

What this line uses really well are "pipes", that's these "|" signs. Simply put you have, in this example, 4 commands and each one receives input from previous one (except first one, of course) and does something with it.

First one, svn status, prints out status of the working copy files and directories.

But we're looking only for files we created that are not added to SVN, and those files/folders have "?" as a status symbol, and that's why we're looking for that question mark in second command grep "^?" and because of that we'll get only lines of the output with question mark at the beginning.
Grep really comes handy for many things, if you're listing files in terminal and you're looking for those that contain "document" in their filenames you can simply write: ls | grep document

But in the output we have both status symbol and file/folder name. And we need only filename. And this is where AWK comes handy awk '{print $2}' . It takes only the second part of the line and prints it out (in case where you want to print the first part, for example, you would use $1 instead of $2). And it send the second part, which is filename (folder name) to our next command...

And our last command simply adds the file to subversion: xargs svn add

And this is the definition of "xargs" from Wikipedia: "xargs is a command on Unix and most Unix-like operating systems. It is useful when one wants to pass a large number of arguments to a command. Until Linux kernel 2.6.23, arbitrarily long lists of parameters could not be passed to a command [1], so xargs will break the list of arguments into sublists small enough to be acceptable."

P.S. In case you were wondering that's Mario & Luigi up there on the picture

Comments

Wonderful! Thank you. I will use this for sure!

Ales 15. Dec 2009 at 2:58 PM

Great way to make this happen on a single line. Works like a charm on Snow Leopard.

Ain 21. Feb 2010 at 7:23 PM

Great! However, this doesn't work if your file names contain spaces.
I managed to compile several snippets from the net and came up with this:
svn status | grep "^?" | awk '{$1="";$0=substr($0,2)}1' | sed -e "s,[^.],\'&," -e "s,\$,\'," | xargs svn add

Phew! Works in Snow Leopard.

karol 24. Feb 2010 at 12:14 PM

Thank for this one-liner!

kozak 18. Mar 2011 at 5:07 PM

karol, thanks for the modified version. helped me so many times already!

Zenvied 21. Mar 2011 at 5:27 PM

Or possibly

svn status | grep "^?" | sed s/^?// | xargs svn add

Kevin Bailey 13. Jul 2011 at 5:24 PM

It'll break when filenames contain a '@'

Karol's modified oneliner to take care of that:

svn status | grep "^?" | awk '{$1="";$0=substr($0,2)}1' | sed -e "s,[^.],\'&," -e "s,\$,\@'," | xargs svn add

elmimmo 27. Jul 2011 at 11:40 AM

Cheers, this is exactly what I was looking for. (Although I was going to opposite way — deleting a huge batch of folders from SVN).

The more Unix I learn, the more I like it.

Dave 8. May 2012 at 12:00 PM

This looked like fun, but you can always just "svn add --force ." from the root of the repo.

Pauldy 18. Sep 2012 at 3:08 AM

Excellent! It's working perfectly.

Thanks!

Mathieu 4. Jul 2016 at 9:51 PM

Leave a comment

*
Leave your e-mail if you would like to get reply from me. Your e-mail will not be shown on my web page nor will I use it for anything other than replying to your question/comment.