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?

6
  • @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 1, 2018 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 1, 2018 at 12:01
  • 1
    Duplicate on Unix & Linux: Recursive glob?
    – wjandrea
    Mar 2, 2018 at 1:51
  • 2
    "Yet now I expect the double star operator to work on the subfolders" Why?
    – muru
    Mar 2, 2018 at 1:51
  • 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. Mar 5, 2018 at 9:05

1 Answer 1

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.

2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .