Home > database >  How does the ** work while searching for a Path
How does the ** work while searching for a Path

Time:04-09

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?

CodePudding user response:

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.

CodePudding user response:

Linux use regex with some commands like grep or find. As you say, ls or find * will show all the files and directories recursively.

"*" wildcard means: shows all that was contained before

If you use ls "*" it will show all the path that contains nothing before, so, all the paths in the current directory.

  • Related