Match all files under all nested directories with shell globbing
Asked Answered
K

6

26

Is there a way to use shell globbing to identify nested directories?

so if I have dir/dir1/dir2/dir3/dir4/dir5/.. and I have files under all of them, what is the equivalent globbing pattern to match all files under all directories, similar to - for example - ls -R

Kazukokb answered 3/12, 2010 at 19:20 Comment(3)
Voted to move to SuperUser.com. Also why is this tagged 'git'?Oriente
gitignore doesn't use shell globbing; that's a separate question.Kempis
Similar: What expands to all files in current directory recursively? at SOHerodotus
W
27

In Bash 4, with shopt -s globstar, and zsh you can use **/* which will include everything except hidden files. You can do shopt -s dotglob in Bash 4 or setopt dotglob in zsh to cause hidden files to be included.

In ksh, set -o globstar enables it. I don't think there's a way to include dot files implicitly, but I think **/{.[^.],}* works.

Whiteside answered 3/12, 2010 at 19:37 Comment(3)
That's cool! will files parsed for shell globbing (like for example .gitignore) use those settings?Kazukokb
@Samer Abukhait: If the shell is doing the globbing then it should.Whiteside
@Samer: But gitignore isn't using shell globbing, it's using fnmatch.Kempis
A
6

Specifically about git (gitignore, gitattributes, and commands that take filenames): if the pattern contains no slash, * wildcards will match deep. If it does contain a slash, git will call fnmatch with the FNM_PATHNAME flag, and simple wildcards won't match slashes. ** to match deep isn't supported. Maybe this kind of deep matching could be more widely supported with a new FNM_STARSTAR flag, and an implementation in glibc, gnulib and other places.

Abshire answered 3/12, 2010 at 20:27 Comment(0)
M
2

If you want to act on all the files returned by find, rather than just list them, you can pipe them to xargs:

find <directory> -type f | xargs ls

But this is only for commands that don't have a recursive flag.

Macfarlane answered 3/12, 2010 at 19:41 Comment(0)
H
1

You may try:

**/*.*

However it'll ignore hidden files (such as .git files). Sometimes it's a life-saver.

Read more at: What expands to all files in current directory recursively? at SO

Herodotus answered 18/4, 2015 at 23:34 Comment(0)
O
0

There is no way to do this with vanilla Bash, however most commands accept a -R or --recursive option to tell them to descend into directories.

If you simply want to list all files located anywhere within a directory or its sub-directories, you can use find.

To recursively find files (-type f) with a given directory:

find <directory> -type f
Oriente answered 3/12, 2010 at 19:26 Comment(0)
E
0

You can use tree, it will show all folders recursively.

tree <path>
Euraeurasia answered 3/12, 2010 at 19:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.