什么扩展到当前目录中的所有文件递归?

时间:2021-02-16 21:44:59

I know **/*.ext expands to all files in all subdirectories matching *.ext, but what is a similar expansion that includes all such files in the current directory as well?

我知道** / * .ext扩展到匹配* .ext的所有子目录中的所有文件,但是什么是类似的扩展,包括当前目录中的所有这些文件?

5 个解决方案

#1


91  

This will work in Bash 4:

这将在Bash 4中起作用:

ls -l {,**/}*.ext

In order for the double-asterisk glob to work, the globstar option needs to be set (default: on):

为了使双星号glob工作,需要设置globstar选项(默认值:on):

shopt -s globstar

From man bash:

来自man bash:

    globstar
                  If set, the pattern ** used in a filename expansion con‐
                  text will match a files and zero or more directories and
                  subdirectories.  If the pattern is followed by a /, only
                  directories and subdirectories match.

#2


10  

This wil print all files in the current directory and its subdirectories which end in '.ext'.

这将打印当前目录及其子目录中以“.ext”结尾的所有文件。

find . -name '*.ext' -print

#3


4  

You can use: **/*.* to include all files recursively (enable by: shopt -s globstar).

您可以使用:** / *。*以递归方式包含所有文件(启用方式:shopt -s globstar)。

Please find below testing of other variations and how they behave.

请在下面找到其他变体的测试以及它们的行为方式。


Testing folder with 3472 files in the sample VLC repository folder:

在示例VLC存储库文件夹中测试包含3472个文件的文件夹:

(Total files of 3472 counted as per: find . -type f | wc -l)

(总计3472个文件计为:find.-type f | wc -l)

  • ls -1 **/*.* - returns 3338
  • ls -1 ** / *。* - 返回3338
  • ls -1 {,**/}*.* - returns 3341 (as proposed by Dennis)
  • ls -1 {,** /} *。* - 返回3341(由Dennis提出)
  • ls -1 {,**/}* - returns 8265
  • ls -1 {,** /} * - 返回8265
  • ls -1 **/* - returns 7817, except hidden files (as proposed by Dennis)
  • ls -1 ** / * - 返回7817,隐藏文件除外(由Dennis提出)
  • ls -1 **/{.[^.],}* - returns 7869 (as proposed by Dennis)
  • ls -1 ** / {。[^。],} * - 返回7869(由Dennis提出)
  • ls -1 {,**/}.?* - returns 15855
  • ls -1 {,** /}。?* - 返回15855
  • ls -1 {,**/}.* - returns 20321
  • ls -1 {,** /}。* - 返回20321

So I think the most closest method to list all files recursively is the first example (**/*.*) as per gniourf-gniourf comment (assuming the files have the proper extensions, or use the specific one), as the second example gives few more duplicates like below:

因此,我认为递归列出所有文件的最接近的方法是第一个示例(** / *。*),根据gniourf-gniourf注释(假设文件具有适当的扩展名,或使用特定的扩展名),作为第二个示例提供更多重复,如下所示:

$ diff -u <(ls -1 {,**/}*.*) <(ls -1 **/*.*)
--- /dev/fd/63  2015-04-19 15:25:07.000000000 +0100
+++ /dev/fd/62  2015-04-19 15:25:07.000000000 +0100
@@ -1,6 +1,4 @@
 COPYING.LIB
-COPYING.LIB
-Makefile.am
 Makefile.am
@@ -45,7 +43,6 @@
 compat/tdestroy.c
 compat/vasprintf.c
 configure.ac
-configure.ac

and the other generate even further duplicates.

而另一个产生更多的重复。


To include hidden files, use: shopt -s dotglob (disable by shopt -u dotglob). It's not recommended, because it can affect commands such as mv or rm and you can remove accidentally the wrong files.

要包含隐藏文件,请使用:shopt -s dotglob(由shopt -u dotglob禁用)。不建议这样做,因为它可能会影响mv或rm等命令,并且可能会意外删除错误的文件。

#4


3  

$ find . -type f

That will list all of the files in the current directory. You can then do some other command on the output using -exec

这将列出当前目录中的所有文件。然后,您可以使用-exec对输出执行其他命令

$find . -type f -exec grep "foo" {} \;

That will grep each file from the find for the string "foo".

这将从查找字符串“foo”grep每个文件。

#5


2  

Why not just use brace expansion to include the current directory as well?

为什么不使用大括号扩展来包含当前目录呢?

./{*,**/*}.ext

Brace expansion happens before glob expansion, so you can effectively do what you want with older versions of bash, and can forego monkeying with globstar in newer versions.

在全局扩展之前进行大括号扩展,因此您可以使用旧版本的bash有效地执行您想要的操作,并且可以放弃在新版本中使用globstar进行修改。

Also, it's considered good practice in bash to include the leading ./ in your glob patterns.

此外,在bash中考虑好的做法是在你的glob模式中包含前导./。

#1


91  

This will work in Bash 4:

这将在Bash 4中起作用:

ls -l {,**/}*.ext

In order for the double-asterisk glob to work, the globstar option needs to be set (default: on):

为了使双星号glob工作,需要设置globstar选项(默认值:on):

shopt -s globstar

From man bash:

来自man bash:

    globstar
                  If set, the pattern ** used in a filename expansion con‐
                  text will match a files and zero or more directories and
                  subdirectories.  If the pattern is followed by a /, only
                  directories and subdirectories match.

#2


10  

This wil print all files in the current directory and its subdirectories which end in '.ext'.

这将打印当前目录及其子目录中以“.ext”结尾的所有文件。

find . -name '*.ext' -print

#3


4  

You can use: **/*.* to include all files recursively (enable by: shopt -s globstar).

您可以使用:** / *。*以递归方式包含所有文件(启用方式:shopt -s globstar)。

Please find below testing of other variations and how they behave.

请在下面找到其他变体的测试以及它们的行为方式。


Testing folder with 3472 files in the sample VLC repository folder:

在示例VLC存储库文件夹中测试包含3472个文件的文件夹:

(Total files of 3472 counted as per: find . -type f | wc -l)

(总计3472个文件计为:find.-type f | wc -l)

  • ls -1 **/*.* - returns 3338
  • ls -1 ** / *。* - 返回3338
  • ls -1 {,**/}*.* - returns 3341 (as proposed by Dennis)
  • ls -1 {,** /} *。* - 返回3341(由Dennis提出)
  • ls -1 {,**/}* - returns 8265
  • ls -1 {,** /} * - 返回8265
  • ls -1 **/* - returns 7817, except hidden files (as proposed by Dennis)
  • ls -1 ** / * - 返回7817,隐藏文件除外(由Dennis提出)
  • ls -1 **/{.[^.],}* - returns 7869 (as proposed by Dennis)
  • ls -1 ** / {。[^。],} * - 返回7869(由Dennis提出)
  • ls -1 {,**/}.?* - returns 15855
  • ls -1 {,** /}。?* - 返回15855
  • ls -1 {,**/}.* - returns 20321
  • ls -1 {,** /}。* - 返回20321

So I think the most closest method to list all files recursively is the first example (**/*.*) as per gniourf-gniourf comment (assuming the files have the proper extensions, or use the specific one), as the second example gives few more duplicates like below:

因此,我认为递归列出所有文件的最接近的方法是第一个示例(** / *。*),根据gniourf-gniourf注释(假设文件具有适当的扩展名,或使用特定的扩展名),作为第二个示例提供更多重复,如下所示:

$ diff -u <(ls -1 {,**/}*.*) <(ls -1 **/*.*)
--- /dev/fd/63  2015-04-19 15:25:07.000000000 +0100
+++ /dev/fd/62  2015-04-19 15:25:07.000000000 +0100
@@ -1,6 +1,4 @@
 COPYING.LIB
-COPYING.LIB
-Makefile.am
 Makefile.am
@@ -45,7 +43,6 @@
 compat/tdestroy.c
 compat/vasprintf.c
 configure.ac
-configure.ac

and the other generate even further duplicates.

而另一个产生更多的重复。


To include hidden files, use: shopt -s dotglob (disable by shopt -u dotglob). It's not recommended, because it can affect commands such as mv or rm and you can remove accidentally the wrong files.

要包含隐藏文件,请使用:shopt -s dotglob(由shopt -u dotglob禁用)。不建议这样做,因为它可能会影响mv或rm等命令,并且可能会意外删除错误的文件。

#4


3  

$ find . -type f

That will list all of the files in the current directory. You can then do some other command on the output using -exec

这将列出当前目录中的所有文件。然后,您可以使用-exec对输出执行其他命令

$find . -type f -exec grep "foo" {} \;

That will grep each file from the find for the string "foo".

这将从查找字符串“foo”grep每个文件。

#5


2  

Why not just use brace expansion to include the current directory as well?

为什么不使用大括号扩展来包含当前目录呢?

./{*,**/*}.ext

Brace expansion happens before glob expansion, so you can effectively do what you want with older versions of bash, and can forego monkeying with globstar in newer versions.

在全局扩展之前进行大括号扩展,因此您可以使用旧版本的bash有效地执行您想要的操作,并且可以放弃在新版本中使用globstar进行修改。

Also, it's considered good practice in bash to include the leading ./ in your glob patterns.

此外,在bash中考虑好的做法是在你的glob模式中包含前导./。