What expands to all files in current directory rec

2019-01-04 10:01发布

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?

5条回答
Rolldiameter
2楼-- · 2019-01-04 10:23

You can use: **/*.* to include all files recursively (enable by: 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:

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

  • ls -1 **/*.* - returns 3338
  • ls -1 {,**/}*.* - returns 3341 (as proposed by Dennis)
  • ls -1 {,**/}* - returns 8265
  • ls -1 **/* - returns 7817, except hidden files (as proposed by Dennis)
  • ls -1 **/{.[^.],}* - returns 7869 (as proposed by Dennis)
  • ls -1 {,**/}.?* - returns 15855
  • ls -1 {,**/}.* - returns 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:

$ 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.

查看更多
可以哭但决不认输i
3楼-- · 2019-01-04 10:38
$ 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

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

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

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-04 10:40

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

find . -name '*.ext' -print
查看更多
可以哭但决不认输i
5楼-- · 2019-01-04 10:41

This will work in Bash 4:

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

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

shopt -s globstar

From 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.
查看更多
我命由我不由天
6楼-- · 2019-01-04 10:43

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.

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

查看更多
登录 后发表回答