Omrežje
  • 0
  • 1
  • 2
  • 3

Archives | Blog (December-2009-13)

Back to: Blog
Sunday, 13. december 2009.

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

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

Comment

Fresh One

Web

Omrežje

  • 4.00
    • 1
    • 2
    • 3
    • 4
    • 5
(current rating: 4.00 | 2 votes)
Calendar

January 2010

  • mon
  • tue
  • wed
  • thu
  • fri
  • sat
  • sun
  •  
  •  
  •  
  •  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

December 2009

  • mon
  • tue
  • wed
  • thu
  • fri
  • sat
  • sun
  •  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

May 2009

  • mon
  • tue
  • wed
  • thu
  • fri
  • sat
  • sun
  •  
  •  
  •  
  •  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31