LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-21-2013, 01:44 PM   #1
jzoudavy
Member
 
Registered: Apr 2012
Distribution: Ubuntu, SUSE, Redhat
Posts: 188

Rep: Reputation: Disabled
how to search for a particular word in a file in linux


Hi all

I got a question about searching in linux.

Assumption: there is a script in my linux system (quagga router) that is contains "ospfd.conf".

To prove that, I need to run an in depth search on each file in the system.

However,
Code:
find / -name ospfd.conf
only gives me file names that contain "ospfd.conf". which is not what i want.

So this isn't something I can use vi's search function, that assumes I already know where the file is. I don't.

thanks guys
 
Old 06-21-2013, 02:27 PM   #2
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 525

Rep: Reputation: 141Reputation: 141
I'm not sure what you are asking, but going by your thread title, i assume you want to search for files with particular word/pattern.

grep -r 'word' <path>

Code:
# grep -nr 'cool' / 2>/dev/null
Searches all files recursively starting from / for word 'cool'
-r = recursive
-n = print line number

Last edited by mddnix; 06-21-2013 at 02:43 PM.
 
Old 06-21-2013, 02:55 PM   #3
jzoudavy
Member
 
Registered: Apr 2012
Distribution: Ubuntu, SUSE, Redhat
Posts: 188

Original Poster
Rep: Reputation: Disabled
thanks for the tip about grep. i can't believe i forgot that...
 
Old 06-23-2013, 11:13 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Note that when using the -r recursive option, the arguments you give it should be starting directories, not files. You can also use the --include and --exclude options to filter what types of files it will actually search.

For even more advanced filtering use find instead, and use grep in its -exec action.

Code:
find <startdirs> -type f -name '*.txt' -exec grep 'searchstring' '{}' +

Last edited by David the H.; 06-23-2013 at 11:16 AM. Reason: small correction
 
  


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
search for key word from a file brian00 Linux - Newbie 19 09-12-2012 09:43 PM
How to build a script that will search for a specific word in a log file kimbo8 Programming 1 03-27-2012 06:17 AM
Search word and delete only the word and the line using Sed command kbmukesh Linux - Newbie 4 06-28-2011 07:35 AM
search file contains word prospekrisal Linux - Newbie 5 07-20-2006 10:24 PM
search a file for a word - bash script paul_mat Linux - Software 12 04-16-2006 02:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:15 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration