Usage of xargs in "Go" Linux

Source: Internet
Author: User

Xargs
Most Linux commands produce output: A list of files, a list of strings, and so on. But what 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 shows only the file name, and you now want to pass these names to the Ls-l command to see the timestamp. 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:
Example 1:

File-lz * | grep ASCII | Cut-d ":"-F1 | Xargs ls-ltr
Let's dissect this command string. The first, File-lz *, is used to find a file that is a symbolic link or a compressed one. He passes the output to the next command, grep ASCII, where the command searches for an "ASCII" string and produces the output shown below:

Alert_DBA102.log:ASCII 中文版 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, showing only the first field:

Alert_dba102.log
Alert_dba102.log.z
Dba102_asmb_12307.trc. Z
Dba102_asmb_20653.trc. Z
For now, we want to use the LS-L command to pass the above list as parameters and pass one at a time. The Xargs command allows you to do so. The last part, Xargs ls-ltr, is used to receive the output and execute the ls-ltr command on it, 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 of much use, but when combined with other commands, his functions are very powerful.
Here is another example where we want to calculate the number of rows in these files:
Example 2:

$ 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.
Using this method, you can quickly rename files in the directory.

Example 3:
$ 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:
Example 4:
$ 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.
He has a few more options. The most useful may be the-p option, which makes the operation interoperable:

Example 5:
$ 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?...
Here are the XARG requirements you need to confirm before running each command. If you press "Y", the command is executed. You'll find this option useful when you're doing something that might be corrupted and unrecoverable, such as deleting or overwriting a file.
The-t option uses a detailed mode; he 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:
Example 6:
$ 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 the Xargs is empty, as shown in the second line (as a result of using the-t this detailed option). While this may be helpful, in some cases you may want to stop xargs if there is no content to process, and if so, you can use the-r option:

Example 7:
$ 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 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.
Example 8:
$ 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
Using this method, you can quickly rename files in the directory.

More Practical applications
$ ls | XARGS-T-i mv {} {}.bak
The-i option tells Xargs to replace {} with the name of each item.


Delete a larger number of files
ls | Xargs-n RM-FR
LS Of course output all file names (separated by a space)
Xargs is the output of the LS, every 20 for a set (with a space delimiter), as the parameters of the RM-RF
This means that all filenames are set to 20, deleted by RM-RF, so that they do not exceed the length of the command line.

Usage of xargs in "Go" Linux

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.