13 December 2009
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!
Great way to make this happen on a single line. Works like a charm on Snow Leopard.
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.
Thank for this one-liner!
karol, thanks for the modified version. helped me so many times already!
Or possibly
svn status | grep "^?" | sed s/^?// | xargs svn add
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
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.
This looked like fun, but you can always just "svn add --force ." from the root of the repo.
Excellent! It's working perfectly.
Thanks!