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. now you want
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 files, ascii text, etc.). you can process the output so that it only displays the file name, now 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. It passes 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 display the first field:
Alert_DBA102.log
Alert_DBA102.log.Z
Dba102_asmb_12307.trc.Z
Dba102_asmb_20653.trc.Z
Now, we want to use the ls-l command to pass the above list as a parameter and 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 convenient when you want to search multiple files and open them for editing.
It also has several 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.
-T option uses a verbose mode. it displays the command to be run, which is a very helpful option during debugging.
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 exists after searching for "SSSSSS". Therefore, the xargs input is empty, as shown in the second line (the result generated by using the-t detailed 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.
Assume that 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? 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 05:21 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 {}.
I need to use. svn-managed projects are managed by CVS. Therefore, before importing the entire project to CVS, I need to delete all directories and sub-directories. svn directory and. sub-directories and files under the svn directory. I have been searching for the simplest method and finally wrote a program to do this. My colleague told me that I only needed one command: find-name '. svn' | xargs rm-rf
This lesson is very heavy. I hate dummies all my life, but at that moment I find myself the biggest dummies!
Yes, even though we -- at least me, use LINUX every day, is it really efficient to use this system? No, most of the time I didn't take the time to study it. I used this system in some silly ways and modes of thinking.
1. create a multi-level Directory:
The mkidr command is used to create a directory. In fact, we always do this. However, in this case, we did not look at the additional functions provided by its parameters.
Mkidr-p/share/dragon; create the share directory under the root directory and create the dragon directory under the/share directory. Mkdir-p guicmd/{bin, lib, src, share/version, doc/{html, pdf, info, man }}
To create a complex project directory tree.
2. use find with xargs:
Find-name '. svn' | xargs rm-rf; needless to say, this is what I mentioned earlier.
Xargs is more like a filter. it is an extremely efficient method to process the content that matches the file name passed by the pipeline.