# xargs

**xargs** 是 Unix 的一个命令行工具，它从标准输入读取以空白或换行分隔的条目，并以这些条目作为参数，一次或多次执行指定的命令（默认为 echo），每次调用时在系统命令行长度限制允许的范围内尽可能多地批量传入条目。<sup>[1](https://manpages.debian.org/testing/findutils/xargs.1.en.html)</sup> 它最常用于将 `find` 等程序生成的包含大量文件名的列表传递给接受文件名作为参数的命令；它由 POSIX 标准化，并与 `find` 和 `locate` 一同包含在 GNU findutils 中。<sup>[2](https://pubs.opengroup.org/onlinepubs/009695399/utilities/xargs.html)</sup><sup> • </sup><sup>[3](https://lists.gnu.org/archive/html/bug-findutils/2024-06/msg00017.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 用途 | 从标准输入构建并执行命令行；命令行构建到系统定义的上限，因此命令调用次数通常远少于输入条目数<sup>[1](https://manpages.debian.org/testing/findutils/xargs.1.en.html)</sup> |
| 长度限制 | POSIX 要求命令调用时参数列表与环境变量列表的总长度不超过 {ARG_MAX} − 2048 字节<sup>[2](https://pubs.opengroup.org/onlinepubs/009695399/utilities/xargs.html)</sup> |
| 安全文件名 | `-0` 选项接受以空字符结尾的条目，并将引号和反斜杠按字面处理；GNU `find -print0` 以此形式生成输入<sup>[1](https://manpages.debian.org/testing/findutils/xargs.1.en.html)</sup> |
| 并行 | `-P max-procs` 同时运行最多 max-procs 个进程（默认为 1）；0 表示尽可能多，可在运行时通过 SIGUSR1 和 SIGUSR2 调整<sup>[1](https://manpages.debian.org/testing/findutils/xargs.1.en.html)</sup> |
| 退出状态 | GNU xargs 成功时退出码为 0；任一调用退出码为 1–125 时退出码为 123；退出状态为 255 时退出码为 124；被信号杀死时退出码为 125；命令无法运行时退出码为 126；命令未找到时退出码为 127<sup>[1](https://manpages.debian.org/testing/findutils/xargs.1.en.html)</sup> |
| 起源 | 由赫布·格利斯（Herb Gellis）在贝尔实验室（Bell Labs）发明；该命令出现在 PWB Unix 中<sup>[1](https://manpages.debian.org/testing/findutils/xargs.1.en.html)</sup><sup> • </sup><sup>[4](https://man.openbsd.com/xargs.1)</sup> |
| 标准化 | `find -print0` 和 `xargs -0` 自第 8 版（IEEE Std 1003.1-2024）起符合 POSIX<sup>[5](https://www.gnu.org/software/findutils/manual/html_node/find_html/Safe-File-Name-Handling.html)</sup> |

## 工作原理

xargs 从标准输入读取条目，并构造命令行，命令行由所命名的工具、xargs 命令行上给出的初始参数，以及在当前长度和数量约束内尽可能多的输入条目组成。该工具会被调用所需次数以消费全部输入；除非请求并行执行，否则 xargs 在每次调用完成后才启动下一次。<sup>[1](https://manpages.debian.org/testing/findutils/xargs.1.en.html)</sup><sup> • </sup><sup>[6](https://manpages.ubuntu.com/manpages/noble/man1/xargs.1posix.html)</sup>

批量处理正是关键所在。由于每次调用命令都会创建一个新进程，对于长列表而言逐条目运行命令代价高昂；xargs 则改为在每次调用中尽可能多地打包条目，直到操作系统的上限。POSIX 要求 xargs 在 {ARG_MAX} 之下留出 2048 字节的余量——{ARG_MAX} 是 `exec` 系列函数所能接受的参数列表与环境变量合计大小的最大值——以便被调用的命令仍有空间修改其环境并运行其他工具。<sup>[2](https://pubs.opengroup.org/onlinepubs/009695399/utilities/xargs.html)</sup><sup> • </sup><sup>[6](https://manpages.ubuntu.com/manpages/noble/man1/xargs.1posix.html)</sup> GNU xargs 将其默认缓冲区计算为 exec 的参数长度限制减去环境变量大小再减去 2048 字节，上限为 128 KiB；`--show-limits` 选项可显示当前生效的各项限制。<sup>[1](https://manpages.debian.org/testing/findutils/xargs.1.en.html)</sup>

若干选项控制批次的形态：`-n max-args` 限制每条命令行的参数个数，`-s max-chars` 限制命令行的字符长度，`-L max-lines` 限制每条命令行对应的非空输入行数。`-I replace-str` 选项则把每个输入行放入初始参数中替换字符串出现的位置，每行运行一次命令；它隐含 `-x` 和 `-L 1`。<sup>[1](https://manpages.debian.org/testing/findutils/xargs.1.en.html)</sup> `-r`（`--no-run-if-empty`）选项抑制默认行为——即使输入为空也运行一次命令。<sup>[1](https://manpages.debian.org/testing/findutils/xargs.1.en.html)</sup>

## 文件名与 -0 选项

默认的输入解析将空白视为分隔符，并赋予引号、反斜杠和尾部空白以特殊含义。由于 Unix 文件名可以包含空白和换行，这种默认处理会错误地处理此类文件名，GNU findutils 手册将由此产生的不匹配描述为 POSIX 规定的默认行为不可避免的后果。<sup>[1](https://manpages.debian.org/testing/findutils/xargs.1.en.html)</sup><sup> • </sup><sup>[7](https://www.gnu.org/software/findutils/manual/html_node/find_html/Security-Considerations-for-xargs.html)</sup>

标准的补救办法是搭配两个选项：`find -print0` 在每个输出的文件名后加上空字符作为终止符，而 `xargs -0` 期望以[空字符](https://www.edgepedia.org/zh/null-character)结尾的条目，将所有字符按字面处理，并禁用文件结束字符串。一条典型的安全管道是：

```
find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing
```

这里的 `-r` 防止在 `find` 无输出时运行 `grep`。<sup>[8](https://www.gnu.org/software/findutils/manual/html_node/find_html/Contents.html)</sup> `find -print0` 和 `xargs -0` 均自该标准第 8 版（IEEE Std 1003.1-2024）起符合 POSIX；在该版之前，POSIX 标准并不要求它们。<sup>[5](https://www.gnu.org/software/findutils/manual/html_node/find_html/Safe-File-Name-Handling.html)</sup><sup> • </sup><sup>[7](https://www.gnu.org/software/findutils/manual/html_node/find_html/Security-Considerations-for-xargs.html)</sup> GNU xargs 还提供 `-d delim` 以选择任意单字符分隔符，例如用 `\n` 处理以换行分隔的输入，不过手册建议尽可能让生成方采用空字符分隔。<sup>[1](https://manpages.debian.org/testing/findutils/xargs.1.en.html)</sup>

## 并行执行

`-P max-procs` 选项同时运行最多 max-procs 个命令调用；值为 0 时运行尽可能多的进程。由于 xargs 在其他情况下一次只运行一条命令，`-P` 通常与 `-n` 或 `-L` 配合使用，以便将输入分配给各并发调用。运行期间，SIGUSR1 增加并发命令数，SIGUSR2 减少并发命令数，上限为实现定义的上限，可通过 `--show-limits` 查看；当被要求减少时，xargs 从不终止正在运行的命令，并且在退出前等待所有子进程结束。<sup>[1](https://manpages.debian.org/testing/findutils/xargs.1.en.html)</sup>

并行输出需要小心处理：对共享资源的访问管理由被调用的进程自行负责，多个进程写入标准输出的内容可能相互混杂，除非它们进行协调，例如使用锁或写入各自独立的文件。<sup>[1](https://manpages.debian.org/testing/findutils/xargs.1.en.html)</sup>

## 与 find -exec 及 GNU Parallel 的比较

`find -exec command {} +` 像 xargs 一样将参数批量合并为一次调用，两者在正确使用时速度大致相当；而 `find -exec command {} \;` 则对每个文件运行一次命令，对于长列表而言要慢得多。<sup>[9](https://techearl.com/find-exec-vs-xargs)</sup> GNU findutils 手册将 `find -exec` 以及（在可用时）`find -execdir` 列为替代方案，可避免 xargs 的文件名解析问题和安全问题。<sup>[7](https://www.gnu.org/software/findutils/manual/html_node/find_html/Security-Considerations-for-xargs.html)</sup>

GNU Parallel 被设计为接受与 xargs 相同的选项，并将自身定位为替代品：它保证并行作业的输出不会交错，可根据 CPU 核心数设定并行度，并更稳健地处理输入中的特殊字符；而 xargs 仅支持指定作业数的并行，且不会对并发进程的输出进行分组。<sup>[10](https://www.gnu.org/software/parallel/parallel_alternatives.html)</sup>

## 安全考量

GNU 手册指出，xargs 无法做到完全安全：从输入文件列表的生成到 xargs 发出的命令实际使用这些文件之间，始终存在时间差，能够访问系统的其他用户可在此期间操纵文件系统，使命令作用于用户本不打算操作的文件。建议的缓解措施是使用 `find -execdir`，或将 `-0` 与空字符分隔的输入配合使用。<sup>[1](https://manpages.debian.org/testing/findutils/xargs.1.en.html)</sup><sup> • </sup><sup>[7](https://www.gnu.org/software/findutils/manual/html_node/find_html/Security-Considerations-for-xargs.html)</sup>

## 标准与版本

POSIX 规范将 xargs 追溯至该标准的第 2 版，并要求遵守 {ARG_MAX} − 2048 字节的限制，支持 `-E`、`-I`、`-L`、`-n`、`-p`、`-s`、`-t` 和 `-x` 选项。<sup>[2](https://pubs.opengroup.org/onlinepubs/009695399/utilities/xargs.html)</sup><sup> • </sup><sup>[6](https://manpages.ubuntu.com/manpages/noble/man1/xargs.1posix.html)</sup> 较旧的 `-l` 和 `-i` 选项出现在 1997 年版 POSIX 中，但在 2004 年版中被删除，因此 GNU xargs 引导用户改用 `-L` 和 `-I`；自 GNU xargs 4.2.9 起，默认不设逻辑文件结束标记，这是 POSIX 所允许的。<sup>[1](https://manpages.debian.org/testing/findutils/xargs.1.en.html)</sup> GNU xargs 在 findutils 中分发，其稳定版 4.10.0 于 2024 年 6 月 1 日发布。<sup>[3](https://lists.gnu.org/archive/html/bug-findutils/2024-06/msg00017.html)</sup> BSD 实现（如 OpenBSD 的实现）遵循 IEEE Std 1003.1-2024，并采用 GNU xargs 对退出值 123、124 和 125 的含义。<sup>[4](https://man.openbsd.com/xargs.1)</sup>

## References

1. [xargs(1) — findutils — Debian Manpages](https://manpages.debian.org/testing/findutils/xargs.1.en.html)
2. [xargs — The Open Group Base Specifications (POSIX)](https://pubs.opengroup.org/onlinepubs/009695399/utilities/xargs.html)
3. [GNU findutils 4.10.0 released, bug-findutils mailing list](https://lists.gnu.org/archive/html/bug-findutils/2024-06/msg00017.html)
4. [xargs(1) — OpenBSD manual pages](https://man.openbsd.com/xargs.1)
5. [Safe File Name Handling, GNU Findutils manual](https://www.gnu.org/software/findutils/manual/html_node/find_html/Safe-File-Name-Handling.html)
6. [Ubuntu Manpage: xargs - construct argument lists and invoke utility (POSIX.1-2017)](https://manpages.ubuntu.com/manpages/noble/man1/xargs.1posix.html)
7. [Security Considerations for xargs, GNU Findutils manual](https://www.gnu.org/software/findutils/manual/html_node/find_html/Security-Considerations-for-xargs.html)
8. [Contents, GNU Findutils manual](https://www.gnu.org/software/findutils/manual/html_node/find_html/Contents.html)
9. [find -exec vs xargs: When to Use Each, techearl.com](https://techearl.com/find-exec-vs-xargs)
10. [DIFFERENCES BETWEEN xargs AND GNU Parallel, GNU Parallel documentation](https://www.gnu.org/software/parallel/parallel_alternatives.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Build, deployment, and development tooling

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/xargs

English original: https://www.edgepedia.org/xargs
