对目录中的所有文件执行命令 [英] Execute command on all files in a directory

查看:221
本文介绍了对目录中的所有文件执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人提供代码以执行以下操作: 假设有一个文件目录,所有文件都需要通过程序运行.程序将结果输出到标准输出.我需要一个脚本,该脚本将进入目录,对每个文件执行命令,并将输出合并为一个大输出文件.

Could somebody please provide the code to do the following: Assume there is a directory of files, all of which need to be run through a program. The program outputs the results to standard out. I need a script that will go into a directory, execute the command on each file, and concat the output into one big output file.

例如,要在1个文件上运行命令:

For instance, to run the command on 1 file:

$ cmd [option] [filename] > results.out

推荐答案

以下bash代码将$ file传递给命令,其中$ file表示/dir中的每个文件

The following bash code will pass $file to command where $file will represent every file in /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

这篇关于对目录中的所有文件执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆