xargs tip of the day

I use xargs a lot especially with find. Many times I want to pass the arguments as parameters to the command I’m trying to run with xargs usually somewhere in the middle. Today I found it 🙂

Time for an arbitrary example, this is completely made up to illustrate the point so don’t bother with “why are you using xargs for that comments”. 🙂 So you want to find .png files that are different in /foobar/tmp and /tmp/.

cd /foobar/tmp; find . -name '*.png' | xargs -n 1 -I imagename diff imagename /tmp/imagename

The -I argument tells xargs to replace the string that follows -I in the command arguments with the values from standard input. This is totally cool, and beats the old way I did things which was to write a simple shell script and call that from xargs 🙂

There are many ways to do what I did above, but hopefully folks find this xargs tip useful.


2 thoughts on “xargs tip of the day

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s