Lesser-known xargs command is a versatile time saver

First, the xarg command’s basic function is pretty simple. It echoes what you toss at it. In a sense, it’s not very different from echo, except that content needs to be redirected to it, not presented as an argument.

$ echo a b c
a b c
$ echo a b c | xargs
a b c

Obviously, nothing is gained in the example above. The pipe and the use of xargs are just making the command more complex and a bit more time-consuming. But the command does illustrate how xargs takes each string that is passed to it and repeats it.

In the example below, we just type xargs and then hit enter. Then we type some text. I typed control-d after the text and pressing the enter key. You won’t see the control-d (^d), of course, but I included it below to show where it was entered. The xargs command displays the text again. You could enter as many lines of text as you cared to and all would be repeated after the control-d.

$ xargs
Hello, World!
Goodbye for now.
^dHello, World! Goodbye for now.

In a similar fashion, if you need to have the contents of a simple file show up on a single line, basically flattening the file, xargs will comply with your wishes.

First, the file:

$ cat data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Then the flattening of its contents with xargs:



Source link