• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

how to search a file which contain ....

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
chsoh
Asked:
chsoh
1 Solution
 
jos010697Commented:
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
 
chsohAuthor Commented:
I think is better if `ls -R` is change to `find *`

0
 
jos010697Commented:
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
 
rajeevmCommented:
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
 
rajanxyzCommented:
The correct answer would be:

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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now