Ubuntu xargs 命令详解


xargs 可将标准输入 (文件或之前命令的输出), 作为之后命令的输入参数。

xargs 可将多行输入, 转换为单行。

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

xargs -n 可避免参数过长引发异常, 如 find locate grep。

另请参阅: find     locate     updatedb

基本用法     详细用法

cho --help | xargs cat

who --help | cat

基本语法

xargs [OPTION]... COMMAND [INITIAL-ARGS]...

注意: 长选项强制 可选参数, 对于短选项也是强制或可选的。

采用自变量 INITIAL-ARGS 运行 COMMAND, 并从输入读取更多自变量。

譬如: command | xargs -item command

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

OPTION 选项

参数 EN 解释 中文翻译 备注
-0, --null

items are separated by a null, not whitespace;

disables quote and backslash processing and logical EOF processing

项由 null 分隔,非空格;

禁用引号 反斜杠处理及逻辑 EOF (文件结束) 处理

-a, --arg-file=FILE read arguments from FILE, not standard input 从 FILE 读取自变量,非标准输入
-d, --delimiter=CHARACTER

items in input stream are separated by CHARACTER, not by whitespace;

disables quote and backslash processing and logical EOF processing

输入流中的项由 CHARACTER 分隔,非空格;

禁用引号 反斜杠处理及逻辑 EOF (文件结束) 处理

-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)

设置逻辑 EOF (文件结束) 字符串;

若 END 作为输入行出现, 忽略其余输入

(忽略,若 -0 或 -d 被指定)

-e, --eof[=END]

equivalent to -E END if END is specified;

otherwise, there is no end-of-file string

相当于 -E END,若 END 被指定;

否则,没有 EOF (文件结束) 字符串

-I R same as --replace=R 如同 --replace=R
-i, --replace[=R]

replace R in INITIAL-ARGS with names read from standard input;

if R is unspecified, assume {}

采用从标准输入读取的名称, 替换 INITIAL-ARGS 中的 R;

若 R 未指定,则假定 {}

-L, --max-lines=MAX-LINES use at most MAX-LINES non-blank input lines per command line 每命令行最多使用 MAX-LINES 非空白输入行
-l[MAX-LINES]

similar to -L;

but defaults to at most one non-blank input line,

if MAX-LINES is not specified

类似 -L;

但默认最多一非空白输入行,

若 MAX-LINES 未被指定

-n, --max-args=MAX-ARGS use at most MAX-ARGS arguments per command line 每命令行最多使用 MAX-ARGS 自变量
-o, --open-tty

Reopen stdin as /dev/tty in the child process,

before executing the command;

useful to run an interactive application.

在子级进程中以 /dev/tty 重新打开 stdin (标准输入),

在执行命令前;

对运行交互式应用程序会很有用。

-P, --max-procs=MAX-PROCS run at most MAX-PROCS processes at a time 每次最多运行 MAX-PROCS 进程
-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

如果没有自变量,

则不运行 COMMAND;

如果此选项不给定,

COMMAND 将至少运行一次

-s, --max-chars=MAX-CHARS limit length of command line to MAX-CHARS 把命令行长度限制为 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 退出,若超出大小 (见 -s)
--help display this help and exit 显示此帮助并退出
--version output version information and exit 输出版本信息并退出

基本用法

功能 命令 文字解释 示例 示例解释
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.
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.
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).
f1 < /etc/passwd | sort | x Generates a compact listing of all the users on the system.
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.
清零 find / -name filename | xargs rm -rf 彻底清洁移除 filename find / -name nginx | xargs rm -rf 彻底清洁移除 nginx
帮助 xargs --help 展示帮助信息
版本 xargs --version 展示版本信息