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

发布于 2024-07-24 22:44:39 字数 609 浏览 5 评论 0原文

我已将一个大文本文件拆分为许多较小的文件集,以进行我正在进行的性能测试。 有很多这样的目录:

/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...)

需要注意的是,每个目录中的文件数量不断增加。 我需要做的是针对输出目录中的每个文本文件运行可执行文件。 对于单个文件,该命令看起来像这样:

./myexecutable -i /home/brianly/output-02/myfile.chunk.00 -o /home/brianly/output-02/myfile.chunk.00.processed

这里 -i 参数是输入文件,-o 参数是输出位置。

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

酒浓于脸红 2024-07-31 22:44:39

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

$ find output-* -name "*.chunk.??" | xargs -I{} ./myexecutable -i {} -o {}.processed

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

For this kind of thing I always use find together with 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. Thus it's a very useful tool to keep in one's utility belt, so I thought it ought to be mentioned.

鹤舞 2024-07-31 22:44:39

就像是:

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

Something like:

for x in `find /home/brianonly -type f`
do
./yourexecutable -i $x -o $x.processed
done
北陌 2024-07-31 22:44:39

正如其他人所建议的,使用 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 ';'

As others have suggested, use 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 ';'
薄情伤 2024-07-31 22:44:39

从提供的信息来看,这听起来像是对您的 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

From the information provided, it sounds like this would be a completely straightforward translation of your C# idea.

for i in /home/brianly/output-*; do
    for j in "$i/"*.[0-9][0-9]; do
        ./myexecutable -i "$j" -o "$j.processed"
    done
done
甜妞爱困 2024-07-31 22:44:39

这就是 find 命令的用途。

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

That's what the find command is for.

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

樱桃奶球 2024-07-31 22:44:39

使用查找和执行。 看看下面的

http://tldp.org/LDP/abs/html/moreadv .html

Use find and exec. Have a look at following

http://tldp.org/LDP/abs/html/moreadv.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文