How to Use the xargs Command in Linux?

The xargs command in Linux accepts inputs from the standard(stdin) and as a command-line argument. However, other commands are designed to take argument inputs. So, to process these arguments, Linux commands require the Xargs Command. 

In this guide, you will learn how to utilize the Linux Xargs Command to accept the standard input and how these codes work with the other commands. Let’s get started!

What is the Linux xargs Command?

Xargs Command is used to build and execute the standard input in the Linux operating system.  Basically, it takes the inputs and changes its arguments for other commands. This interpretation feature is generally used to manage file management. Xargs Command may/may not be used independently but combined with rm, cp, mkdir, and other similar commands.

In simple terms, Linux Xargs Command takes the passage input. Although it can accept inputs from the files and take them as parameters for the commands you want to run with. If you do not give commands to work, it automatically executes with the echo command in Linux.

Ways to Use the xargs Command in Linux With Examples

Ways to Use the xargs Command in Linux With Examples

When Xargs Command is used on Linux, it usually prompts the popup message on the user’s terminal window to enter the text string command to pass the echo command.

This example takes the input from the Echo Command Output.

#1. Combine Xargs with Find

The Command “Find” often processes Xargs Multiple Commands in a pipeline. You can use this command to get an ample range of lists of files for further precedence. 

Syntax of Find Commands:

find [location] -name “[search-term]” -type f | xargs [command]

The above example explains the use of Find Command to identify all files present in your system with the .sh extension. Once you get the listed file, then it should be piped with the Xargs Commands. This command uses the “rm” command with itself to delete them. The point to be kept in mind is that the Xargs command doesn’t consider the files having spaces in their names. You need to use other options or commands to evaluate those spaced files. For that, you should use

 -printO option to find files

-O option for Xargs

So, the syntax become looks alike:

find [location] -name “[search-term]” -type f -print0 | xargs -0 [command]

Note: “Rm” delete all files with the extension “.sh

#2. Combine Xargs with Grep

To find the String in the list of files, use Xargs Command with Grep Command.

A syntax that you should follow to execute the programs be like

find . -name ‘[search-term]’ | xargs grep ‘[string-to-find-in-files]’

In the example above, all your files with the .txt extension are searched and piped to xargs. So, it executes the grep command.

#3. Xargs Multiple Commands

To execute and run Xargs Multiple commands, you need to use the  -I option. 

Syntax of -I option with Xargs Command be like:

[command-providing-input] | xargs -I % sh -c ‘[command-1] %; [command-2] %’

When you execute the “-I” option with the Xargs command, the files named with file4.txt were displayed first. Then, the mkdir option automatically creates the files in a folder for each word.

#4. Read Items from File

As we mentioned earlier, the Xargs command reads the standard input. So, to read the contents of a file on the Linux operating system, use the “-a” option.

The syntax be like:

xargs -a [filename]

#5. Find and archive images using tar

Tar Command is used with the Xargs to create tar.gz archive images and use it with the command files.


find [location] -name “[search-term]” -type f -print0 | xargs -0 tar -cvzf [tar-gz-archive-name]

#6. Print Command

To view the standard output executed by the Xargs command, use”-t” option.


[command-providing-input] | xargs -t [command]

In this output, the xargs command executes the other command “mkdir” from the list of entire strings you have by the echo. 

#7. Approve Xargs Command with Execution

Some operations listed in the xargs command are invariable. You can’t change them. Such as removing files & folders etc. 

To prevent the execution of these commands, use the -p option.


[command-providing-input] | xargs -p [command]w

Once you execute the -p option with the xargs command in Linux, you’ll get a confirmation line on your terminal window screen. To proceed with the command, type “y” or “n” for the cancellation. 

#8. Limit Output with Line

Sometimes, it is crucial to prevent your operations from the multiple arguments that a xargs command takes simultaneously. 

To perform this function, use an option “-n “followed by the multiple arguments limited by xargs commands. 


[command-providing-input] | xargs –n [number] [command]

With this example, you can see a xargs command fetching the echo command string and breaking it into more than two parts.  A separate echo command is executed for each part.

With the Linux operating system, you can render the way to flush or free the Ram Cache if any process eats away your system’s memory unnecessarily. Learn how to Free or Clear Cache in Linux, Buffer, and Swap Space.

#9. Specify the Delimiter

Default Xargs is a blank space. To change it from default, use “-d” command followed by a single character or an escape character. Example: n for new line.


[command-providing-input] | xargs -d [new-delimiter] | xargs [command]

Xargs Command instructs you to use the asterisk (*) as a delimiter and apply mkdir for each argument you receive.

#10. List all the Linux Accounts on the System

Use xargs to organize the output of the commands, such as cut. Consider the following example:

xargs commands help to organize command outputs. You can use a cut also with it. 


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

Cut command access the /etc/passwd file and uses “:” delimiter to divide the beginning line in each file. The output of this execution process is then piped to sort and proceed with sorting each received string. Finally, after executing this process, it finally sorts the xargs commands that display them:

#11. Remove Blank Spaces in String

While looking for arguments, xargs ignores the blank spaces. So, the command eliminates the unnecessary blank spaces from strings. 

echo “[string-with-unnecessary-spaces]” | xargs

#12. List Number of Lines/Words/Characters in each File

To display a list of files within the line and character command, use xargs with the WC Command.

ls | xargs wc

The example shows you Is a command that basically works in piping to xargs only the files containing the word “example”. After this process, the xargs command is then applied to “wc” to that list:

#13. Copy Files to Multiple Directories. 

To copy files from multiple directories, use the xargs command. 


echo [directory-1] [directory-2] | xargs -n 1 cp -v [filename]


Echo = Provide directory names.

Xargs = to fetch standard output.

Cp Command = copy files into given directories. 


Once you go through this article, you should be aware of the xargs Command Usage in Linux. Through this guide, you will get to know an ample range of command options with examples about how to use them with xargs. So, I hope this guide is informative for you! 

Stay tuned with Hostbillo for more technical information! 

Arpit Saini

He is the Chief Technology Officer at Hostbillo Hosting Solution and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles

Leave a Reply

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