【shell命令】xargs的常用法

$ xargs --help

Usage: xargs [OPTION]... COMMAND [INITIAL-ARGS]...
Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.

Mandatory and optional arguments to long options are also
mandatory or optional for the corresponding short option.
  -0, --null                   items are separated by a null, not whitespace;
                                 disables quote and backslash processing and
                                 logical EOF processing
                                 【表示禁用空白符转义,即换行符、制表符等都将显示为相应的字符】
  -a, --arg-file=FILE          read arguments from FILE, not standard input
                                 【从指定文件FILE中读取xargs的参数】
  -d, --delimiter=CHARACTER    items in input stream are separated by CHARACTER,
                                 not by whitespace; disables quote and backslash
                                 processing and logical EOF processing
                                 【将分隔符由空格替换为指定的字符,如':'';'等;且禁用空白符转义】
  -E END                       set logical EOF string; if END occurs as a line
                                 of input, the rest of the input is ignored
                                 (ignored if -0 or -d was specified)
                                 【设置文件结束符:如果接收到一个只有文件结束符的一行,则忽略之后的内容】
  -e, --eof[=END]              equivalent to -E END if END is specified;
                                 otherwise, there is no end-of-file string
                                 【当指定END参数时同-E EOF选项;如果不指定END,则不设置文件结束符】
  -I R                         same as --replace=R
  -i, --replace[=R]            replace R in INITIAL-ARGS with names read
                                 from standard input; if R is unspecified,
                                 assume {}
                                 【用R替换标准输入的内容;如果不指定R则用{}替换】
  -L, --max-lines=MAX-LINES    use at most MAX-LINES non-blank input lines per
                                 command line
  -l[MAX-LINES]                similar to -L but defaults to at most one non-
                                 blank input line if MAX-LINES is not specified
  -n, --max-args=MAX-ARGS      use at most MAX-ARGS arguments per command line
                               【指定每个命令行最大参数个数:就是把所有参数列表切片分组】
  -P, --max-procs=MAX-PROCS    run at most MAX-PROCS processes at a time
                               【指定最大进程数】
  -p, --interactive            prompt before running commands
                               【运行命令前提示、交互】
      --process-slot-var=VAR   set environment variable VAR in child processes
                               【在子进程中设置变量VAR】
  -r, --no-run-if-empty        if there are no arguments, then do not run COMMAND;
                                 if this option is not given, COMMAND will be
                                 run at least once
  -s, --max-chars=MAX-CHARS    limit length of command line to MAX-CHARS
      --show-limits            show limits on command-line length
  -t, --verbose                print commands before executing them
  -x, --exit                   exit if the size (see -s) is exceeded
      --help                   display this help and exit
      --version                output version information and exit

以上是xargs的help文档。

  • 1.命令参数和标准输入/输出
    命令选项/参数:跟在命令后面的选项/参数,告诉命令‘做什么’以及‘怎么做’
  • 2.标准输入输出
    部分命令的参数可以由管道/文件输入,其内容即使命令的处理对象

一. 不带参数的xargs

其常跟在管道符后使用,且不带参数:

e.g. 找到当前目录下的所有.txt文本并用cat查看
$ find . -name '*.txt'|xargs cat
xargs仅仅是把find到的所有路径一个个追加到cat命令后面当参数

二. xargs -0

表示禁用空白符转义,即换行符、制表符等都将显示为相应的字符:
即’\n’、’\t’不会处理为换行和制表,直接显示’\n’、’\t’

三. xargs -i

用{}替换标准输入的内容

e.g. 找到所有.sh脚本,全部用bash执行
find . -name '*.sh'|xargs -i bash {}
相当于bash执行所有的.sh脚本(这里不是单纯地将所有.sh脚本放在bash后面,而是对每个.sh脚本分别使用bash执行;bash a.sh b.sh c.sh 只会执行a.sh !!),很好用的功能

四. xargs -P

指定最大进程数,当有多个任务时,会启动多进程,进程数最大数由-P参数指定。也是shell下实现多进程/多线程的一种方式。

猜你喜欢

转载自blog.csdn.net/u013390088/article/details/80734513