Solved

grep will not search subdirectories

Posted on 2004-04-23
5
2,405 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
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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…
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now