Introduction
Certain 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 xargs
command.
In this tutorial, you will learn how to use the Linux xargs
command to manipulate the standard input and work with other commands.
Prerequisites
- A system running Linux
- Access to the command line
What is the xargs Command?
The xargs
command 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 xargs
is used in combination with rm
, cp
, mkdir
, and other similar commands.
How to Use the xargs Command With Examples
When used on its own, xargs
prompts the user to enter a text string that it then passes to the echo
command.
The example shows an example input, followed by the output of the echo
command.
Note: The echo command is a built-in Linux feature that prints out arguments as the standard output. echo
is 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 find
The find command often precedes xargs
in a pipeline. Use it to provide a list of files for further processing by xargs
. The syntax looks like this:
find [location] -name "[search-term]" -type f | xargs [command]
The example above demonstrates using the find
command to find all files with the .sh
extension. The list of files is then piped to xargs
, which uses the rm
command to delete them.
However, xargs
does not automatically include files which contain blank spaces in their names. To include those files too, use the -print0
option for find
, and the -0
option for xargs
:
find [location] -name "[search-term]" -type f -print0 | xargs -0 [command]
rm
now deletes all the files with the .sh
extension.
Combine xargs with grep
Use xargs
with the grep command to search for a string in the list of files provided by the find
command.
find . -name '[search-term]' | xargs grep '[string-to-find-in-files]'
The example above searched for all the files with the .txt
extension and piped them to xargs
, which then executed the grep
command on them.
Xargs Multiple Commands
To run more than one command with xargs
, use the -I
option. The syntax is:
[command-providing-input] | xargs -I % sh -c '[command-1] %; [command-2] %'
In the example, the contents of file4.txt
were displayed first. Then mkdir
created a folder for each word in the file.
Read Items From File
As mentioned before, xargs
reads the standard input. Use the -a
option to read the contents of a file instead.
xargs -a [filename]
Find and Archive Images Using tar
When used with the tar command, xargs
creates a tar.gz
archive and populates it with files provided by the find
command.
find [location] -name "[search-term]" -type f -print0 | xargs -0 tar -cvzf [tar-gz-archive-name]
Print Command
To see the commands executed by xargs
in standard output, use the -t
option.
[command-providing-input] | xargs -t [command]
In the example above, notice that xargs
executed the mkdir command on the entire string provided by echo
.
Approve xargs Command Execution
Some xargs
operations, like removing files and folders, are irreversible. To control the execution of those commands, use the -p
option.
[command-providing-input] | xargs -p [command]
When you execute the command with the -p
option, xargs
displays a confirmation line before executing it. Type y
to proceed, or n
to cancel the operation.
Limit Output per Line
Sometimes it is necessary to control the number of arguments xargs
takes at the same time. Perform this action using the -n
option followed by the number of arguments you are limiting xargs
to:
[command-providing-input] | xargs –n [number] [command]
In the example below, xargs
takes the string from the echo
command and splits it into three. Then it executes another echo
for each of the parts:
Specify the Delimiter
The default xargs
delimiter is a blank space. To change the default delimiter, use the -d
command followed by a single character or an escape character such as n
(a new line).
[command-providing-input] | xargs -d [new-delimiter] | xargs [command]
In the example below, the xargs
command instructs the system to use *
as a delimiter and apply mkdir
to each of the obtained arguments.
List All Linux User Accounts on the System
Use xargs
to organize the output of the commands, such as cut
. Consider the following example:
cut -d: -f1 < /etc/passwd | sort | xargs
The cut command accesses the /etc/passwd
file 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 xargs
that displays them:
Note: For alternative ways to list users, read How to List Users in Linux.
Remove Blank Spaces in String
Since xargs
ignores 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 File
Use xargs
with the wc
command to display a list of files with the line, word, and character count.
ls | xargs wc
The example below instructed the ls command to pipe to xargs
only the files containing the word “example”. xargs
then applied wc
to that list:
Copy File to Multiple Directories
Copy files to multiple directories using the xargs
command. The syntax is simple:
echo [directory-1] [directory-2] | xargs -n 1 cp -v [filename]
The echo
command provides directory names, and xargs
uses the cp command to copy the given file into each of the directories.
Conclusion
After completing this tutorial, you should know how to use the xargs
command. The article provided a list of command options and showed how to use xargs
in combination with the commands that are frequently used with it.
原创文章,作者:506227337,如若转载,请注明出处:https://blog.ytso.com/224309.html