Thursday, June 14, 2007

xargs -- construct and execute command lines

xargs -- construct and execute command lines:

"xargs is a command of the Unix and most Unix-like operating systems which eases passing command output to another command as command line arguments.

It splits its often piped input at whitespaces (or the null character) and calls the command given as an argument with each element of the split input as parameter. If the parameter list is too long, it calls the program as often as necessary. It often covers the same functionality as the backquote feature of many shells, but is more flexible and often also safer, especially if there are blanks or special characters in the input."

Example: ls | xargs -t -i {} mv {} {}.old
In this simple example, xargs replaces {} with the filenames passed from the ls command, then executes to mv command to rename these files with a new extension.

No comments: