12

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?

k0pernikus
  • 6,115
  • @dessert Not a duplicate as in that question the globstar is one of many possible solutions, whereas this question is about enabling the globstar specifically. – k0pernikus Mar 01 '18 at 11:25
  • 2
    I beg to differ: It's true you wrote the question to point in that direction only, but the other question still tries to achieve the very same goal and its answers apply to your problem: One could add an answer “You can do the same with find as follows: …” here as well. – dessert Mar 01 '18 at 12:01
  • 1
    Duplicate on Unix & Linux: Recursive glob? – wjandrea Mar 02 '18 at 01:51
  • 2
    "Yet now I expect the double star operator to work on the subfolders" Why? – muru Mar 02 '18 at 01:51
  • @muru Because I had it working in the past, yet didn't know how to activate in anymore. – k0pernikus Mar 02 '18 at 08:06
  • 2
    While question is related to linked duplicate, it is indeed bash-specific. End result being same, doesn't imply it is strictly duplicate. That said, however, the question being closed doesn't significantly impact anyone and cross-linking related questions in one way or another still makes a post useful. – Sergiy Kolodyazhnyy Mar 05 '18 at 09:05

1 Answers1

22

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.

wjandrea
  • 14,236
  • 4
  • 48
  • 98
k0pernikus
  • 6,115
  • Works great but needs to be enabled every time I start a new terminal, how do you permanently enable it? – protango Jun 21 '21 at 08:06
  • @protango add it to your .bashrc file. For more info: https://unix.stackexchange.com/questions/129143/what-is-the-purpose-of-bashrc-and-how-does-it-work – Zaya Sep 13 '22 at 14:38