IntroductionCertain Linux commands take input both from the standard input (stdin) and as a command-line argument. However, others are designed to take input only as an argument. To be able to process the standard input, those Linux commands need to utilize the
xargscommand.In this tutorial, you will learn how to use the Linux
xargscommand to manipulate the standard input and work with other commands.
- A system running Linux
- Access to the command line
What is the xargs Command?The
xargscommand builds and executes commands provided through the standard input. It takes the input and converts it into a command argument for another command. This feature is particularly useful in file management, where
xargsis used in combination with
mkdir, and other similar commands.
How to Use the xargs Command With ExamplesWhen used on its own,
xargsprompts the user to enter a text string that it then passes to the
echocommand. The example shows an example input, followed by the output of the
Note: The echo command is a built-in Linux feature that prints out arguments as the standard output.
echois commonly used to display text strings or command results as messages. Learn about all the different ways you can use the echo command in Linux.
Combine xargs with findThe find command often precedes
xargsin a pipeline. Use it to provide a list of files for further processing by
xargs. The syntax looks like this:
The example above demonstrates using the
find [location] -name "[search-term]" -type f | xargs [command]
findcommand to find all files with the
.shextension. The list of files is then piped to
xargs, which uses the
rmcommand to delete them.However,
xargsdoes not automatically include files which contain blank spaces in their names. To include those files too, use the
find, and the
find [location] -name "[search-term]" -type f -print0 | xargs -0 [command]
rmnow deletes all the files with the
Combine xargs with grepUse
xargswith the grep command to search for a string in the list of files provided by the
The example above searched for all the files with the
find . -name '[search-term]' | xargs grep '[string-to-find-in-files]'
.txtextension and piped them to
xargs, which then executed the
grepcommand on them.
Xargs Multiple CommandsTo run more than one command with
xargs, use the
-Ioption. The syntax is:
In the example, the contents of
[command-providing-input] | xargs -I % sh -c '[command-1] %; [command-2] %'
file4.txtwere displayed first. Then
mkdircreated a folder for each word in the file.
Read Items From FileAs mentioned before,
xargsreads the standard input. Use the
-aoption to read the contents of a file instead.
xargs -a [filename]
Find and Archive Images Using tarWhen used with the tar command,
tar.gzarchive and populates it with files provided by the
find [location] -name "[search-term]" -type f -print0 | xargs -0 tar -cvzf [tar-gz-archive-name]
Print CommandTo see the commands executed by
xargsin standard output, use the
In the example above, notice that
[command-providing-input] | xargs -t [command]
xargsexecuted the mkdir command on the entire string provided by
Approve xargs Command ExecutionSome
xargsoperations, like removing files and folders, are irreversible. To control the execution of those commands, use the
When you execute the command with the
[command-providing-input] | xargs -p [command]
xargsdisplays a confirmation line before executing it. Type
yto proceed, or
nto cancel the operation.
Limit Output per LineSometimes it is necessary to control the number of arguments
xargstakes at the same time. Perform this action using the
-noption followed by the number of arguments you are limiting
In the example below,
[command-providing-input] | xargs -n [number] [command]
xargstakes the string from the
echocommand and splits it into three. Then it executes another
echofor each of the parts:
Specify the DelimiterThe default
xargsdelimiter is a blank space. To change the default delimiter, use the
-dcommand followed by a single character or an escape character such as
n(a new line).
In the example below, the
[command-providing-input] | xargs -d [new-delimiter] | xargs [command]
xargscommand instructs the system to use
*as a delimiter and apply
mkdirto each of the obtained arguments.
List All Linux User Accounts on the SystemUse
xargsto organize the output of the commands, such as
cut. Consider the following example:
The cut command accesses the
cut -d: -f1 < /etc/passwd | sort | xargs
/etc/passwdfile and uses the
:delimiter to cut the beginning of each line in the file. The output is then piped to
sort, which sorts the received strings, and finally to
xargsthat displays them:
Note: For alternative ways to list users, read How to List Users in Linux.
Remove Blank Spaces in StringSince
xargsignores blank spaces when looking for arguments, the command is useful for removing unnecessary blank spaces from strings.
echo "[string-with-unnecessary-spaces]" | xargs
List Number of Lines/Words/Characters in Each FileUse
xargswith the wc command to display a list of files with the line, word, and character count.
The example below instructed the ls command to pipe to
ls | xargs wc
xargsonly the files containing the word “example”.
wcto that list:
Copy File to Multiple DirectoriesCopy files to multiple directories using the
xargscommand. The syntax is simple:
echo [directory-1] [directory-2] | xargs -n 1 cp -v [filename]
echocommand provides directory names, and
xargsuses the cp command to copy the given file into each of the directories.
ConclusionAfter completing this tutorial, you should know how to use the
xargscommand. The article provided a list of command options and showed how to use
xargsin combination with the commands that are frequently used with it.