Linux shell Script Learning xargs command using the detailed _linux shell

Source: Internet
Author: User
Tags stdin

Xargs is a filter that passes parameters to a command and is 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 pipes 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 that passes through the pipe to the Xargs will contain newline and white space, but the newline and whitespace will be replaced by a space by the xargs processing.

Xargs is a powerful command that captures the output of a command and then passes it to another command, and here is a practical example 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

Find ~-name ' *.log '-print0 | xargs-0 rm-f

2. To get the list of files at the end of all *.conf in/etc/, there are several different ways to get the same result, and the following example is just a demonstration of how to use Xargs, in this case the practical xargs to pass the output of the Find command to Ls-l

# find/etc-name "*.conf" | Xargs ls–l

3. If you have a file that contains a lot of URLs you would like to download, you can use Xargs to download all links

# Cat Url-list.txt | Xargs Wget–c

4. Find all JPG files and compress it

# Find/-name *.jpg-type F-print | Xargs Tar-cvzf images.tar.gz

5. Copy all the picture files to an external hard drive

# 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. This would work incorrectly if there are any filenames containing or newlines.

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 way that file or dire Ctory names containing spaces or newlines are correctly.

Find/tmp-depth-name Core-type F-delete
Find files named core in or below the directory/tmp and deletes 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 the "all" users on the system.

Xargs sh-c ' emacs ' $@ ' </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 is example achieves the same effect as BSD ' s-o option, but in a more flexible and portable way.

For example, the following command:

Copy Code code as follows:

RM ' Find/path-type f '

If there are too many files in the path directory, the error cannot be performed because the argument list is too long. However, after switching to Xargs, the problem is solved.

Copy Code code as follows:

Find/path-type f-print0 | xargs-0 RM

In this example, xargs the long list of files produced by the find to be separated into multiple substrings, and then call RM on each substring. -print0 indicates that the output is null-delimited (-print uses a newline);-0 indicates that the input is null-delimited. This is much more efficient than using the Find command as follows.

Copy Code code as follows:

Find/path-type f-exec rm ' {} ' \;

The Xargs command should immediately follow the pipe operator, which takes the standard input as the primary source stream, uses stdin, and executes other commands by supplying command-line arguments, such as:

Copy Code code as follows:

Command | Xargs

Instance applies 1 To convert multiline input to Single-line output:

Copy Code code as follows:

amosli@amosli-pc:~/learn$ Cat Example.txt
1 2 3 4 5
6 7
8
amosli@amosli-pc:~/learn$ Cat Example.txt | Xargs
1 2 3 4 5 6 7 8

Instance applies 2 to convert single-line input to multiline output:

Copy Code code as follows:

amosli@amosli-pc:~/learn$ Cat Example.txt | Xargs-n 2
1 2
3 4
5 6
7 8

A space is the default delimiter, and-n indicates that several arguments are displayed per line

You can also use the-D argument to separate parameters, as follows:

Copy Code code as follows:

amosli@amosli-pc:~/learn$ echo "Splitxhixamoslixsplit" | Xargs-d "X"-N 1
Split
Hi
Amosli
Split

Instance applies 3, reads stdin, and passes formatting parameters to the command

Copy Code code as follows:

#定义一个echo命令每次在输出参数后都加上 #
amosli@amosli-pc:~/learn$ Cat cecho.sh
echo $* ' # '

# requirement 1: Output multiple parameters
amosli@amosli-pc:~/learn$ SH cecho.sh arg1
arg1#
amosli@amosli-pc:~/learn$ SH cecho.sh arg2
arg2#
amosli@amosli-pc:~/learn$ SH cecho.sh arg3
arg3#

#需求2: Provide all command parameters at once
amosli@amosli-pc:~/learn$ sh cecho.sh arg1 arg2 arg3
Arg1 arg1 arg2 arg3#

# for Demand 1, 2, use Xargs instead, first with VI to build a new file Args.txt, as follows:
amosli@amosli-pc:~/learn$ Cat Args.txt
Arg1
Arg2
Arg3
# Bulk OUTPUT Parameters:
amosli@amosli-pc:~/learn$ Cat Args.txt | Xargs-n 1
Arg1
Arg2
Arg3
amosli@amosli-pc:~/learn$ Cat Args.txt | Xargs-n 2 sh cecho.sh
Arg1 arg2#
arg3#
# Output all parameters at once:
amosli@amosli-pc:~/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:

Copy Code code as follows:

amosli@amosli-pc:~/learn$ SH cecho.sh-p args1-1
-P args1-1#
amosli@amosli-pc:~/learn$ SH cecho.sh-p args2-1
-P args2-1#
amosli@amosli-pc:~/learn$ SH cecho.sh-p args3-1
-P args3-1#

Using the Xargs solution:

Copy Code code as follows:

amosli@amosli-pc:~/learn$ Cat Args.txt | xargs-i {} sh cecho.sh-p {}-1
-P arg1-1#
-P arg2-1#
-P arg3-1#

#-i {} A replacement string was granted, and the string {} is replaced with a parameter read from stdin, which, when used, can be recycled to replace the corresponding argument as required

Example Application 4, combining find using Xargs

As mentioned above, it should be noted that the file name delimiter separates the output with the character null, as shown below, otherwise you may accidentally delete the file

Copy Code code as follows:

amosli@amosli-pc:~/learn$ find. -type f-name "*test*.txt"-print0 | xargs-0 rm-f

Other:

Copy Code code as follows:

Cat File | (while read Arg; doing cat $arg; done)
Cat File | Xargs-i {} cat {}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.