How to Use the xargs Command in Linux?

In Linux, the terminal acts as the default command-line interpreter, which executes the command arguments to perform the desired function. In the xargs command, the output of the first command is taken as an argument for the next command/pattern.

Let’s discuss the uses of the xargs command by performing examples with the following timeline:

What is the xargs Command in Linux?

The xargs (execute arguments) command builds and executes the commands from the previous output. It converts the output of one command to arguments, which is used as input to another command.

The xargs command has the following syntax:

$ xargs [options] [command [arguments]]

The syntax components are described below:

  • xargs: It represents the command that will execute the arguments (xargs).
  • options: Set an option to perform a specific task.
  • command: Enter the command which will take the xargs as input. 

The xargs command offers the following options:

-aRead arguments from the file.
-0 –nullThe option is used to terminate the input.
-dIt shows a delimiter like Space ( ) or Comma (,).
-EEnd of line and ignores the next input lines.
-sSpecifies the length of the command.
-tPrint command before executing.

How to Use the xargs Command in Linux?

This section will use the xargs command for performing different functions and usage of the xargs command with other commands.

Let’s start with a basic overview of the xargs command.

Example 1: Use xargs Command to Enter Input

The xargs command default behavior is to output/display the input command. When you enter the “xargs” command in the terminal, an input text editor is initiated. After completing the required input text, press the “Ctrl + D” shortcut key to tell the input editor it’s an end of input:

$ xargs

Once you press the “Ctrl + D” shortcut key, the input text will be displayed as output.

The above image shows that “This is sample input text” was entered as input to the xargs command, which converted it to arguments and was given as the input to the standard output (stdout).

Example 2: Use xargs to Display the Content of the File

The “a” option available within the xargs command enables the user to read the file’s content. For instance, to read the “testfile2.txt” file, utilize the following command:

$ xargs -a testfile2.txt

The output shows “testfile2.txt” content.

Example 3: Use xargs Command to Create Files

The xargs command takes the input of the previously executed command output. Similarly, to create files “testfile1.txt” and “testfile2.txt”, output their name first using the “echo” command, and those names will be given as input to the xargs command, which performs the “touch” operation (creates files):

Note:ls” is used to verify the creation of the two files.

$ echo “file1.txt file2.txt” | xargs -t touch

Example 4: Use xargs Command to Replace One File With Another

Sometimes, the user needs to replace one file with the other by copying its content to that file. To replace a file named “testfile5.txt” with the “testfile4.txt” while copying the content of that file, use the xargs command as shown below:

$ echo testfile4.txt | xargs -p mv testfile5.txt 
$ ls 
$ cat testfile4.txt

The output shows the following things:

  • The testfile5.txt is replaced with testfile4.txt.
  • The “ls” command verifies that the “testfile5.txt” does not exist after executing the command.
  • The “cat” command shows the replaced (deleted) file “testfile5.txt” content is copied to the “testfile4.txt”.

Example 5: Use the xargs Command to Get Word Count

xargs counts the words in a specific file or multiple files. To count the words from a file named “testfile4.txt” using xargs and wc (words count), use the below command:

$ ls testfile4.txt | xargs wc

For finding the words count for files having a specific word “test” in their name, use the following command:

$ ls *test*  | xargs wc

Example 6: Use xargs Command to Limit Words Per Line

The “n” option of the xargs command limits the words per line. For instance, the text can be limited to specific words per line (in this case, 4); use the following command:

$ echo "This is a new sample text for a file" | xargs -n 4

The output shows that a maximum of four (4) words can be printed on a single line.

Example 7: Use the xargs Command to Specify the Delimiter

The “d” option allows the xargs command to check for a delimiter like a bracket(), Comma (,), Dot (.), or any symbols. For example, to check the three files “testfile1.txt”, “testfile2.txt”, and “testfile3.txt” by combining with a dash () delimiter and then display the content of those files using the “cat” command, use the below syntax:  

$ echo "testfile1.txt-testfile2.txt-testfile3.txt" | xargs -d- | xargs cat

Example 8: Use File Text as Input to xargs Command

The first command’s output can be used as input for another command using xargs. To get the list of all files, use the “ls” command and use it as input for the next command, “cat”, which displays the content as follows:

$ cat testfile1.txt
$ cat testfile1.txt | xargs ls

Example 9: Use xargs Command to Move Specific Files

We can use the xargs command with the “find” command to check a specific file or file type in the system and move the files to the desired directory. For instance, to find the “mp4” files in the system and move them to the “VideosFolder”, use this command:

$ find . -name '*.mp4' -type f | xargs mv -t VideosFolder 
$ ls VideosFolder

The output shows the “.mp4” files are moved to VideosFolder.

Example 10: Use xargs Command to Copy Specific Files

Similarly, copy the “.mp4” files to the “TestFolder” directory by running the below xargs command:

$ find . -name '*.mp4' -type f | xargs mv -t VideosFolder

Example 11: Use xargs Command to Find txt Files having a Specific Word

If you want to search for a word in a file with a particular type, the find can be used with the xargs command. For instance, to find a “pdf” file having the word “sample” in that file, use the below command:

$ find . -name '*.pdf' | xargs grep 'sample'

The output shows two “pdf” files (testfile1.pdf and testfile3.pdf) with sample words.


The xargs command converts the standard input to arguments and executes it as commands on the output. This command performs several functions, like showing content and creating and replacing files. Moreover, we can use this command with other commands to build and execute based on previous output, as discussed in this article.