有人可以提供代码来执行以下操作: 假设有一个文件目录,所有这些文件都需要通过一个程序来运行。该程序将结果输出到标准输出。我需要一个脚本,它将进入一个目录,对每个文件执行命令,并将输出连接到一个大输出文件中。
例如,要在 1 个文件上运行命令:
$ cmd [option] [filename] > results.out
最佳答案
以下 bash 代码会将 $file 传递给命令,其中 $file 将代表/dir 中的每个文件
for file in /dir/*
do
cmd [option] "$file" >> results.out
done
例子
el@defiant ~/foo $ touch foo.txt bar.txt baz.txt
el@defiant ~/foo $ for i in *.txt; do echo "hello $i"; done
hello bar.txt
hello baz.txt
hello foo.txt
关于bash - 对目录中的所有文件执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10523415/