Solved

grep will not search subdirectories

Posted on 2004-04-23
5
2,418 Views
Last Modified: 2013-12-26
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" *
0
Comment
Question by:psmithphil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 10904091
>>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.


0
 
LVL 7

Accepted Solution

by:
fim32 earned 250 total points
ID: 10904102
how deep do you wanna go?

cuz grep sure doesn't descend into subdirectories...

you could do grep -l "blah" */*  to go one directory deep

or, if you wanted to scan ALL of your files, you'll have to combine that with a find:

find . -exec grep -l "blah" {} \;

if there are a lot of files, you're also going to wanna use xargs:

find . -type p | xargs grep -l "blah"
0
 

Author Comment

by:psmithphil
ID: 10904602
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!!
0
 
LVL 7

Expert Comment

by:fim32
ID: 10904696
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...
0
 

Author Comment

by:psmithphil
ID: 10905837
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?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
isEverywhere  challenge 19 86
My project did see openJDK that I installed. What could be the problem 7 161
changeXy challenge 13 93
Excel file not created as expected 7 94
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question