How to use the xargs command in Linux?

Filed Under: UNIX/Linux
Xargs Command

The xargs command allows us to pass the output of one command as the input for another command without having to write the output in a file.

Some tasks that may require the use of multiple commands that cross-reference outputs from other commands will be greatly helped with the use of xargs. Let’s have a look at understanding the command and some of the uses of the command today.

What is xargs?

Xargs allows you to pass outputs as input to another command. While a command like grep allows the user to use inputs as parameters, xargs shows its importance by allowing the input to be in the form of arguments.

This command is almost always used in combination with other commands with the help of piping. Let’s understand the basic usage of the command.

Basics of xargs Command in Linux

The best way to understand any command is through understanding it’s syntax. Here is how the syntax for the xargs command in Linux looks like.

xargs [option] [command]

Here, the [command] can be any command(s) which we wish to execute on our standard input. If we don’t specify the commands, the xargs utility uses the default command.

The default command for the xargs utility is ‘echo’. 

The xargs utility allows several options to dictate how commands are built and executed. Here is a list of some of the commonly used options offered by the xargs utility in Linux.

OptionEffect
-0This option is used to specify to the xargs utility that the items in the standard input string are terminated using a NULL character instead of a white space.
-a filenameRead from file
-rDon’t run if the input is empty
-d delimSpecify a delimiter
-xExit if the input size specified by -s is exceeded.
-I replace-strThis is used to declare a string as replace-str. Then all occurrences of replace-str are swapped by the parameter or argument from the standard input.
–helpThis option is used to display the help message for the xargs utility. This includes all the possible options which can be used with the xargs utility.

While this list covered the basic options used in the xargs utility. Don’t forget to explore the man page for xargs.

Using the xargs command

Now we have developed an understanding of the xargs command in Linux and its parameters. It’s time to use this knowledge for the application of the xargs utility. For this tutorial, we will go over some examples to learn how to use the xargs utility.

The xargs Default Behavior

As we mentioned earlier, the xargs command will run the echo command by default for the input arguments if no command is specified after it.

Let’s see that in action to verify our claim.

ls -ls | xargs
Xargs Default
Xargs Default

As you can see in the above screenshot, the output for the ls command gets printed out without the line breaks and formatting when passed through the echo and the xargs command.

Create Multiple Directories Using a File with List of Names

If you have the list of names in a single line, you can copy and paste the directory names separated by spaces and mkdir command will create the directories.

But what if we have a file with the list of all the names of directories we want to create? Let’s see how we can create multiple directories from a list of directory names.

cat <filename> | xargs mkdir
Xargs Pipe Output
Xargs Pipe Output

As you can see in the above screenshot, we tried to directly pipe the output of the cat command to mkdir and failed. But with the addition of xargs, we were able to execute the command and successfully create the directories.

Find Files Containing a Specific String

Grep can search for strings within files or output messages. But it cannot search for files. The find command can search for files, but not the contents within. The xargs command can help connect both of these together.

find <search directory> -name "<name>" | xargs grep -E 'fatal|Warning'
Find Xargs
Find Xargs

We searched for the file “syslog” in the /var/log directory and looked for lines containing the words “fatal” or “Warning”. Since we knew the file we want in this case, we could have directly passed it on to grep. But if we did not know the file path, the find command can be used to find the file and pass the paths to grep.

Delete Files Having Specific Extensions

Like creating and identifying files and directories, the xargs utility is also helpful in getting rid of files from your system. For this example, we will delete all the text files present in our current directory.

find <path> -name <filename> | xargs rm -rf
Delete Files Xargs
Delete Files Xargs

Here, the find command first searches for files that have the .txt extension. This output is then sent to the rm command.

This command takes the files from the find command and deletes them. The output of the successful execution of this command should give and output similar to the one given below.

Conclusion

The xargs command in Linux is a powerful tool to build and execute commands using a standard input through your command line. It makes your command line usage efficient by allowing you to use the output of one command in place of an argument in another command.

This command-line utility is widely used by both beginner and experienced Linux users regularly.

We hope this tutorial was able to help you understand the xargs command in Linux. We discussed only the basic usage of the xargs utility in this tutorial, so make sure to explore the command on your own.

If you have any feedback, queries or suggestions, feel free to reach out to us in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages