首页 Linux xargs 命令
文章
取消

Linux xargs 命令

xargs 命令可以读取标准输入和管道中的数据,默认以空格、Tab、回车作为分隔符和结束符。

xargs 命令后面的默认命令是 echo。

我们先简要看下用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@centos uphie]# xargs --help
Usage: xargs [OPTION]... COMMAND INITIAL-ARGS...
Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.

Mandatory arguments to long options are mandatory for short options too.
Non-mandatory arguments are indicated by [square brackets]
  -0, --null                   Items are separated by a null, not whitespace.
                               Disables quote and backslash processing
  -a, --arg-file=FILE          Read arguments from FILE, not standard input
  -d, --delimiter=CHARACTER    Input items are separated by CHARACTER, not by
                               blank space. Disables quote and backslash
                               processing
  -E END                       If END occurs as a line of input, the rest of
                               the input is ignored.
  -e [END], --eof[=END]        Equivalent to -E END if END is specified.
                               Otherwise, there is no end-of-file string
  --help                       Print a summary of the options to xargs.
  -I R                         same as --replace=R (R must be specified)
  -i,--replace=[R]             Replace R in initial arguments with names
                               read from standard input. If R is
                               unspecified, assume {}
  -L,-l, --max-lines=MAX-LINES Use at most MAX-LINES nonblank input lines per
                               command line
  -l                           Use at most one nonblank input line per
                               command line
  -n, --max-args=MAX-ARGS      Use at most MAX-ARGS arguments per command
                               line
  -P, --max-procs=MAX-PROCS    Run up to max-procs processes at a time
  -p, --interactive            Prompt before running commands
  --process-slot-var=VAR       Set environment variable VAR in child
                               processes
  -r, --no-run-if-empty        If there are no arguments, run no command.
                               If this option is not given, COMMAND will be
                               run at least once.
  -s, --max-chars=MAX-CHARS    Limit commands to MAX-CHARS at most
  --show-limits                Show limits on command-line length.
  -t, --verbose                Print commands before executing them
  --version                    Print the version number
  -x, --exit                   Exit if the size (see -s) is exceeded

将错误报告发送到 <bug-findutils@gnu.org>

选项

-d

指定分隔符。

1
[root@centos uphie]# echo "dir1:dir2:dir3" | xargs -d ':' mkdir

-0

指定 NULL 而不是空格、Tab、回车来作为分隔符和结束符。

当我们想删除一个名称带空格的文件,由于名称中有空格,rm 当成了两个文件,但文件不存在,于是删除不成功。

1
2
3
4
5
[root@centos uphie]# find ./ -name '*hao.txt'
./ni hao.txt
[root@centos uphie]# find ./ -name '*hao.txt' | xargs rm -f
[root@centos uphie]# find ./ -name '*hao.txt' | xargs echo
./ni hao.txt

对于这种情况我们可以为 find 增加一个 print0 选项,设置在输出的文件名后自动添加一个 NULL 来替代换行符,然后 xargs 使用 -0 参数指定使用 NULL 而不是空格、Tab、回车作为分隔符和结束符:

1
2
3
[root@centos uphie]# find ./ -name '*hao.txt' -print0 | xargs -0 rm
[root@centos uphie]# find ./ -name '*hao.txt' | xargs echo

-I/–replace=[R]

如果 xargs 要将命令行参数传给多个命令,可以使用 -I 参数,指定每一项命令行参数的替代字符串。

用法 :-I replacement,replacement 为要替换的字符串。

对当前目录下所有 Python 脚本查看 md5 值和内容:

1
2
3
4
5
6
[root@centos uphie]#  ll *.py | awk '{print $9}' | xargs -I py /bin/bash -c 'md5sum py;cat py'
c8a007ca8df7467a2fc63ef3804814a2  hello.py
print('hello world!')
f675422bbfde5b5b040900209621d589  sleep.py
import time
time.sleep(1000)

-L/–max-lines=MAX-LINES

等同于 --max-lines

如果标准输入中包含多行内容,那么可以使用多少行来作为下一个命令的参数。

指定一行作为一个参数:

1
2
3
4
[root@centos uphie]# echo  -e "1.txt\n2.txt\n3.txt" | xargs -L 1 -t touch
touch 1.txt
touch 2.txt
touch 3.txt

-n num

指定一次读取几个输入

1
2
3
4
5
6
7
8
9
10
11
[root@centos uphie]# ll *.txt|awk '{print $9}'
animal.txt
data.txt
fruit.txt
phones.txt
[root@centos uphie]# ll *.txt|awk '{print $9}' | xargs -n 2
animal.txt data.txt
fruit.txt phones.txt
[root@centos uphie]# ll *.txt|awk '{print $9}' | xargs -n 3
animal.txt data.txt fruit.txt
phones.txt

-p

对要执行的命令进行显示,并要求用户进行确认。

1
2
3
4
[root@centos uphie]# echo grades.csv | xargs -p rm
rm grades.csv ?...n
[root@centos uphie]# ll grades.csv
-rw-r--r--. 1 root root 95 2月  13 10:25 grades.csv

-t

显示要执行的命令,不需要用户进行确认。

1
2
3
4
[root@centos uphie]# echo grades.csv | xargs -t rm
rm grades.csv
[root@centos uphie]# ll grades.csv
ls: 无法访问grades.csv: 没有那个文件或目录

–max-procs

xargs 默认使用一个进程执行命令,如果要执行多次需要等上一次执行完。

--max-procs n 指定使用 n 个进程并发执行,n 为0 时表示不限制进程数,会以最快的方式执行。

1
[root@centos uphie]# docker ps -q | xargs -n 1 --max-procs 0 docker kill
本文由作者按照 CC BY 4.0 进行授权