Why doesn't grep -r also look in subdirectories when I search with a glob? [duplicate]
Grep with -r
was not working for me.
I then made a test situation. The directory /home/den/backup
now contains a file with the word washer
in it. I also made a subdirectory within /home/den/backup
. In that directory a file contains the word washer
. The following should return two hits at /home/den/backup/great.txt
and /home/den/backup/aaa/info.txt
If I issue
grep -r "washer" /home/den/backup/*.*
the result is one hit.
If I issue
grep -r "washer" /home/den/backup/aaa/*.*
the result is one hit.
Shouldn't the first one have also found the second one, which is in one if its sub-directories?
You can see what's happening here by setting the shell into debug mode using set -x
$ set -x
$ grep -r "washer" /home/steeldriver/backup/*.*
+ grep --color=auto -r washer /home/steeldriver/backup/great.txt
washer
i.e. the shell is expanding *.*
and matching the single file great.txt
- so grep
searches that single file.
If you want to recursively search the whole directory, just give the directory as the argument:
$ grep -r "washer" /home/steeldriver/backup/
+ grep --color=auto -r washer /home/steeldriver/backup/
/home/steeldriver/backup/aaa/info.txt:washer
/home/steeldriver/backup/great.txt:washer
(You can turn debug mode off again using set +x
)