The xargs command may be one that many Linux users don’t know, but it’s also one that can save a lot of time and trouble on the command line. It’s an unusual command with some very handy uses. It can build and execute commands and, once you see how xargs works, you’ll likely be impressed with how easy it is to use.
Put simply, the xargs command allows you to send the output of one command to some other command to be used as parameters. Think of it as creating an “execution pipeline”. For example, you could use xargs with the find command to specify what you want done with the files that are found.
[ Find more handy commands in NetworkWorld’s Linux command line cheat sheet ]It can also be used for doing a lot of other time-saving things on the command line that might surprise you. For example, you might want to find all files in a certain folder that end with .pl or .sh and make them executable. Or you might want to select a series of files and touch (update the timestamp on) them. These tasks are very easy to do with an xargs command appended to your find command. Here is a simple example:
$ find . -name ”*.pl” | xargs chmod 755
The command above finds perl scripts (files that end in .pl) and makes them executable. The next command sends a list of files to xargs which then uses the touch command to create them.
$ echo file1 file2 file3 | xargs touch
$ ls -l
total 0
-rw-r--r--. 1 shs shs 0 Oct 15 12:41 file1
-rw-r--r--. 1 shs shs 0 Oct 15 12:41 file2
-rw-r--r--. 1 shs shs 0 Oct 15 12:41 file3
In the command below, we’re looking for all C source code files that refer to the standard IO library, stdio.h
$ find . -name '*.c' | xargs grep 'stdio.h'
./libcaca-0.99.beta19/src/aafire.c:# include
./libcaca-0.99.beta19/src/aafire.c:#include
./libcaca-0.99.beta19/src/cacaclock.c:# include
./libcaca-0.99.beta19/src/cacaserver.c:#include
./libcaca-0.99.beta19/src/cacademo.c:# include
./libcaca-0.99.beta19/src/cacadraw.c:# include
These are all fairly common uses of xargs. At the same time, xargs has many other uses that make it considerably more versatile. Let’s take a closer look at how it works and what it can do.
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:
$ cat data | xargs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
You can also view your data by squeezing a given number of lines together. In this example, we’re displaying every 5 lines from the file as a single line.
$ cat colors | xargs -l5
aqua azure black blue bronze
brown chocolate cyan dark blue gold
green grey lime maroon navy blue
olive orange pink purple red
salmon sİlver teal tomato turquoise
violet wheat white yellow
This can work with numbers generated by brace expansion as well. In this example, we’re using the echo command and the range of values expressed in the braces to feed the numbers between 1 and 100 to xargs and then grouping them five at a time.
$ echo {1..100} | xargs -n 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
36 37 38 39 40
41 42 43 44 45
46 47 48 49 50
51 52 53 54 55
56 57 58 59 60
61 62 63 64 65
66 67 68 69 70
71 72 73 74 75
76 77 78 79 80
81 82 83 84 85
86 87 88 89 90
91 92 93 94 95
96 97 98 99 100
In this next command, we’re adding each set of three lines together from the data file together and then displaying the sums.
$ cat data | tr ' ' 'n'|xargs -l3|tr ' ' '+'|bc
6
15
24
33
42
To have xargs display what it’s doing, you can add the -t option as shown below.
$ cat data | tr ' ' 'n'|xargs -t -l3|tr ' ' '+'|bc
echo 1 2 3
echo 4 5 6
echo 7 8 9
echo 10 11 12
echo 13 14 15
6
15
24
33
42
Wrap-up
The xargs command can be handy for a number of data manipulation tasks. While it’s primarily used to construct new commands so that you don’t have to do that by hand, it also allows you to do some unusual rearrangements of data that might just come in handy.
Remember to like our facebook and our twitter @WindowsMode for a chance to win a free Surface Pro every month!
Discover more from Windows Mode
Subscribe to get the latest posts sent to your email.