bash脚本在目录中执行的所有文件命令 [英] Bash Script to Execute Command on All Files in Directory

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

问题描述

可能有人请提供code做到以下几点:
假设有文件,所有这些都需要通过一个程序来运行的一个目录。该程序将结果输出到标准输出。我需要一个脚本,将进入一个目录,在每个文件执行命令,Concat的输出成一个大的输出文件。

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

  CMD $ [选项] [文件名]> results.out


解决方案

下面的bash code将通过$文件命令,其中$文件将重新present在/目录中每个文件

 在/ DIR文件/ *

  CMD [选项]$文件>> results.out
DONE

示例

  EL @挑衅〜/ foo的$触摸foo.txt的跳回到bar.txt baz.txt
埃尔@在* .TXT对我挑衅的〜/ foo的$;不要回声你好$ I; DONE
你好跳回到bar.txt
你好baz.txt
你好foo.txt的

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.

For instance, to run the command on 1 file

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

解决方案

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

Example

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脚本在目录中执行的所有文件命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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