Home > Back-end >  Does double asterisk include the current directory?
Does double asterisk include the current directory?

Time:04-17

The following question asks What do double-asterisk (**) wildcards mean?

Many answers there indicate that ** includes the current directory as well as sub-directories.

However, when I test this in a bash console it only appears to list files from sub-directories. The following both list only sub-directory files

$ find **/*.md
docs/architecture.md
docs/autodetect.md
# ...

$ ls -d **/*.md
docs/architecture.md
docs/autodetect.md
# ...

Despite there being .md files in the current directory

$ ls -d *.md
CODE_OF_CONDUCT.md  CONTRIBUTING.md  README.md  SECURITY.md

Am I misunderstanding the answers to the referenced question, or am I testing the pattern incorrectly?

How can I be sure a command in a script operates on files in both the current directory and sub-directories?

CodePudding user response:

The globstar option needs to be turned on to enable special interpretation of **/. Otherwise, it is just two * globs in a row which won't match the current directory.

When the globstar shell option is enabled, and * is used in a filename expansion context, two adjacent *s used as a single pattern will match all files and zero or more directories and subdirectories. If followed by a /, two adjacent *s will match only directories and subdirectories.

Here is the default behavior (shopt -u globstar):

$ ls **/*.md
docs/architecture.md  docs/autodetect.md

And here is what you get when it's set (shopt -s globstar):

$ shopt -s globstar
$ ls **/*.md
CODE_OF_CONDUCT.md  CONTRIBUTING.md  README.md  SECURITY.md  docs/architecture.md  docs/autodetect.md
  • Related