bash - 如何对 bash 中的文件列表执行多个命令(xargs?)

标签 bash xargs command-substitution process-substitution

我有一个包含文件列表的文件,我想对每个文件执行两个命令。

files.txt 的内容:

file1
file2
file3

我想对每个文件执行的命令是(例如)ls -sdu

我希望输出最终是这样的:

<ls size> <du size> file1
<ls size> <du size> file2
<ls size> <du size> file3

我知道如何使用 bash 脚本执行此操作,但有没有一种方法可以使用进程替换或其他方式轻松地在一行中执行此操作?

我以为我可以做这样的事情,但是没有成功:

cat files.txt | tr '\n' '\0' | xargs -0 -I{} cat <(du {} ) <(ls -s {})

出现了这些错误:

du: cannot access `{}': No such file or directory
ls: cannot access {}: No such file or directory

有办法吗?

最佳答案

您可以使用xargs 运行sh,您可以在其中放置多个命令,例如:

xargs -0 -I{} sh -c 'du {}; ls -s {}'

关于bash - 如何对 bash 中的文件列表执行多个命令(xargs?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40328543/

相关文章:

java - 打开终端/提示符并传递命令 (ping)

shell - 无法通过管道将 docker pid 传递给 bash 命令

bash - 更改 xargs 参数以在每个参数中包含一个标志的方法?

bash - bash局部变量赋值命令替换的退出代码

bash - 在 msys bash 上包含换行符时 PS1 命令替换失败

增加变量的 bash 替换调用

bash - "while read -r line; do ...; done < file` 和 `cat file | while IFS= read -r line; do ...; done` 之间的 bash 有什么区别吗?

arrays - 从 bash 数组中查找并显示唯一值

bash - git add - 当唯一的错误是存在被忽略的文件时,我可以强制退出代码 0 吗?

bash - xargs jar tvf - 不起作用