Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match all files under all nested directories with shell globbing

Tags:

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

like image 892
Samer Buna Avatar asked Dec 03 '10 19:12

Samer Buna


People also ask

What is globbing in shell script?

The Bash shell feature that is used for matching or expanding specific types of patterns is called globbing. Globbing is mainly used to match filenames or searching for content in a file. Globbing uses wildcard characters to create the pattern.

What is file globbing in Linux?

p>File globbing also known as Path Name Expansion. It is the method of recognizing wildcard patterns in linux and then finding the file path expansion based on these patterns. Wildcard Patterns are strings that are used for selection of multiple files based on patterns.

How do you search for a string in all files recursively in Linux?

Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. Using the grep command, we can recursively search all files for a string on a Linux.


2 Answers

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.

like image 94
Dennis Williamson Avatar answered Oct 13 '22 02:10

Dennis Williamson


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.

like image 45
Tobu Avatar answered Oct 13 '22 01:10

Tobu