Link to home
Start Free TrialLog in
Avatar of psmithphil
psmithphilFlag for United States of America

asked on

grep will not search subdirectories

I am using AIX Unix.  I am searching for a string which I know is in a file in a subdirectory of my home directory.  I want to list the files that contain the string.  I thought by using a "*" that it would check all files in all subdirectories, but apparently not.  The grep command below returns nothing, but should be finding the string in a subdirectory.  What am I doing wrong?  Thank you!

grep -l "HOME/ScriptsTEST/echotest" *
Avatar of jkr
jkr
Flag of Germany image

>>What am I doing wrong?

'grep' simply does not recurse into subdirectories unless explicitly specified. Try

grep -r -l "HOME/ScriptsTEST/echotest" *

From the manpage:

       -r, --recursive
              Read  all  files under each directory, recursively;
              this is equivalent to the -d recurse option.


ASKER CERTIFIED SOLUTION
Avatar of fim32
fim32

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of psmithphil

ASKER

Thank you both for your excellent help!

I tried the -r switch and got the message "grep: Not a recognized flag: r".    The version of grep at my workplace must be old!

grep -l "HOME/ScriptsTEST/echotest" */* worked because the file was one level below my home directory.  I'll have to study how the combination find and grep works as I want to be able to search subdirectories several layers deep.

Fim, I will award you the points as you supplied a solution for me.  I sure wish the -r switch would have worked, but my place of employment obviously isn't up to snuff.

Thank you both!!
Avatar of fim32
fim32

actually, due to varying differences in grep, it is possible for you to have the latest version of grep (for your os) and not have that flag.

i know that on solaris 9, -r is not a recognizable flag, either...
Thank you, Fim, that's a shame, but I guess I'm stuck with that situation of no -r switch.

By the way, when I tried
find . -type p | xargs grep -l "HOME/ScriptsTEST/echotest"
It didn't return any results even though the file is only one level below.

find . -exec grep -l "HOME/ScriptsTEST/echotest" {} \;
This worked successfully.  
Why is the one with xargs supposed to be better?