LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to find a particular string!! from a Directory (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-find-a-particular-string-from-a-directory-894137/)

anishkumarv 07-27-2011 01:57 PM

How to find a particular string!! from a Directory
 
Hi all,

I want to search a particular string in a directory , that directory contains N number of subdirectories, and files and my requirement is inside the directory and subdirectory what are the files contains particular string like "TBone" i want to display that files.
Is it possible to do that.. Dont think this as a homework, this simple command helps me in lot of ways. so please guide me to solve this thread.

T3RM1NVT0R 07-27-2011 02:09 PM

@ Reply
 
Hi there,

As I can understand you are trying to look for a filename/directory name that contains a particular string say ABC. I think locate command will work for you.

Code:

updatedb /* This command will update the database which locate command will use */
locate <string>

or

You can use find command to do your job

Code:

find / -name *string*
I hope this helps.

grail 07-27-2011 02:26 PM

Is the string in the name of the file or actually inside the file?

As previous post answers part 1 then the answer to part 2 would be to use grep:
Code:

grep -r -l string top_dir/*

anishkumarv 07-27-2011 08:56 PM

Hi grail,


My requirement is inside the file,

for example:

a directory name
Code:

"grail"
in that dir contains 3 files and 2 sub directories llike

Code:

anish,txt
linux.txt
boys.txt
jesus
father

in that
Quote:

anish.txt
file contains this string.

Code:

God Save Me, i love my dad
and
Quote:

boys.txt
file contains this string.

Code:

I love My friends
and sub dir
Quote:

jesus
contains one file
Quote:

"qustion.txt"
that file contains this string
Code:

Who love me?
if i searched the keyword
Quote:

"love"
means I want the output like this.

Code:

boys.txt
anish.txt
question.txt

How is it possible?? I hope you understand my requirement, sorry my english is not good compare to you guys.

grail 07-27-2011 10:12 PM

The grep i provided should be able to help:
Code:

grep -r -l "God Save Me, i love my dad" grail/*

samtoddler 07-28-2011 12:43 AM

Hi grail,

command you have given provide only one level search , It doesn't list all the files there. I've tried with the scenario described by anish
it gives me following result

directory structure


[root@master tmp]# ls -R
.:
grail

./grail:
anish.txt boys.txt father jesus linux.txt

./grail/father:

./grail/jesus:
questions.txt


and command provided by you shows this result

[root@master tmp]# grep -i -l "love" grail/*
grail/anish.txt
[root@master tmp]# cat grail/anish.txt
God Save Me,i love my mom

I should list all the files containing the word love...

thanx
samtoddler

grail 07-28-2011 01:28 AM

Maybe if you used -r instead of -i, which was my suggestion, you will get the correct result.

samtoddler 07-28-2011 01:36 AM

hi grail,


thanx lil typo...

samtoddler

anishkumarv 07-28-2011 01:10 PM

Hi all,

Thanks for all replies, you guys done a great help for me.. Thank you so much :-)


All times are GMT -5. The time now is 10:25 PM.