LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 07-27-2011, 01:57 PM   #1
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Rep: Reputation: 10
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.
 
Old 07-27-2011, 02:09 PM   #2
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, Ubuntu, SLES, CentOS
Posts: 1,790

Rep: Reputation: 324Reputation: 324Reputation: 324Reputation: 324
@ 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.
 
Old 07-27-2011, 02:26 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,503

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
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/*
 
1 members found this post helpful.
Old 07-27-2011, 08:56 PM   #4
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Original Poster
Rep: Reputation: 10
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.

Last edited by anishkumarv; 07-27-2011 at 09:46 PM.
 
Old 07-27-2011, 10:12 PM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,503

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
The grep i provided should be able to help:
Code:
grep -r -l "God Save Me, i love my dad" grail/*
 
1 members found this post helpful.
Old 07-28-2011, 12:43 AM   #6
samtoddler
Member
 
Registered: Jul 2011
Location: Delhi
Distribution: Cent OS/RHEL
Posts: 37

Rep: Reputation: Disabled
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
 
1 members found this post helpful.
Old 07-28-2011, 01:28 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,503

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
Maybe if you used -r instead of -i, which was my suggestion, you will get the correct result.
 
Old 07-28-2011, 01:36 AM   #8
samtoddler
Member
 
Registered: Jul 2011
Location: Delhi
Distribution: Cent OS/RHEL
Posts: 37

Rep: Reputation: Disabled
hi grail,


thanx lil typo...

samtoddler
 
Old 07-28-2011, 01:10 PM   #9
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Original Poster
Rep: Reputation: 10
Hi all,

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to find a string in files under the current directory? thomas2004ch Linux - Newbie 4 10-05-2010 06:41 AM
Shell scripting - Getting just directory string from file path string? arashi256 Programming 5 10-16-2009 08:21 AM
find string in filename and use string to create directories daberkow Linux - Newbie 11 05-01-2009 02:12 PM
How to find a string in a directory? iclinux Linux - Newbie 2 07-02-2005 08:20 AM
Find string pattern in directory of text files magnum818 Linux - Newbie 2 10-15-2003 08:19 PM


All times are GMT -5. The time now is 02:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration