Playing with xargs
We use pipes to redirect stdout
(standard output) of a command to stdin
(standard input) of another command. For example:
cat foo.txt | grep "test"
Some of the commands accept data as command-line arguments rather than a data stream through stdin
(standard input). In that case, we cannot use pipes to supply data through command-line arguments.
We should try alternate methods. xargs
is a command that is very helpful in handling standard input data to the command-line argument conversions. It can manipulate stdin
and convert to command-line arguments for the specified command. Also, xargs
can 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.
All Bash users love one-liner commands, which are command sequences that are joined by using the pipe operator, but do not use the semicolon terminator (;
) between the commands used. Crafting one-line commands makes tasks more efficient...