Unix
命令都带有参数,有些命令可以接受”标准输入(stdin)”作为参数。而管道命令(|)的作用,是将左侧命令的标准输出转换为标准输入,提供给右侧命令作为参数使用。虽然,在 Unix 系统中大多数命令都不接受标准输入作为参数,只能直接在命令行输入参数,这导致无法用管道命令传递参数。比如,我们日常使用的 echo 命令就不接受管道传参。而 xargs 命令的作用,就是将标准输入转为命令行参数。
注意:xargs 后面的默认跟的是 echo 命令,所以它可以单独使用
帮助信息
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
示例:
基本使用:
# 将标准输入转为命令行参数
echo "hello rumenz" | xargs echo
# grep接受管道传参
cat /etc/passwd | grep root
# echo 命令不接受管道传参
echo "hello rumenz" | echo
参数:
-d 指定分隔符,默认使用空格分割
# 空格作为分隔符
echo "one two three" | xargs mkdir
# 指定制表符为分隔符
echo -e "a\tb\tc" | xargs -d "\t" echo
-p 打印出要执行的命令并询问用户是否要执行
echo 'one two three' | xargs -p touch
-0 表示用 null 当作分隔符
find命令有一个特别的参数-print0,用来指定输出的文件列表以null作为分隔符
find /path -type f -print0 | xargs -0 rm
# 指定多少行作为一个命令行参数
xargs -L 1 find -name "*.txt"
-n 指定每次将多少项作为命令行参数
echo {0..9} | xargs -n 2 echo
# 指定每一项命令行参数的替代字符串
# 将命令行参数传给多个命令
cat foo.txt
one
two
three
cat foo.txt | xargs -I {} sh -c 'echo {}; mkdir {}'
one
two
three
ls
one two three
# 将多行输入转换成单行输入
> cat rumenz.txt
1 2 3 4
5 6
7 8 9
> cat rumenz.txt | xargs
1 2 3 4 5 6 7 8 9
# 将单行文本转换成多行
> cat rumenz.txt
1 2 3 4 5 6 7 8 9
> cat rumenz.txt | xargs -n 3
1 2 3
4 5 6
7 8 9
# 指定分隔符进行行转换
> echo "rumenz:123:rumenz:456:rumenz:789" | xargs -d : -n 2
rumenz 123
rumenz 456
rumenz 789
# xargs和find结合
find . -type f -name "*.txt" -print | xargs rm -f
# 批量下载文件
cat rumenz.txt | xargs wget -c