In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about the use of the xargs command on the basis of Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Brief introduction
The xargs command has two main points. First, you must list the target file. Second, you must specify the commands or scripts to be executed for each file.
The xargs command is used to process files distributed in different directories:
Calculate the number of lines in all files
Prints the first line of the specified file
Execute a custom script for each file
Xargs can convert the input (usually passed through a command line pipeline) into parameters for subsequent commands, usually for the following purposes:
Command combinations: in particular, some commands do not support pipeline input, such as ls.
Avoid too long parameters: xargs can group parameters with-nx to avoid too long parameters.
The syntax to use is as follows
Usage: xargs [OPTION]... COMMAND INITIAL-ARGS...Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.
Getting started example
First, create a test file
Touch a.js b.js c.js
Next, run the following command:
Ls * .js | xargs ls-al
The output is as follows:
-rw-r--r-- 1 a wheel 0 12 18 16:18 a. JS wheel 0 12 18 16:18 an a wheel 0 12 18 16:18 b.js Mustang RW Mustang r Mube-1 a wheel 0 12 18 16:18 c.js
The command explains:
First, the output of ls * .js is a.js b.js c.js.
Through the pipeline, a.js b.js c.js is used as the input parameter for xargs.
After receiving the input parameters, the xargs command parses the parameters and splits them into multiple parameters with spaces / line breaks as delimiters, which is changed to a.js, b.js and c.js.
Xargs passes the split parameters to subsequent commands as arguments to subsequent commands, that is, to make up such a command ls-al a.js b.js c.js.
You can add the-t argument to print out the following command before executing it.
Ls * .js | xargs-t ls-al
The output is as follows, and you can see that there is an extra line of ls-al a.js b.js c.js, which is the command that is actually running.
Ls-al a.js b.js c.jsmurrwkashi-1 a wheel 0 12 18 16:18 a.jsKui RWKui-1 a wheel 0 12 18 16:18 b.jsMurray RWMui-1 a wheel 0 12 18 16:18 c.js
Example: parameter replacement
Sometimes, we need to use the original parameters, which can be achieved by the parameter-I or-I. The parameters are described as follows
-I R same as-- replace=R (R must be specified)-- imam Murray Replace R in initial arguments with names read from standard input = [R] place. If R is unspecified, assume {}
As an example, all files ending with .js are suffixed with .backup. -I'{} 'means to replace the {} of the subsequent command line with the parameters parsed earlier.
Ls * .js | xargs-t-I'{}'mv {}. Backup
The expanded command is as follows:
Mv a.js a.js.backupmv b.js b.js.backupmv c.js c.js.backup
Example: parameter grouping
The command line limits the maximum length of parameters, and xargs solves this problem by grouping parameters through-nx.
First, create four files to do the experiment.
Touch a.js b.js c.js d.js
Then run the following command:
Ls * .js | xargs-t-N2 ls-al
The output is as follows,-N2 indicates that the parameters are passed in groups of two to the following commands.
Ls-al a.js b.js-rw-r--r-- 1 root root 0 Dec 18 16:52 a.js Dec root root 0 Dec 18 16:52 a.js root root 0 Dec 18 16:52 b.jsls-al c.js d.js-rw-r--r-- 1 root root 0 Dec 18 16:52 d.js
Example: special file name
Sometimes, there may be special characters in the file name, such as spaces in the file name below.
Touch 'hello 01.css'' hello 02.css'
The previous command will report an error because xargs uses spaces / line breaks as delimiters, resulting in unexpected behavior.
# command find. -name'* .css'| xargs-t ls-al# outputs ls-al. / hello 01.css. / hello 02.css # expanded command ls: cannot access. / hello: No such file or directoryls: cannot access 01.css: No such file or directoryls: cannot access. / hello: No such file or directoryls: cannot access 02.css: No such file or directory
This is how xargs solves the problem.
-print0: tells the find command to follow the NULL character instead of the newline character after the file name is output
-0: tell xargs to use NULL as the parameter delimiter
Find. -name'* .css'- print0 | xargs-0-t ls-al
Example: log backup
Back up the logs from 7 days ago to a specific directory
Find. -mtime + 7 | xargs-I'{}'mv {} / tmp/otc-svr-logs/ thank you for reading! This is the end of this article on "what is the use of xargs commands on the basis of Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.