Playing with xargs
Unix commands accept data either from the standard input (stdin
) or as command line arguments. Previous examples have shown how to pass data from one application's standard output to another's standard input with a pipe.
We can invoke applications that accept command-line arguments in other ways. The simplest is to use the back-tic symbol to run a command and use its output as a command line:
$ gcc `find '*.c'`
This solution works fine in many situations, but if there are a lot of files to be processed, you'll see the dreaded Argument list too long
error message. The xargs
program solves this problem.
The xargs
command reads a list of arguments from stdin
and executes a command using these arguments in the command line. The xargs
command can also convert any one-line or multiple-line text inputs into other formats, such as multiple lines (specified number of columns) or a single line, and vice versa.
Getting ready
The xargs
command should be the first command to appear after...