What is Xargs and How to Use it (with 13 Examples)

Linux TLDR
Last Updated:
Reading time: 4 minutes

Xargs is a fantastic command-line tool used to read data streams from the standard input of one command and pass them as arguments to another command when combined via piping. You can seamlessly pair it with various other commands, such as find, rm, cp, mkdir, tar, etc.

If you provide no input, xargs will default to acting as an echo. However, it’s not intended to work as an echo; instead, it’s often used to take input from one command and convert it into an argument for another command.

In this beginner’s guide, I’ll show you how to use xargs with other commands, including its options.

How to Use Xargs Command in Linux

Xargs is a built-in tool, requiring no installation. It accepts two arguments: one as the option and the other as the argument, both of which are optional.

$ xargs [OPTIONS] [ARGS]

The argument could be anything from a path to a command; however, the crucial part is the option that allows you to modify the behavior of xargs. Below are some important options that you can use with xargs.

OptionDescription
-aReads a file’s contents.
-tPrint the resulting command before execution.
-pAsk for approval before executing commands that you undoubtedly use when performing critical tasks like file removal.
-nControl the number of arguments xargs can accept at the same time.
-dAllows you to change the default (space) delimiter.

Let’s now see how we can use the xargs command in combination with other commands, including its options.

1. Archive Files to Tar Format Using Xargs

Let’s say you have multiple text files in your document directory mixed with other files, and you want to archive them. For that purpose, you can use the find command to specify the path of the text file directory, then use β€œ-name” for specifying the extension, β€œ-type” for specifying type (which is β€œf” for file), and β€œ-print0” to include files with or without a space in the filename.

It will list all the text files that you can forward to the β€œxargs” command and use β€œ-o tar -cvzf filename” to create an archive file from the forwarded text files in β€œtar” format.

$ find ~/Documents/ -name "*.txt" -type f | xargs -0 tar -cvzf textfile.tar.gz

Output:

find text file and archive using tar

2. Delete File With Specific File Type Using Xargs

Once more, consider the earlier scenario: a text file mixed in with various files in the document directory. Now, instead of archiving, you aim to delete them. Thus, the find command remains unchanged, but now you use the β€œrm -f” command along with xargs to delete the redirected files.

$ find ~/Documents/ -name "*.txt" -type f | xargs rm -f

Output:

deleting files using xargs command

3. Print the Resulting Command Before Execution

To preview the command syntax before execution, you can use the β€œ-t” option alongside xargs; this could be crucial when performing file manipulation tasks like creating, renaming, or deleting a file.

For instance, the following command is identical to the previous one, but previously, it produced no output; however, with the β€œ-t” option, it will display the output of the command that is about to be executed.

find ~/Documents/ -name "*.txt" -type f | xargs -t rm -f

Output:

printing command syntax before execution

4. Approve Xargs Command Execution

When performing file manipulation tasks, especially unavoidable ones like deleting files, it is recommended to always use the β€œ-p” option, which prompts for confirmation before executing the command.

$ find ~/Documents/ -name "*.txt" -type f | xargs -p rm -f

Output:

approving xargs command execution

5. Find a String From a Text File Using Xargs

You can use the grep command to search for a specific string within a group of text files located by the find command and redirected by the xargs command.

$ find ~/Documents/ -name "*.txt" -type f | xargs grep "LinuxTLDR'

Output:

searching specific string from files using xargs

6. List All User Accounts in One Line

The multi-line input redirected to the xargs command is transformed into a single line. Therefore, when reading a file like β€œ/etc/passwdβ€œ, you can use the β€œcut” command to extract usernames from the first column and generate a compact list of all Linux users on your system using xargs.

$ cut -d: -f1 < /etc/passwd | sort | xargs

Output:

find list of linux users

7. List the Number of Lines, Words, or Characters in Each File

To determine the number of lines, words, or characters in files stored somewhere in your system, use the ls command to list files and pipe them to xargs, which redirects the data to the wc command.

$ ls ~/Documents/*.txt | xargs wc

Output:

Count Number of Lines, Words and Characters in Files

8. Read the Content of File

The xargs β€œ-a” option, when specified with the path of a file, will print the file’s content.

$ xargs -a ~/Documents/file_2.txt

Output:

reading the content of text file

9. Remove Extra Spaces from the Output

By default, xargs eliminates unnecessary blank spaces, so if you have a regular or config file filled with blank spaces, you can use xargs to get clean output.

$ echo "[string-with-unnecessary-spaces]" | xargs

Output:

removing blank spaces from text file

10. Specify the Custom Delimiter

The default delimiter for xargs is a space, but it can be changed to something else by using the β€œ-d” option followed by the new delimiter.

$ echo "file1*file2*file3" | xargs -d* | xargs touch

Output:

replacing xargs default delimiter

11. Print the Output in Single Line

As previously mentioned, xargs takes a redirected input and converts it into a single line; however, this default behavior might not be needed, and you may want to print each item on a new line; for that purpose, you can use the β€œ-n” option followed by the number of lines as an argument.

$ echo "1 2 3 4 5 6 7 8 9" | xargs -n 1

Output:

print each result in new line

In method four, asking for approval before deleting the file was prompted only once for all files because the results were combined, but by combining that with this method, you can prompt approval for each file individually.

12. Copy Files into Multiple Directories At Once

Let’s say you have a file in your home directory that you want to copy to both your desktop and documents directory; instead of doing this individually, you can use xargs to copy the file to multiple directories at once.

$ echo ~/Desktop/ ~/Documents/ | xargs -n 1 cp -v myfile.txt

Output:

copying files into multiple directories

13. Run Multiple Chained Commands form Single Input data

Assume you have a text file containing a list of directory names that you want to create. Using xargs, you can forward the text file output to the echo command to print the text content, and then forward the same data to the mkdir command to create directories.

$ cat myfile.txt | xargs -I % sh -c 'echo %; mkdir %'

Output:

passing single command input to two command using xargs

Final Word

This article has come to an end. Xargs is an amazing tool that is frequently used in installation commands or when working with piped commands. In this article, you learned about its various use cases. If you have any thoughts to share, please let us know in the comments section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.