Solved

grep will not search subdirectories

Posted on 2004-04-23
5
2,422 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

695 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