Use the linux command xargs

REVIEW xargs is a command passed a filter parameter data can be converted into a pipe or standard input parameters, the default command is echo, which means that the input piped to xargs will contain line, but by a process xargs, In other lines will be substituted with spaces.

How to use the xargs command

grammar:

xargs [OPTIONS] [COMMAND [initial-arguments]]

As an example: We symbol transmission pipelines to xargs, and run the touch command for each parameter, -t represents the first print before you perform, create three files:

[root@localhost ~]# echo "file1 file2 file3"|xargs -t touch
touch file1 file2 file3

How to limit the number of parameters

By default, the command of the number of arguments passed to the limit determined by the system. -n option to specify the number of arguments passed to the command. xargs many times as needed to run the specified command until all parameters are exhausted.

The following example specifies each pass a parameter:

[root@localhost ~]# echo "file1 file2 file3"|xargs -n1 -t touch
touch file1 
touch file2 
touch file3

How to run multiple commands

To use xargs to run multiple commands, use the -i or -I option. In -i -I or pass parameters back a custom symbol, all matching items are to replace the parameters passed to xargs.

xargs to run the following two commands When the example, the first touch to create a file, then ls to list:

[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 option or --delimiter set custom delimiter can be a single character, it can also be based on \ the beginning of the escape character.

The following example using the # delimiters, echo command with the -n option, meaning not output a new line:

[root@localhost ~]# echo -n file1#file2#file3#file4|xargs -d \# -t touch
touch file1 file2 file3 file4

How to read entries from a file

xargs command can also read from a file entry, instead of reading the entries from the standard input. -A option, followed by the file name.
Ip.txt create a file, one would use each address xargs command ping inside:

[root@localhost ~]# cat ip.txt 
114.114.114.114
www.linuxprobe.com
202.102.128.68

Use -L 1 option, which represents a xargs reads one line. If this option is omitted, xargs will all ip passed to a ping command.

[root@localhost ~]# xargs -a ip.txt -t -L 1 ping -c 1
ping -c 1 114.114.114.114 
PING 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 0ms
rtt min/avg/max/mdev = 11.026/11.026/11.026/0.000 ms
ping -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 0ms
rtt min/avg/max/mdev = 20.934/20.934/20.934/0.000 ms
ping -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: icmp_seq=1 ttl=83 time=8.71 ms

--- 202.102.128.68 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 8.710/8.710/8.710/0.000 ms

Used in conjunction with xargs find

xargs often used in conjunction with a find command. You can find search for a specific file, and then perform operations on these files xargs.

To avoid line breaks or other special characters in the file name problem, always use -print0 option to find, so you can make the find print the full file name, use -0 with xargs command or option to the correct interpretation --null .

The following example, find the folder log the following file types for all the files, compression packing up:

[root@localhost ~]# find log/ -type f -print0|xargs --null tar -zcvf logs.tar.gz
log/anaconda/anaconda.log
log/anaconda/syslog
log/anaconda/program.log
log/anaconda/packaging.log
log/anaconda/storage.log
log/anaconda/ifcfg.log
log/anaconda/ks-script-TOLvJc.log
log/anaconda/ks-script-VRY9yQ.log
log/anaconda/ks-script-pjDijm.log
log/anaconda/journal.log
log/audit/audit.log
log/boot.log
log/boot.log-20200126
log/btmp
log/btmp-20200126
…
[root@localhost ~]# ll
total 604
-rw-------. 1 root root   1285 Dec 21 17:19 anaconda-ks.cfg
drwxr-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

to sum up

The reason to use this command, the key is because many commands are not supported pipeline to pass arguments, xargs can solve this problem, but also very easy to use, with powerful energy. Linux in respect of such a school

Published 10 original articles · won praise 38 · views 180 000 +

Guess you like

Origin blog.csdn.net/Linuxprobe18/article/details/104155744