?
Solved

grep will not search subdirectories

Posted on 2004-04-23
5
Medium Priority
?
2,426 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 1000 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

765 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