How to enable the double star ** (globstar) operator?

I know that there is an operator similar to the *, with the difference that it works into all the sub-directories.

Assume you have a folder structure:

.
├── bar
│   ├── foo
│   │   └── baz
│   │   └── hurz
│   │   └── lolz
│   │   └── hello.txt
│   └── poit.txt
└── fnord.txt

Then ls with single star * would list:

$ ls *.txt
fnord.txt

I expect the double star operator ** to work on the subfolders, yet it is not complete. I know that this can work as I had this enabled on another machine yet I forgot how.

$ ls **/*.txt
bar/poit.txt

I was expecting the output to look like:

ls **/*.txt
bar/foo/baz/hurz/lolz/hello.txt bar/poit.txt fnord.txt

How is the ** operator called and how do I activate it properly?

6

1 Answer

You are looking for the globstar shell option which was introduced in bash version 4.

If you unsure what version you are running you can test that via:

$ echo $BASH_VERSION
4.4.12(1)-release

Check if it is enabled via:

$ shopt globstar

It defaults to off. If you want to use it you have to enable it:

$ shopt -s globstar

Then it will work as expected. You might want to add this to your .bashrc for it to be always enabled in new shells.

To disable it:

$ shopt -u globstar

See help shopt for details.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like