• Uncategorized

About linux : How-does-the-work-while-searching-for-a-Path

Question Detail

I didn’t find a lot of info about this, as far as I know it matches filenames and directories recursively, but how does it work?

Question Answer

The glob-expression ** is used to match all files and zero or more directories and subdirectories. If the pattern is followed by a /, only directories and subdirectories match.

This means that it is used in a recursive file-search during path-name expansion patterns on the command line.

Depending on the shell you use, it needs to be enabled. In bash this is done with:

$ shopt -s globstar

Here are examples:

# list all files recursively
$ echo **
# list all files recursively that end with .txt
$ echo **/*.txt
# list all files recursively that are in a subdirectory foo
$ echo **/foo/**

Beware that the following pattern does not work recursively **.txt. This is just seen as a combination of two single asterisk globs and is identical to *.txt.

Note: there are subtle differences between bash and zsh, but in general it works the same.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.