Wednesday, November 18, 2009

xargs xargs xargs.. ??

I was learning about how to use xargs today.. quite an interesting unix tool. What it lets you do is to trigger a command on each input simple you received from input stream stdin. for example:

ls -l | awk '/.*/ { print $8}' | xargs -n1 -I{} gcc -o {}.o -g {}

long list the current directory with detail, take the last column with awk which contains a list of names, take each name and execute "gcc -o .o -g on them

xargs can print the command that is being generated using the -t option, or -p option for you to confirm each command that will be executed on the file. The -I{} indicates a special symbol {} is going to be used for the location where the symbol name should be put into instead of the default at the end of the command. the -n indicate how many symbols you will be using for each command, -l indicates how many lines of symbols you will be using for each command

xargs called without any command will act the same as an echo command.

It is essentially an easy replacement for for loops in a shell script

No comments: