Solved

how to search a file which contain ....

Posted on 1998-12-29
5
223 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
Comment Utility
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
Comment Utility
I think is better if `ls -R` is change to `find *`

0
 
LVL 4

Expert Comment

by:jos010697
Comment Utility
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
Comment Utility
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
Comment Utility
The correct answer would be:

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

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

763 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

12 Experts available now in Live!

Get 1:1 Help Now