psmithphil
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 " *
grep -l "HOME/ScriptsTEST/echotest
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!!
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
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!!
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...
i know that on solaris 9, -r is not a recognizable flag, either...
ASKER
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?
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?
'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.