Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 06-21-2013, 12:44 PM   #1
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.

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, 01:27 PM   #2
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 537

Rep: Reputation: 149Reputation: 149
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>

# 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 01:43 PM.
Old 06-21-2013, 01:55 PM   #3
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, 10:13 AM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,842

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
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.

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

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


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

All times are GMT -5. The time now is 03:42 PM.

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