Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the xargs command

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use the xargs command, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Xargs is a filter for passing parameters to commands and a tool for combining multiple commands. It divides a data stream into blocks small enough to facilitate filter and command processing. Here's how to use the xargs command.

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 file1touch file2touch 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.txt114.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.114PING 114.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.comPING 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.68PING 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 msxargs 8.710 msxargs 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 thank you for reading this article carefully. I hope the article "how to use xargs commands" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report