xargs 是一个强大的命令行工具,用于将标准输入数据转换为命令行参数,并执行一个命令。以下是 xargs 的一些详细用法和选项:
xargs 命令的基本格式如下:
command | xargs another_command
这里,command 的输出将作为 another_command 的参数。
-0 或 --null:输入项之间用空字符分隔,而不是默认的空白字符(空格、制表符或换行)。-a file:从文件中读取输入,而不是标准输入。-I replace-str:将输入项替换为 replace-str。这在循环执行命令时非常有用。-i 或 --replace:与 -I 类似,但 replace-str 可以是 {},这是一个特殊的占位符。-L number:每次处理 number 行输入。-l 或 --lines:与 -L 类似。-n number 或 --max-args number:每次执行 another_command 时传递的最大参数数量。-P max-procs 或 --max-procs=max-procs:使用的最大进程数(并行执行)。-r 或 --no-run:不要执行命令,只打印构建的参数。-t 或 --no-terminate:在参数后面不添加换行符。-x 或 --exit:如果 another_command 执行失败,则退出 xargs。-E eof-str 或 --eof=eof-str:当读取到 eof-str 时停止读取输入。-J replace-str 或 --arg-file=replace-str:与 -I 类似,但用于从文件中读取参数。-p:提示用户确认每个参数。rm 命令:echo "file1 file2 file3" | xargs rm
-I 选项循环删除文件:echo "file1 file2 file3" | xargs -I {} rm {}
find . -name "*.txt" -print | xargs -n 1 -P 4 cat
这个命令会找到所有 .txt 文件,并并行(使用 4 个进程)执行 cat 命令。
-0 选项处理以空字符分隔的输入:printf "%s
登录查看全部
参与评论
手机查看
返回顶部