Solved

how to search a file which contain ....

Posted on 1998-12-29
5
226 Views
Last Modified: 2010-08-05
how can I search a file and display a filename where
these files contain the "string"
I know that I can use "grep" to find but how to advance the searching included the sub-directory
thank
0
Comment
Question by:chsoh
5 Comments
 
LVL 4

Accepted Solution

by:
jos010697 earned 0 total points
ID: 2008578
Something like this could do the job for you:

   grep -l string `ls -R`

(note: the quotes around the ls command are back quotes).

kind regards,

Jos aka jos@and.nl

0
 

Author Comment

by:chsoh
ID: 2008579
I think is better if `ls -R` is change to `find *`

0
 
LVL 4

Expert Comment

by:jos010697
ID: 2008580
Yes, although I don't know about the 'better' part, but find can do the job too:

find . -name '*' -exec grep -l string {} ';'

kind regards,

Jos aka jos@and.nl

ps. All those silly quotes _are_ necessary here ...
0
 

Expert Comment

by:rajeevm
ID: 2008581
Hello ,
If you want to search recursively , can you please try below given shell script.
#!/usr/bin/ksh

if [[ $# -eq 0 ]]
then
   echo "Usage $0 Pattern"
   return 1
fi

Pattern=$*

echo "Please wait : Extracting  directory names ...\c"

Dirs=$(find . -type d -print)

echo "done."

for dir in $Dirs
do
  grep -in $Pattern $dir/*
done

return 0                
0
 

Expert Comment

by:rajanxyz
ID: 2008582
The correct answer would be:

find . -type f -exec grep -l "string" \{\} \;

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

856 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