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
6 个解决方案
#1
258
The following bash code will pass $file to command where $file will represent every file in /dir
下面的bash代码将把$file传递给命令,其中$file表示/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
#2
123
How about this:
这个怎么样:
find /some/directory -maxdepth 1 -type f -exec cmd option {} \; > results.out
-
-maxdepth 1
argument prevents find from recursively descending into any subdirectories. (If you want such nested directories to get processed, you can omit this.) - -maxdepth 1参数阻止find递归地下降到任何子目录中。(如果您希望处理这样的嵌套目录,可以忽略它。)
-
-type -f
specifies that only plain files will be processed. - -type -f指定只处理纯文件。
-
-exec cmd option {}
tells it to runcmd
with the specifiedoption
for each file found, with the filename substituted for{}
- -exec cmd选项{}告诉它运行cmd,每个文件都有指定的选项,文件名被替换为{}
-
\;
denotes the end of the command. - \;表示命令的结束。
- Finally, the output from all the individual
cmd
executions is redirected toresults.out
- 最后,所有cmd执行的输出都被重定向到result .out
However, if you care about the order in which the files are processed, you might be better off writing a loop. I think find
processes the files in inode order (though I could be wrong about that), which may not be what you want.
但是,如果您关心文件处理的顺序,您最好编写一个循环。我认为按inode顺序查找文件(尽管我可能会错),这可能不是您想要的。
#3
25
I'm doing this on my raspberry pi from the command line by running:
我在命令行中的树莓派上运行:
for i in *;do omxplayer "$i";done
#4
2
Based on @Jim Lewis's approach:
基于@Jim Lewis的方法:
Here is a quick solution using find
and also sorting files by their modification date:
这里有一个快速解决方案,使用查找和排序文件的修改日期:
$ find directory/ -maxdepth 1 -type f -print0 | \
xargs -r0 stat -c "%y %n" | \
sort | cut -d' ' -f4- | \
xargs -d "\n" -I{} cmd -op1 {}
For sorting see:
排序:
http://www.commandlinefu.com/commands/view/5720/find-files-and-list-them-sorted-by-modification-time
http://www.commandlinefu.com/commands/view/5720/find-files-and-list-them-sorted-by-modification-time
#5
1
I needed to copy all .md files from one directory into another, so here is what I did.
我需要将所有的。md文件从一个目录复制到另一个目录中,所以我这样做了。
for i in **/*.md;do mkdir -p ../docs/"$i" && rm -r ../docs/"$i" && cp "$i" "../docs/$i" && echo "$i -> ../docs/$i"; done
我在**/*.md,做mkdir -p ../docs/ $i & rm -r/文档/“$i”& cp“$i”/docs/$i" & echo "$i -> .. ./docs/$i";完成
Which is pretty hard to read, so lets break it down.
这很难读,我们把它分解一下。
first cd into the directory with your files,
第一张cd进入你的文件目录,
for i in **/*.md;
for each file in your pattern
因为我在* * / *。海事;对于模式中的每个文件
mkdir -p ../docs/"$i"
make that directory in a docs folder outside of folder containing your files. Which creates an extra folder with the same name as that file.
mkdir - p . ./docs/“$i”将目录放在包含文件的文件夹之外的docs文件夹中。创建一个与该文件同名的额外文件夹。
rm -r ../docs/"$i"
remove the extra folder that is created as a result of mkdir -p
rm - r . ./docs/“$i”删除mkdir -p结果创建的额外文件夹
cp "$i" "../docs/$i"
Copy the actual file
cp " $我" . ./docs/$i"复制实际文件。
echo "$i -> ../docs/$i"
Echo what you did
回声“$ i - > . ./docs/$i“和你做的一样
; done
Live happily ever after
;从此过上了幸福的生活。
#6
1
One quick and dirty way which gets the job done sometimes is:
一种快速而肮脏的方式,有时完成工作是:
find directory/ | xargs Command
For example to find number of lines in all files in the current directory, you can do:
例如,要在当前目录中的所有文件中查找行数,可以这样做:
find . | xargs wc -l
#1
258
The following bash code will pass $file to command where $file will represent every file in /dir
下面的bash代码将把$file传递给命令,其中$file表示/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
#2
123
How about this:
这个怎么样:
find /some/directory -maxdepth 1 -type f -exec cmd option {} \; > results.out
-
-maxdepth 1
argument prevents find from recursively descending into any subdirectories. (If you want such nested directories to get processed, you can omit this.) - -maxdepth 1参数阻止find递归地下降到任何子目录中。(如果您希望处理这样的嵌套目录,可以忽略它。)
-
-type -f
specifies that only plain files will be processed. - -type -f指定只处理纯文件。
-
-exec cmd option {}
tells it to runcmd
with the specifiedoption
for each file found, with the filename substituted for{}
- -exec cmd选项{}告诉它运行cmd,每个文件都有指定的选项,文件名被替换为{}
-
\;
denotes the end of the command. - \;表示命令的结束。
- Finally, the output from all the individual
cmd
executions is redirected toresults.out
- 最后,所有cmd执行的输出都被重定向到result .out
However, if you care about the order in which the files are processed, you might be better off writing a loop. I think find
processes the files in inode order (though I could be wrong about that), which may not be what you want.
但是,如果您关心文件处理的顺序,您最好编写一个循环。我认为按inode顺序查找文件(尽管我可能会错),这可能不是您想要的。
#3
25
I'm doing this on my raspberry pi from the command line by running:
我在命令行中的树莓派上运行:
for i in *;do omxplayer "$i";done
#4
2
Based on @Jim Lewis's approach:
基于@Jim Lewis的方法:
Here is a quick solution using find
and also sorting files by their modification date:
这里有一个快速解决方案,使用查找和排序文件的修改日期:
$ find directory/ -maxdepth 1 -type f -print0 | \
xargs -r0 stat -c "%y %n" | \
sort | cut -d' ' -f4- | \
xargs -d "\n" -I{} cmd -op1 {}
For sorting see:
排序:
http://www.commandlinefu.com/commands/view/5720/find-files-and-list-them-sorted-by-modification-time
http://www.commandlinefu.com/commands/view/5720/find-files-and-list-them-sorted-by-modification-time
#5
1
I needed to copy all .md files from one directory into another, so here is what I did.
我需要将所有的。md文件从一个目录复制到另一个目录中,所以我这样做了。
for i in **/*.md;do mkdir -p ../docs/"$i" && rm -r ../docs/"$i" && cp "$i" "../docs/$i" && echo "$i -> ../docs/$i"; done
我在**/*.md,做mkdir -p ../docs/ $i & rm -r/文档/“$i”& cp“$i”/docs/$i" & echo "$i -> .. ./docs/$i";完成
Which is pretty hard to read, so lets break it down.
这很难读,我们把它分解一下。
first cd into the directory with your files,
第一张cd进入你的文件目录,
for i in **/*.md;
for each file in your pattern
因为我在* * / *。海事;对于模式中的每个文件
mkdir -p ../docs/"$i"
make that directory in a docs folder outside of folder containing your files. Which creates an extra folder with the same name as that file.
mkdir - p . ./docs/“$i”将目录放在包含文件的文件夹之外的docs文件夹中。创建一个与该文件同名的额外文件夹。
rm -r ../docs/"$i"
remove the extra folder that is created as a result of mkdir -p
rm - r . ./docs/“$i”删除mkdir -p结果创建的额外文件夹
cp "$i" "../docs/$i"
Copy the actual file
cp " $我" . ./docs/$i"复制实际文件。
echo "$i -> ../docs/$i"
Echo what you did
回声“$ i - > . ./docs/$i“和你做的一样
; done
Live happily ever after
;从此过上了幸福的生活。
#6
1
One quick and dirty way which gets the job done sometimes is:
一种快速而肮脏的方式,有时完成工作是:
find directory/ | xargs Command
For example to find number of lines in all files in the current directory, you can do:
例如,要在当前目录中的所有文件中查找行数,可以这样做:
find . | xargs wc -l