xargs my new friend
February 1st, 2007
Tonight, I was looking back at some old code and decided to do a little refactoring. To start I thought, it’d be nice to move some files around to make it a little easier to work with from the command line since the code had originally been written in an IDE. I’ve used xargs before for simple tasks adding new files to subversion:
svn st | grep ^? | xargs svn add
or removing unwanted files from subversion:
find . -name "expr" | xargs svn rm
But for my task I needed to use cp operation on a list of files. A little frustrated with myself for not knowing this by now I decided to google and finally learn. So, here’s what I learned tonight about xargs. It’s very easy to execute a command and use the result of that command anywhere in the expression executed via xargs.
find src/ -name "*.java" | xargs -I {} cp {} src/rhg/crawler/

Since you were calling find already, you could’ve just used -exec instead of xargs
i.e.
find src/ -name “*.java” -exec cp {} src/rhg/crawler/ \;
code is the poetry of computers. shine on…