Bash:如何遍历目录结构并执行命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1003528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:58:47  来源:igfitidea点击:

Bash: how to traverse directory structure and execute commands?

linuxbashcommand-linedirectorytraversal

提问by Brian Lyttle

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. 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 参数是输出位置。

In C# I'd loop over the directories get the list of files in each folder, then loop over them to run the commandlines. 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?

在 C# 中,我会遍历目录以获取每个文件夹中的文件列表,然后遍历它们以运行命令行。如何使用 bash 遍历这样的目录结构,并根据位置和该位置的文件使用正确的参数执行命令?

采纳答案by Lars Haugseth

For this kind of thing I always use findtogether with xargs:

对于这种事情,我总是将findxargs一起使用:

$ 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. Thus it's a very useful tool to keep in one's utility belt, so I thought it ought to be mentioned.

现在,由于您的脚本一次只处理一个文件,因此直接将-exec(或-execdir)与find 一起使用,正如已经建议的那样,同样有效,但我习惯使用xargs,因为这通常在喂食时效率更高一次对多个参数进行操作的命令。因此它是一个非常有用的工具,可以放在一个人的实用腰带上,所以我认为应该提到它。

回答by nikudesu

Something like:

就像是:

for x in `find /home/brianonly -type f`
do
./yourexecutable -i $x -o $x.processed
done

回答by Adam Rosenfield

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 ';'

回答by ephemient

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

回答by Methos

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

回答by S.Lott

That's what the findcommand is for.

这就是find命令的用途。

http://linux.die.net/man/1/find

http://linux.die.net/man/1/find