In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "the usage of xargs command in linux". In daily operation, I believe many people have doubts about the usage of xargs command in linux. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the usage of xargs command in linux". Next, please follow the editor to study!
How to use the xargs command
Syntax:
Xargs [OPTIONS] [COMMAND [initial-arguments]]
To take an example: we use pipe characters to transfer to xargs and run the touch command for each parameter, and-t means to print and create three files before execution:
[root@localhost ~] # echo "file1 file2 file3" | xargs-t touchtouch file1 file2 file3
How to limit the number of parameters
By default, the number of parameters passed to the command is determined by the system limit. The-n option specifies the number of parameters to pass to the command. Xargs runs the specified command as many times as needed until all the parameters are used up.
The following example specifies that one parameter is passed at a time:
[root@localhost ~] # echo "file1 file2 file3" | xargs-N1-t touchtouch file1 touch file2 touch file3
How to run multiple commands
To run multiple commands using xargs, use the-I or-I options. Customize a pass parameter symbol after-I or-I, and all matching items are replaced with the parameters passed to xargs.
In the following example, xargs runs two commands, first touch to create the file, and then ls to list it:
[root@localhost ~] # echo "file1 file2 file3" | xargs-t-I% sh-c 'touch%; ls-l%' sh-c touch file1 file2 file3;ls-l file1 file2 file3-rw-r--r--. 1 root root 0 Jan 30 00:18 file1-rw-r--r--. 1 root root 0 Jan 30 00:18 file2-rw-r--r--. 1 root root 0 Jan 30 00:18 file3
How to specify a delimiter
Use the-d or-- delimiter option to set a custom delimiter, either a single character or an escape character that begins with\.
The following example uses # as the delimiter, and the echo command uses the-n option, which means no new lines are printed:
[root@localhost ~] # echo-n file1#file2#file3#file4 | xargs-d\ #-t touchtouch file1 file2 file3 file4
How to read entries from a file
The xargs command can also read entries from a file rather than from standard input. Use the-an option, followed by the file name.
Create an ip.txt file and use each address in the xargs command ping:
[root@localhost ~] # cat ip.txt 114.114.114.114www.linuxprobe.com202.102.128.68
Use the-L 1 option, which means that xargs reads one row at a time. If this option is omitted, xargs will pass all ip to a ping command.
[root@localhost] # xargs-an ip.txt-t-L 1ping-c 1ping-c 1 114.114.114.114 PING 114.114.114 (114.114.114.114) 56 (84) bytes of data.64 bytes from 114.114.114.114: icmp_seq=1 ttl=93 time=11.0 ms--- 114.114.114.114 ping statistics-- 1 packets transmitted, 1 received, 0 packet loss Time 0msrtt min/avg/max/mdev = 11.026 received 11.026 msping-c 1 www.linuxprobe.com PING www.linuxprobe.com.w.kunlunno.com (221.15.65.202) 56 (84) bytes of data.64 bytes from hn.kd.jz.adsl (221.15.65.202): icmp_seq=1 ttl=48 time=20.9 ms--- www.linuxprobe.com.w.kunlunno.com ping statistics-1 packets transmitted, 1 received, 0 packet loss Time 0msrtt min/avg/max/mdev = 20.934 msping-c 1 202.102.128.68 PING 202.102.128.68 (202.102.128.68) 56 (84) bytes of data.64 bytes from 202.102.128.68 ping statistics-- 1 packets transmitted, 1 received, 0% packet loss Time 0msrtt min/avg/max/mdev = 8.710 ms 8.710 ms
Xargs is used with find
Xargs is usually used in conjunction with the find command. You can use find to search for specific files, and then use xargs to perform operations on those files.
To avoid problems with file names that contain newline characters or other special characters, always use the-print0 option of find, which allows find to print the full file name, which can be interpreted correctly with the-0 or-- null option with the xargs command.
In the following example, look for all files of type file under the log folder, package and compress them:
[root@localhost ~] # find log/-type f-print0 | xargs-- null tar-zcvf logs.tar.gzlog/anaconda/anaconda.loglog/anaconda/sysloglog/anaconda/program.loglog/anaconda/packaging.loglog/anaconda/storage.loglog/anaconda/ifcfg.loglog/anaconda/ks-script-TOLvJc.loglog/anaconda/ks-script-VRY9yQ.loglog/anaconda/ks-script-pjDijm.loglog/anaconda/journal.loglog/audit/audit.loglog/boot.loglog/boot.log-20200126log/btmplog/btmp-20200126... [root@localhost] # lltotal 604 RWMI. 1 root root 1285 Dec 21 17:19 anaconda-ks.cfgdrwxr-xr-x. 8 root root 4096 Jan 29 23:02 log-rw-r--r--. 1 root root 607566 Jan 30 00:58 logs.tar.gz, this is the end of the study on "the usage of the xargs command in linux". I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.