0

I'm experiencing issue with the double star associated to grep. I'm using ubuntu 16.04.

In my understanding (and after a lot of research): grep 'a' **/* should find any occurence of 'a' in all files in my directory and all sub-directories (reccursively).

However, it doesn't work like that in my system. Here's a test: My file directory .a (file containing an "a") ba/a (file containing "in ba") ba/ca/a (file containing "in ca in ba")

 grep 'a' *
 a:a
 ba is a directory

 grep 'a' **/*
 ba/a:in ba
 grep: ba/ca: is a directory

The first case is obvious, but I was expecting from the second case to see the three files...

What is the explanation behind that?

Thanks, Bob

BobCitron
  • 35
  • 5
  • 1
    What is your grep version? `grep --version` – fedorqui May 23 '17 at 11:36
  • I haven't thought of that... For complex package versioning this is my first reflex but there I was clueless! My version is 2.24, I will upgrade it to 3.0 and check it again. – BobCitron May 23 '17 at 12:15

1 Answers1

1

grep is not "associated" with the double star. Your shell expands these.

Depending on which shell you use and which settings you have in that shell, the double star may mean to expand any number of subdirectory levels.

If you want to recursively grep, use grep -r like grep -r a .

marcolz
  • 2,880
  • 2
  • 23
  • 28
  • This seems reasonable. However, the advantage of using **/* is to specify a pattern for the filename. Is there any simple solution (not using find + grep) to adress this issue? Thanks for your time – BobCitron May 23 '17 at 13:49
  • 1
    Well, no, you either need to use `find` or run the command from a shell that does what you want, like `zsh` or `bash`. – marcolz May 24 '17 at 08:38
  • 1
    For bash, you need to have `globstar` set: `shopt -s globstar`, see https://stackoverflow.com/questions/28176590/what-do-double-asterisk-wildcards-mean – marcolz May 24 '17 at 08:48
  • Thanks to you, I've now activated globstar and it works! – BobCitron May 24 '17 at 09:42
  • Of course there is. Check `grep --include`, `--include-dir`, `--exclude` and `--exclude-dir` – bloody Apr 28 '20 at 19:33