Linux xargs command usage detailed "Go"

Source: Internet
Author: User

Xargs is a very useful command in Linux, and it is often used in combination with other commands and is very flexible.
Xargs is a filter that passes parameters to the command, and is also a tool for combining multiple commands. It divides a stream into small enough chunks to facilitate the processing of filters and commands. This command is also a powerful replacement for the post reference. When a command substitution that uses too many parameters is generally unsuccessful, It is generally successful to replace it with Xargs. 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. This means that the input passed to Xargs by the pipeline will contain newline and white space, but the line breaks and whitespace will be replaced by a space by the xargs processing. 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"
./www.qixoo.qixoo.com/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 uses gzips to compress each file in the current directory, one at a time, and prompts the user before each compression.
Note: An interesting xargs option is N-nn,nn, which limits the number of arguments that are passed in each pass.
ls | Xargs-n 8 echo Lists all the files in the current directory in the form of 8 columns per row.
Note: Another useful option is-0, using the combination of find-print0 or GREP-LZ. This allows processing of parameters that contain whitespace or quotation marks.
Find/-type f-print0 | xargs-0 Grep-liwz GUI | xargs-0 rm-f
Grep-rliwz GUI/| xargs-0 rm-f
The top two lines can be used to delete any files that contain "GUI".
-I means the result of find passed to Xargs is replaced by {}.
I think it's similar to I, so I think I can use-I {} instead
-P Interactive Ask Y to confirm each execution of the command.
-t echo each command before execution
There are also parameter-s and-X specific search manuals.

Here is another example where we want to calculate the number of rows in these files:



$ file * | grep ASCII | Cut-d ":"-F1 | Xargs wc-l

47853 Alert_dba102.log

Dba102_cjq0_14493.trc

29053 DBA102_MMNL_14497.TRC

154 DBA102_RECO_14491.TRC

Dba102_rvwr_14518.trc

77122 Total

(Note: The above tasks can also be used to complete the following command:)

$ Wc-l ' file * | grep ASCII | Cut-d ":"-F1 | grep ASCII | Cut-d ":"-f1 '

The Xargs version is used to illustrate concepts. Linux can accomplish the same task in several ways, using the method that best suits your situation.



With this method, you can quickly rename files in a 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.



Another very useful operation is when you use VI to open the file you want to edit:



$ file * | grep ASCII | Cut-d ":"-F1 | Xargs VI

This command uses VI to open the file one by one. This command is handy when you want to search for multiple files and open them for editing.



It also has several options. The most useful may be the-p option, which makes the operation interoperable:



$ 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 confirm before running each command. If you press "Y", the command is executed. You will find this option useful when you are doing something that might be corrupted and unrecoverable, such as deleting or overwriting it.



The-t option uses a verbose mode, which displays the command to run, which 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 $

Here, there is no matching content after searching for "ssssss", so the input to Xargs is empty, as shown in the second line (as a result of the verbose option we use-t). While this may help, in some cases you may want to stop xargs if you do not have content to work with, and if so, you can use the-r option:

$ file * | grep ssssss | Cut-d ":"-F1 | Xargs-t-R Wc-l $

If there is no content to run, the command exits.



Suppose you want to delete a file by using the RM command, which will be a parameter to the Xargs command. However, RM can only accept a limited number of parameters. What if your parameter list exceeds this limit? The-n option of Xargs limits the number of arguments to a single command line.

The following shows how to limit the use of only two parameters per command line: Even if you pass five files 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 19:18 DBA102_CJQ0_14493.TRC

-rw-r--r--1 Oracle DBA 2410225 05:31 Alert_dba102.log





Ls-ltr DBA102_MMNL_14497.TRC DBA102_RECO_14491.TRC

-RW-R-----1 Oracle DBA 5386163 17:55 DBA102_MMNL_14497.TRC

-RW-R-----1 Oracle DBA 6808 05:21 DBA102_RECO_14491.TRC





Ls-ltr DBA102_RVWR_14518.TRC

-RW-R-----1 Oracle DBA 2087 04:30 DBA102_RVWR_14518.TRC

With this method, you can quickly rename files in a directory.

Xargs command usage under Linux "Go"

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.