Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'd like to write something like this:

$ ls **.py

in order to get all .py filenames, recursively walking a directory hierarchy.

Even if there are .py files to find, the shell (bash) gives this output:

ls: cannot access **.py: No such file or directory

Any way to do what I want?

EDIT: I'd like to specify that I'm not interested in the specific case of ls, but the question is about the glob syntax.

share|improve this question

3 Answers 3

up vote 22 down vote accepted

In order to do recursive globs in bash, you need the globstar feature from bash version 4 or higher.

From the bash manpage:

globstar
    If set, the pattern ** used in a pathname expansion context will
    match all files and zero or more directories and subdirectories.
    If the pattern is followed by a /, only directories and
    subdirectories match.

For your example pattern:

shopt -s globstar
ls **/*.py
share|improve this answer
2  
I'd recommend also enabling nullglob –  glenn jackman Oct 4 '12 at 16:50
4  
@glennjackman But before enabling nullglob, I would strongly advise reading following warnings. –  Serge Stroobandt Jun 19 '13 at 21:53

Since bash 4 (including zsh) a new globbing option called globstar has been added which treats the pattern ** differently when it's set.

It is matching the wildcard pattern and returning the file and directory names that match then by replacing the wildcard pattern in the command with the matched items.

Normally when you use **, it works similar to *, but it's recurses all the directories recursively (like a loop).

To see if it's enabled, check it by shopt globstar (in scripting, use shopt -q globstar).

The syntax is the same as you would use for *. That's why it's known as extended globbing.

The example **.py would work only for the current directory, as it doesn't return list of directories which can be recurses, so that's why you need to use multiple directory-level wildcard **/*.py, so it can go deeper.

Please find on SO few syntax tests which I did for finding all the files recursively.

share|improve this answer
find . -name '*.py'

** doesn't do anything more than a single *, both operate in the current directory

share|improve this answer
    
Interesting. Though, I'm more focused on the glob syntax by itself, because I have to use it in a configuration file (include directive). I don't need a list of files. –  Paolo Oct 4 '12 at 15:48
    
@Doug O'Neal, that's no longer true. bash has now copied that zsh feature (though it adopted a syntax closer to that of ksh93 and like ksh, doesn't support zsh's globbing qualifiers yet which limits its usefulness) –  Stéphane Chazelas Oct 4 '12 at 20:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.