Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. Credit: ezpick / Shutterstock 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 -ltotal 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 ca b c$ echo a b c | xargsa 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. $ xargsHello, 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 data123456789101112131415 Then the flattening of its contents with xargs: $ cat data | xargs1 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 -l5aqua azure black blue bronzebrown chocolate cyan dark blue goldgreen grey lime maroon navy blueolive orange pink purple redsalmon sİlver teal tomato turquoiseviolet 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 51 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 7576 77 78 79 8081 82 83 84 8586 87 88 89 9091 92 93 94 9596 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 ' ' '+'|bc615243342 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 ' ' '+'|bcecho 1 2 3echo 4 5 6echo 7 8 9echo 10 11 12echo 13 14 15615243342 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. Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe