使用Bash,如何遍历指定文件夹中的文件夹,查找指定文件类型的所有文件,每次找到文件时,获取文件名和完整文件路径的完整文件路径,不带文件名作为变量,并将它们传递给另一个Bash脚本,执行它,并继续搜索下一个文件?
unw*_*ind 16
假设一个GNU查找(这不是不合理的)你可以使用find来做到这一点:
find /path -type f -name '*.ext' -exec my_cool_script \{\} \;
Run Code Online (Sandbox Code Playgroud)
找到就是办法。使用 xargs 处理文件/目录的长列表。此外,为了正确处理带有空格和类似问题的名称,我发现的最好的 find 行命令是:
find ${directory} -name "${pattern}" -print0 | xargs -0 ${my_command}
Run Code Online (Sandbox Code Playgroud)
诀窍是 find -print0 与 xargs -0 兼容:它将结束行替换为 '\0' 以正确处理空格和转义字符。当您的文件列表太长时,使用 xargs 可以为您节省一些“行太长”的消息。
您可以将 xargs 与 --no-run-if-empty 一起使用来处理空列表,并使用 --replace 来管理复杂的命令。
看起来很像家庭作业。
find /path -type f -name "*.ext" -printf "%p:%h\n" | while IFS=: read a b
do
# execute your bash script here
done
Run Code Online (Sandbox Code Playgroud)
阅读 find 的手册页以获取更多 printf 选项......
| 归档时间: |
|
| 查看次数: |
17415 次 |
| 最近记录: |