[英]Bash: how to traverse directory structure and execute commands?
I have split a large text file into a number of sets of smaller ones for performance testing that i'm doing.我已将一个大文本文件拆分为多个较小的文件集,用于我正在进行的性能测试。 There are a number of directories like this:
有很多这样的目录:
/home/brianly/output-02 (contains 2 files myfile.chunk.00 and myfile.chunk.01)
/home/brianly/output-04 (contains 4 files...)
/home/brianly/output-06 (contains 6 files...)
It's important to note that there is an increasing number of files in each directory.请务必注意,每个目录中的文件数量都在增加。 What I need to do is run an executable against each of the text files in the output directories.
我需要做的是针对 output 目录中的每个文本文件运行一个可执行文件。 The command looks something like this against a single file:
该命令针对单个文件看起来像这样:
./myexecutable -i /home/brianly/output-02/myfile.chunk.00 -o /home/brianly/output-02/myfile.chunk.00.processed
Here the -i parameter is the input file and -o parameter is the output location.这里的-i参数是输入文件,-o参数是output位置。
In C# I'd loop over the directories get the list of files in each folder, then loop over them to run the commandlines.在 C# 中,我将遍历目录获取每个文件夹中的文件列表,然后遍历它们以运行命令行。 How do I traverse a directory structure like this using bash, and execute the command with the correct parameters based on the location and files in that location?
如何使用 bash 遍历这样的目录结构,并根据位置和该位置中的文件使用正确的参数执行命令?
For this kind of thing I always use find together with xargs :对于这种事情,我总是将find与xargs一起使用:
$ find output-* -name "*.chunk.??" | xargs -I{} ./myexecutable -i {} -o {}.processed
Now since your script processes only one file at a time, using -exec (or -execdir ) directly with find , as already suggested, is just as efficient, but I'm used to using xargs , as that's generally much more efficient when feeding a command operating on many arguments at once.现在,由于您的脚本一次只处理一个文件,因此如前所述,直接将-exec (或-execdir )与find一起使用也同样有效,但我习惯使用xargs ,因为这通常在喂食时更有效一次对许多 arguments 进行操作的命令。 Thus it's a very useful tool to keep in one's utility belt, so I thought it ought to be mentioned.
因此,它是随身携带的非常有用的工具,因此我认为应该提及它。
Something like:就像是:
for x in `find /home/brianonly -type f`
do
./yourexecutable -i $x -o $x.processed
done
As others have suggested, use find(1)
:正如其他人所建议的那样,使用
find(1)
:
# Find all files named 'myfile.chunk.*' but NOT named 'myfile.chunk.*.processed'
# under the directory tree rooted at base-directory, and execute a command on
# them:
find base-directory -name 'output.*' '!' -name 'output.*.processed' -exec ./myexecutable -i '{}' -o '{}'.processed ';'
From the information provided, it sounds like this would be a completely straightforward translation of your C# idea.从提供的信息来看,这听起来像是对您的 C# 想法的完全直截了当的翻译。
for i in /home/brianly/output-*; do
for j in "$i/"*.[0-9][0-9]; do
./myexecutable -i "$j" -o "$j.processed"
done
done
That's what the find
command is for.这就是
find
命令的用途。
http://linux.die.net/man/1/find http://linux.die.net/man/1/find
Use find and exec.使用查找和执行。 Have a look at following
看看下面的
http://tldp.org/LDP/abs/html/moreadv.html http://tldp.org/LDP/abs/html/moreadv.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.