Stupid xargs trick
Sunday, June 24, 2007 5:09:00 AM
Here's a simple tip for using xargs to replace for loops on the commandline.
The other day I needed to rename a set of files to a set of names that didn't necessarily work well programatically. While there are tools to do this (vidir comes to mind) I didn't have any available.
So I created a simple file to math things up:
sourcefile1 destfile-a sourcefile3 destfile-b sourcefile2 destfile-c
Then I ran this command:
cat movelist | xargs -n 2 mv
and all the files were renamed.
The trick is the -n 2 parameter to xargs. It passes words to mv two whitespace-delimited words at a time. You can do more complicated things like while loops with the read command, but that would have been overkill for this one-off solution.
To see another use of this, see my other blog.







