bash 使用bash终端命令打开目录和子目录中的所有文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29202889/
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-18 12:35:56  来源:igfitidea点击:

Open all files in a directory and subdirectories using bash terminal command?

bashterminalzsh

提问by o_O

I have an alias where I can do open file1.type file2.typeor open *.type

我有一个别名,我可以在那里做open file1.type file2.typeopen *.type

What I want is to be able to use this on all subdirectories of my current location. So if I'm in the parent directory and there are two child directories, running the command will be the same as running open file1.type file2.type child1/file1.type child2/file1.type

我想要的是能够在我当前位置的所有子目录上使用它。因此,如果我在父目录中并且有两个子目录,则运行该命令将与运行相同open file1.type file2.type child1/file1.type child2/file1.type

So something like open -? *.typeis what I'm looking for.

所以open -? *.type我正在寻找类似的东西。

回答by Ignacio Vazquez-Abrams

If running zsh or bash 4.x with the globstaroption set, **will match all directories recursively.

如果使用globstar设置的选项运行 zsh 或 bash 4.x ,**将递归匹配所有目录。

#!/bin/zsh
open **/*.type

...

...

#!/bin/bash
shopt -s globstar
open **/*.type

回答by Paul Hicks

findworks for this sort of functionality. Something like this:

find适用于这种功能。像这样的东西:

find . -type f -name \*.type -exec open {} \;

Or in this case, since openis an alias, you have to run the shell as the command:

或者在这种情况下,由于open是别名,您必须将 shell 作为命令运行:

find . -type f -name \*.type -exec bash -c open {} \;