Record a Linux Command: xargs

Source: Internet
Author: User
Original post: http://www.sudu.cn/info/html/edu/linux/20080102/290238.html

Xargs
Most Linux commands generate output: file list, string list, and so on. But what if I want to use another command and use the output of the previous command as a parameter? For example, the file command displays the file type (Executable File, ASCII text, etc.); you can process the output so that it only displays the file name, currently, you want to pass these names to the LS-l command to view timestamps. The xargs command is used to complete this task. It allows you to execute some other commands on the output. Remember the following syntax from section 1st:
File-LZ * | grep ASCII | cut-d ":"-F1 | xargs LS-LTR
Let's analyze this command string. First, file-LZ * is used to find a symbolic link or compressed file. He will pass the output to the next command grep ASCII, which searches for the "ASCII" string and generates the following output: alert_dba102.log: ASCII English text
Alert_dba102.log.z: ASCII text (compress 'd data 16 bits)
Dba102_asmb_12307.trc.z: ASCII English text (compress 'd data 16 bits)
Dba102_asmb_20653.trc.z: ASCII English text (compress 'd data 16 bits)
Because we are only interested in the file name, we apply the next command cut-d ":"-F1, only show the first field: alert_dba102.log
Alert_dba102.log.z
Dba102_asmb_12307.trc.z
Dba102_asmb_20653.trc.z
Currently, we want to use the LS-l command to pass the above list as a parameter and pass one at a time. The xargs command allows you to do this. In the last part, xargs LS-LTR is used to receive the output and execute the LS-LTR command on it, as shown below:
Ls-LTR alert_dba102.log
Ls-LTR alert_dba102.log.z
Ls-LTR dba102_asmb_12307.trc.z
Ls-LTR dba102_asmb_20653.trc.z
Therefore, although xargs itself is not very useful, it is very powerful when combined with other commands.
The following is another example. We want to calculate the number of rows in these files:
$ 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 preceding tasks can also be completed using the following command :)
$ WC-l 'file * | grep ASCII | cut-d ":"-F1 | grep ASCII | cut-d ":"-f1'
The xargs version is used to explain the concept. Linux can use several methods to complete the same task. Please use the method that best suits your situation.
By 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 indicates that xargs prints the command before executing it.
Another useful operation is when you use VI to open the file to be edited:
$ 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.
He has several more options. The most useful option 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 confirm before running each command. If you press "Y", execute the command. This option is useful when you perform operations (such as deleting or overwriting) on files that may be damaged and irrecoverable.
The-T option uses a detailed mode. It shows the command to be run, which is a very helpful option in the debugging process.
What if the output passed to xargs is null? Consider the following command:
$ File * | grep Ssssss | cut-d ":"-F1 | xargs-t wc-l
WC-l
0
$
In this case, no matching content is found after searching for "Ssssss". Therefore, the xargs input is empty, as shown in the second line (the results produced by using the detailed-T option ). This may be helpful, but in some cases, if there is no content to process, you may want to stop xargs; 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, this command exits.
Suppose you want to use the RM command (this command will be used as a parameter of the xargs command) to delete the file. 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 parameters in a single command line.
The following shows how to restrict each command line to only two parameters: Even if 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 dba102_cjq0_14493.trc
-RW-r -- 1 Oracle DBA 2410225 Aug 13 alert_dba102.log
Ls-LTR dba102_mmnl_14497.trc dba102_reco_14491.trc
-RW-r ----- 1 Oracle DBA 5386163 Aug 10 dba102_mmnl_14497.trc
-RW-r ----- 1 Oracle DBA 6808 Aug 13 dba102_reco_14491.trc
Ls-LTR dba102_rvwr_14518.trc
-RW-r ----- 1 Oracle DBA 2087 Aug 10 dba102_rvwr_14518.trc
By 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 {}.

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.