销售推广_唐女士 QQ:1721809125
QQ在线客服

上班时间(星期一至星期五):
10:30-12:00 14:00-17:30

技术推广_江先生 QQ:1477987636
QQ在线客服

上班时间(星期一至星期五):
09:00-12:00 14:00-18:30

基本功能

“xargs” 可实现 “将标准输入,作为命令的参数”。

“xargs” 是一个强有力的命令,它能捕获一个命令的输出,然后传递给另一个命令。

“xargs” 是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。

“xargs” 把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处理。

通常情况下,“xargs” 会从管道或 stdin 中读取数据;但是,“xargs” 也能从文件输出中读取数据。

“xargs” 的默认命令是 echo,这意味着,通过管道传递给 “xargs” 的输入将会包含换行和空白;不过, 通过 “xargs” 的处理,换行和空白会被空格取代。


作用:

“xargs” 可将多行输入,转换为单行 (特殊功效)。

“xargs” 可将输入参数整理后,去除 <newline> 换行符,以一列表形式进行处理。

使用 xargs -n 参数适当控制,可避免参数过长引发的问题;对于经常产生大量输出的命令,比如:find、locate 和 grep 来说非常有用。


譬如:

cho "--help" | xargs ca

输出内容如下:

Usage: cat [OPTION]... [FILE]...

Concatenate FILE(s), or standard input, to standard output.

&nb

A, --show-all                     equivalent to -vET

b, --number-nonblank    number nonempty output lines, overr

                                         equivalent to -vE

E, --show-ends               display $ at end of each

, --number                      number all outpu

, --squeeze-blank         suppress repeated empty outpu

                                          equivalent to -vT

T, --show-tabs                 display TAB characters as ^I

u                                         (ignored)

v, --show-nonprinting     use ^ and M- notation, except for LFD and TAB

     --help                             display this help a

     --version                        output version information a

&nb

With no FILE, or when FILE is -, read standard input.

&nb

Exa

cat f - g Output f's contents, then standard input, then g's contents.

cat Copy standard input to standard output.

&nb

Report cat bugs to bug-coreutils@gnu.org

GNU coreutils home page: <http://www.gnu.org/software/coreutils/&g

General help using GNU software: <http://www.gnu.org/gethelp/&g

For complete documentation, run: info coreutils 'cat invocation'


管道可实现 “将前面的标准输出,作为后面的标准输入”。

譬如:

cho "--help" | ca

输出内容如下:

--help

基本语法

Usage: xargs [-0prtx] [--interactive] [--null] [-d|--delimiter=delim]

[-E eof-str] [-e[eof-str]] [--eof[=eof-str]]

[-L max-lines] [-l[max-lines]] [--max-lines[=max-lines]]

[-I replace-str] [-i[replace-str]] [--replace[=replace-str]]

[-n max-args] [--max-args=max-args]

[-s max-chars] [--max-chars=max-chars]

[-P max-procs] [--max-procs=max-procs] [--show-limits]

[--verbose] [--exit] [--no-run-if-empty] [--arg-file=file]

[--version] [--help] [command [initial-arguments]]


譬如:

command | xargs -item comma

不带 command 时,默认使用 echo 输出

Option 参数

# 参数 EN 解释 中文翻译 备注
01 a file, --arg-file=fi Read items from file instead of standard input. If you use this option, stdin remains unchanged when commands are run. Otherwise, stdin is redirected from /dev/null. 从文件读取项,而不是 stdin 标准输入。若您使用此选项,stdin 标准输入会保持不变,当运行命令时。否则,stdin 标准输入会自 /dev/null 重定向。
02 0, Input items are terminated by a null character instead of by whitespace, and the quotes and back‐slash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode. 通过 null 字符终止输入项,而不是空格;且引号、反-斜杠 (\) 并不特殊 (每个字符取字面意思)。会禁用像对待任何其他自变量的文件字符串结尾。当输入项可能包含空格、引号或反斜杠时,会很有用。GNU 发现 -print0 选项产生的输入,适合这种模式。
03 im,--delimiter=delim Input items are terminated by the specified character. Quotes and backslash are not special; every character in the input is taken literally. Disables the end-of-file string, which is treated like any other argument. This can be used when the input consists of simply newline-separated items, al‐though it is almost always better to design your program to use --null where this is possible. The specified delimiter may be a single character, a C-style character escape such as \n, or an octal or hexadecimal escape code. Octal and hexadecimal escape codes are understood as for the printf com‐mand. Multibyte characters are not supported.
04 E eof Set the end of file string to eof-str. If the end of file string occurs as a line of input, the rest of the input is ignored. If neither -E nor -e is used, no end of file string is used.
05 [eof-str], --eof[=eof-str] This option is a synonym for the -E option. Use -E instead, because it is POSIX compliant while this option is not. If eof-str is omitted, there is no end of file string. If neither -E nor -e is used, no end of file string is used.
06 p Print a summary of the options to xargs and exit. 打印 xargs 选项汇总并退出。
07 I repla Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. Implies -x and -L 1.
08 [replace-str], --replace[=replace-str] This option is a synonym for -Ireplace-str if replace-str is specified, and for -I{} otherwise. This option is deprecated; use -I instead.
09 L max-lin Use at most max-lines nonblank input lines per command line. Trailing blanks cause an input line to be logically continued on the next input line. Implies -x.
10 [max-lines], --max-lines[=max-lines] Synonym for the -L option. Unlike -L, the max-lines argument is optional. If max-lines is not specified, it defaults to one. The -l option is deprecated since the POSIX standard specifies -L instead.
11 n max-args, --max-args=max-arg Use at most max-args arguments per command line. Fewer than max-args arguments will be used if the size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit.
12 p, --interactiv Prompt the user about whether to run each command line and read a line from the terminal. Only run the command line if the response starts with `y' or `Y'. Implies -t.
13 , --no-run-if-emp If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input. This option is a GNU extension.
14 max-chars, --max-chars=max Use at most max-chars characters per command line, including the command and initial-arguments and the terminating nulls at the ends of the argument strings. The largest allowed value is system-de‐pendent, and is calculated as the argument length limit for exec, less the size of your environment, less 2048 bytes of headroom. If this value is more than 128KiB, 128Kib is used as the default val‐ue; otherwise, the default value is the maximum. 1KiB is 1024 bytes.
15 , --v Print the command line on the standard error output before executing it. 执行前,打印标准错误输出命令行。
16 v Print the version number of xargs and exit. 打印 xargs 版本号并退出。
17 how-limi Display the limits on the command-line length which are imposed by the operating system, xargs' choice of buffer size and the -s option. Pipe the input from /dev/null (and perhaps specify --no-run-if-empty) if you don't want xargs to do anything.
18 x, --ex Exit if the size (see the -s option) is exceeded.
19 P max-procs, --max-procs=max-p Run up to max-procs processes at a time; the default is 1. If max-procs is 0, xargs will run as many processes as possible at a time. Use the -n option with -P; otherwise chances are that only one exec will be done.

详细用法


功能 # 命令规范 文字解释 示例 示例解释
01 find /tmp -name core -type f -print | xargs /bin/rm -f ind files named core in or below the directory /tmp and delete them. Note that this will work incorrectly if there are any filenames containing newlines or spaces.
01 find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f Find files named core in or below the directory /tmp and delete them, processing filenames in such a way that file or directory names containing spaces or newlines are correctly handled.
01 find /tmp -depth -name core -type f Find files named core in or below the directory /tmp and delete them, but more efficiently than in the pre‐vious example (because we avoid the need to use fork(2) and exec(2) to launch rm and we don't need the ex‐tra xargs process).
01 f1 < /etc/passwd | sort | x Generates a compact listing of all the users on the system.
01 xargs sh -c 'emacs "$@" < /dev/tty' ema Launches the minimum number of copies of Emacs needed, one after the other, to edit the files listed on xargs' standard input. This example achieves the same effect as BSD's -o option, but in a more flexible and portable way.
删除文件 01 find / -name filename | xargs rm -rf 彻底清洁移除 filenam find / -name nginx | xargs rm -rf 彻底清洁移除 nginx
查看帮助 01 xargs --help 查看 xargs 摘要帮助信息
查看版本 01 xargs --v 查看 xargs 版本号、版本声明、许可协议、作者等信息