Redbeard's Thoughts

Everything you didn't want to know about what I'm thinking

Stupid xargs trick

,

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.

Opera Mini vs iPhoneHarmony Prelude

Write a comment

New comments have been disabled for this post.

May 2013
S M T W T F S
April 2013June 2013
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31