Log one Linux command: Xargs ____linux

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

Xargs
Most Linux commands produce output: A list of files, a list of strings, and so on. But what to do if you want to use one of the other commands and take the output of the previous command as a parameter. For example, the file command displays the types of files (executables, ASCII text, and so on); You can process the output so that it displays only the filename, and you currently want to pass these names to the Ls-l command to view the time stamp. The Xargs command is used to do the work. He allows you to perform some other commands on the output. Remember the following syntax from the 1th part:
File-lz * | grep ASCII | Cut-d ":"-F1 | Xargs ls-ltr
Let's dissect this command string. The first, File-lz *, is used to find symbolic links or compressed files. He passes the output to the next command, grep ASCII, where the command searches for the "ASCII" string and produces the output shown below: Alert_DBA102.log:ASCII Chinese text
Alert_dba102.log.z:ascii text (compress ' d data bits)
Dba102_asmb_12307.trc. Z:ascii 中文版 text (compress ' d data bits)
Dba102_asmb_20653.trc. Z:ascii 中文版 text (compress ' d data bits)
Since we are only interested in file names, we apply the next command cut-d ":"-f1 to show only 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 list as a parameter and pass one at a time. The Xargs order allows you to do so. The last part, Xargs ls-ltr, is used to receive the output and execute the ls-ltr command, as follows:
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
As a result, xargs itself is not very useful, but when combined with other commands, he is very powerful.
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, please use the method that best suits your situation.
With this method, you can quickly rename the 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 the file you want to edit:
$ file * | grep ASCII | Cut-d ":"-F1 | Xargs VI
The command uses VI to open files one by one. It is convenient to use this command when you want to search multiple files and open them for editing.
He has a few more 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 requirements here require you to confirm before running each command. If you press "Y", execute the command. This option is useful when you have some potentially destructive and unrecoverable actions (such as deletion or overwrite) of a file.
The-t option uses a detailed mode; he shows the command to run, a very helpful option during the debugging process.
What to do 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 Xargs's input is empty, as shown in the second line (as a result of our use-t-this detailed option). While this may help, 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, the command exits.
Suppose you want to delete a file using the RM command, which will be the parameter of the Xargs command. However, RM can only accept a limited number of parameters. What to do if your argument list exceeds this limit. The Xargs-n option restricts the number of arguments for a single command line.
The following shows how to limit the use of only two parameters per command line: Even if five files are passed to Xargs ls-ltr, only two files are passed to ls-ltr each 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 19:18 DBA102_CJQ0_14493.TRC
-rw-r--r--1 Oracle DBA 2410225 Aug 05:31 Alert_dba102.log
Ls-ltr DBA102_MMNL_14497.TRC DBA102_RECO_14491.TRC
-RW-R-----1 Oracle DBA 5386163 Aug 17:55 dba102_mmnl_14497.trc
-RW-R-----1 Oracle DBA 6808 Aug 05:21 dba102_reco_14491.trc
Ls-ltr DBA102_RVWR_14518.TRC
-RW-R-----1 Oracle DBA 2087 Aug 04:30 dba102_rvwr_14518.trc
With this method, you can quickly rename the 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.