Home  >  Article  >  Operation and Maintenance  >  Detailed explanation of the usage of xargs in linux

Detailed explanation of the usage of xargs in linux

巴扎黑
巴扎黑Original
2017-08-22 13:57:522283browse

xargs is a very useful command in Linux. It is often used in combination with other commands and is very flexible.

xargs is a filter for passing parameters to commands, and it is also a way to combine multiple commands. Tool. It splits a data stream into pieces that are small enough to be processed by filters and commands. Therefore, this command is also a powerful replacement for back references. In general, command replacement that uses too many parameters fails. Sometimes, replacing it with xargs usually succeeds. Normally, xargs reads data from a pipe or stdin, but it can also read data from the output of a file.

The default command of xargs is echo. This means that the input passed to xargs through the pipe will contain newlines and whitespace, but through xargs processing, newlines and whitespace will be replaced by spaces. For example:

bash$ ls -l

total 0

-rwxr-xr-x 2 root root 4096 2009-02-23 090218.txt

-rwxr-xr-x 2 root root 12288 2009-06-08 090607 .txt

bash$ ls -l | xargs

090218.txt 090607.txt

bash$ find ~/mail -type f | xargs grep "Linux"

./misc:User-Agent: slrn/0.9.8.1 (Linux)

./sent-mail-jul-2005: hosted by the Linux Documentation Project.

. /sent-mail-jul-2005: (Linux Documentation Project Site, rtf version)

./sent-mail-jul-2005: Subject: Criticism of Bozo's Windows/Linux article

. . .

ls | xargs -p -l gzip Use gzips to compress each file in the current directory, one at a time, and prompt the user before each compression.

Note: a The interesting xargs option is -n NN. NN limits the number of parameters passed in each time.

ls | xargs -n 8 echo lists all files in the current directory in the form of 8 columns per line.

Note: Another useful option is -0, combined with find -print0 or grep -lZ. This allows processing of arguments containing whitespace or quotes.

find / - type f -print0 | xargs -0 grep -liwZ GUI | xargs -0 rm -f

grep -rliwZ GUI / | Delete any files containing "GUI".

-i means that the result of find passed to xargs is replaced by {}

-I I think it is similar to i. You can think that -i can be replaced by -I {}

-p Interactively ask y to confirm each execution of the command.

-t Echo each command before execution

There are also parameters -s and -x. Please check the manual for details.

The following is another example. We hope to calculate the contents of these files. Number of lines:

$ file * | grep ASCII | cut -d":" -f1 | xargs wc -l

47853 alert_DBA102.log

19 dba102_cjq0_14493.trc

29053 dba102_mmnl_14497.trc

154 dba102_reco_14491.trc

43 dba102_rvwr_14518.trc

77122 total

(Note: The above tasks can also be completed with the following commands:)

$ wc -l 'file * | grep ASCII | cut -d":" -f1 | grep ASCII | cut -d":" -f1'

This xargs version is used to illustrate concepts. Linux can accomplish the same task in several ways; use the method that best suits your situation.

Using this method, you can quickly rename files in the directory.

$ ls | xargs -t -i mv {} {}.bak

# The -i option tells xargs to replace {} with the name of each item. The -t option instructs xargs to print the command before executing it.

Another very useful operation is when you use vi to open a file for editing:

$ file * | grep ASCII | cut -d":" -f1 | xargs vi

This command uses vi to open files one by one. This command is very convenient when you want to search multiple files and open them for editing.

It also has several options. Probably the most useful is the -p option, which makes the operation interactive:

$ file * | grep ASCII | cut -d":" -f1 | xargs -p vi

vi alert_DBA102.log dba102_cjq0_14493.trc dba102_mmnl_14497.trc

dba102_reco_14491.trc dba102_rvwr_14518.trc ?...

The xarg here requires you to do it before running each command. confirm. If you press "y", the command is executed. You may find this option useful when you do something to a file that may be damaging and unrecoverable, such as deleting or overwriting it.

The -t option uses a verbose mode; it displays the commands to be run and is a very helpful option during debugging.

What if the output passed to xargs is empty? Consider the following command:

$ file * | grep SSSSSS | cut -d":" -f1 | xargs -t wc -l wc -l 0 $

at Here, the search for "SSSSSS" yields no matches; therefore the input to xargs is empty, as shown in the second line (due to our use of the -t verbose option). While this may help, in some cases you may want to stop xargs if there is nothing to process; if so, you can use the -r option:

$ file * | grep SSSSSS | cut -d":" -f1 | xargs -t -r wc -l $

If there is nothing to run, the command exits.

Suppose you wish to delete a file using the rm command (which will be passed as an argument to the xargs command). However, rm can only accept a limited number of arguments. What if your parameter list exceeds that limit? The -n option of xargs limits the number of arguments in a single command line.

The following shows how to limit each command line to only two arguments: even though five files are passed to xargs ls -ltr , only two files are passed to ls -ltr at a time.

$ file * | grep ASCII | cut -d":" -f1 | xargs -t -n2 ls -ltr

ls -ltr alert_DBA102.log dba102_cjq0_14493. trc

-rw-r----- 1 oracle dba 738 Aug 10 19:18 dba102_cjq0_14493.trc

-rw-r--r-- 1 oracle dba 2410225 Aug 13 05 :31 alert_DBA102.log

ls -ltr dba102_mmnl_14497.trc dba102_reco_14491.trc

-rw-r----- 1 oracle dba 5386163 Aug 10 17:55 dba102_mmnl_14497.trc

-rw-r----- 1 oracle dba 6808 Aug 13 05:21 dba102_reco_14491.trc

ls -ltr dba102_rvwr_14518.trc

-rw-r----- 1 oracle dba 2087 Aug 10 04:30 dba102_rvwr_14518.trc

Using this method, you can quickly rename files in the directory.

The above is the detailed content of Detailed explanation of the usage of xargs in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn