The effect is to convert the argument list to small pieces to pass to other commands to avoid the problem of too long argument list
Xargs is a filter that passes parameters to the command, and is also a tool for combining multiple commands. It divides a data stream into small enough chunks to facilitate the processing of filters and commands. Typically, Xargs reads data from a pipe or stdin, but it can also read data from the output of a file. The default command for Xargs is echo, which means that the input passed to Xargs by the pipeline will contain line breaks and whitespace, but with xargs processing, line breaks and whitespace will be replaced by spaces.
Xargs is a powerful command that captures the output of a command and then passes it to another command, and here are some practical examples of how to use xargs effectively.
1. When you try to delete too many files with RM, you may get an error message:/BIN/RM Argument list too long. Use Xargs to avoid this problem.
?
1 |
find~ -name ‘*.log‘ -print0 |xargs-0rm-f |
2. Get a list of all the *.conf endings in/etc/, there are several different ways to get the same result, the following example is just a demonstration of how practical Xargs, in this example practical xargs to pass the output of the Find command to Ls-l
?
1 |
# find /etc -name "*.conf" | xargs ls –l |
3. If you have a file containing many URLs that you would like to download, you can use Xargs to download all the links
?
1 |
# cat url-list.txt | xargs wget –c |
4. Find all the JPG files and compress it
?
1 |
# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz |
5. Copy all the picture files to an external hard drive
?
1 |
# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory |
EXAMPLES
Find/tmp-name Core-type F-print | Xargs/bin/rm-f
Find files named core in or below the directory/tmp and delete them. Note that this would work incorrectly if there is any filenames containing newlines or spaces.
Find/tmp-name Core-type f-print0 | xargs-0/bin/rm-f
Find files named core in or below the directory/tmp and delete them, processing filenames in such a-to that file or dire Ctory names containing spaces or newlines are correctly handled.
Find/tmp-depth-name Core-type F-delete
Find files named core in or below the directory/tmp and delete them, but more efficiently than in the previous example (b Ecause we avoid the need to use fork (2) and EXEC (2) to launch RM and we don ' t need the extra xargs process).
Cut-d:-f1 </etc/passwd | Sort | Xargs Echo
Generates a compact listing of the users on the system.
Xargs sh-c ' emacs ' [email protected] ' </dev/tty ' Emacs
Launches the minimum number of copies of Emacs needed, one after the other, to edit the files listed on Xargs ' standard in Put. This example achieves the same effect as BSD ' s-o option, but in a more flexible and portable the.
For example, the following command:
The code is as follows:
RM ' Find/path-type f '
If there are too many files in the path directory, the error cannot be performed because the parameter list is too long. But after switching to Xargs, the problem is resolved.
The code is as follows:
Find/path-type f-print0 | xargs-0 RM
In this example, Xargs splits the long list of files created by find into multiple substrings, and then calls RM for each substring. -print0 indicates that the output is delimited by null (-print uses line wrapping);-0 means that the input is delimited by null. This is much more efficient than using the Find command as follows.
The code is as follows:
Find/path-type f-exec rm ' {} ';
The Xargs command should be immediately followed by the pipe operator, which takes standard input as the primary source data stream, and uses stdin to execute additional commands by providing command-line arguments, such as:
The code is as follows:
Command | Xargs
The instance applies 1 to convert the multiline input to a single-line output:
The code is as follows:
[Email protected]:~/learn$ cat Example.txt
1 2 3) 4 5
6 7
8
[Email protected]:~/learn$ cat Example.txt | Xargs
1 2 3 4 5 6 7 8
Instance application 2, converts a single-line input to multiple lines of output:
The code is as follows:
[Email protected]:~/learn$ cat Example.txt | Xargs-n 2
1 2
3 4
5 6
7 8
Whitespace is the default delimiter,-n means that several parameters are displayed per line
You can also use the-d parameter to separate parameters, as follows:
The code is as follows:
[Email protected]:~/learn$ echo "Splitxhixamoslixsplit" | Xargs-d "X"-N 1
Split
Hi
Amosli
Split
Instance application 3, reading stdin, passing formatting parameters to the command
The code is as follows:
#Define an echo command to add after each output parameter #
[Email protected]:~/learn$ cat cecho.sh
echo $* ' # '
# requirement 1: Output multiple parameters
[Email protected]:~/learn$ sh cecho.sh arg1
arg1#
[Email protected]:~/learn$ sh cecho.sh arg2
arg2#
[Email protected]:~/learn$ sh cecho.sh arg3
arg3#
#需求2: All command parameters are available at once
[Email protected]:~/learn$ sh cecho.sh arg1 arg2 arg3
Arg1 arg1 arg2 arg3#
# for Demand 1, 2, using Xargs instead, first with VI to build a new file Args.txt, as follows:
[Email protected]:~/learn$ cat Args.txt
Arg1
Arg2
Arg3
# Batch OUTPUT parameters:
[Email protected]:~/learn$ cat Args.txt | Xargs-n 1
Arg1
Arg2
Arg3
[Email protected]:~/learn$ cat Args.txt | Xargs-n 2 sh cecho.sh
Arg1 arg2#
arg3#
# Output all parameters at once:
[Email protected]:~/learn$ cat Args.txt | Xargs SH cecho.sh;
Arg1 arg2 arg3#
Requirement 3, how do I embed parameters in a fixed command line? As shown below:
The code is as follows:
[Email protected]:~/learn$ sh cecho.sh-p args1-1
-P args1-1#
[Email protected]:~/learn$ sh cecho.sh-p args2-1
-P args2-1#
[Email protected]sli-pc:~/learn$ sh cecho.sh-p args3-1
-P args3-1#
Solutions for using Xargs:
The code is as follows:
[Email protected]:~/learn$ cat Args.txt | xargs-i {} sh cecho.sh-p {}-1
-P arg1-1#
-P arg2-1#
-P arg3-1#
#-i {} has a replacement string, the string {} is replaced with a parameter read from stdin, and when I is used, it can be cycled to replace the corresponding parameter as required
Example application 4, combined with find using Xargs
As already mentioned above, it is important to note that the file name delimiter is delimited by the null character of the output, as shown below, or may mistakenly delete the file
The code is as follows:
[Email protected]:~/learn$ Find. -type f-name "*test*.txt"-print0 | xargs-0 rm-f
Other:
The code is as follows:
Cat File | (while read Arg; does cat $arg; done)
Cat File | Xargs-i {} cat {}
Linux shell scripting Learn Xargs commands using detailed