Register a domain and help support LQ
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 02-19-2008, 01:18 AM   #1
LQ Newbie
Registered: Feb 2008
Posts: 1

Rep: Reputation: 0
Help with BASH to search text files on disk

Hello guys,
M new to bash scripting and i wish to search all text files present on local disk. But m unable to write correct script for it. Please help me....
Old 02-19-2008, 01:29 AM   #2
Senior Member
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,124

Rep: Reputation: 164Reputation: 164
It's worth having a look at the advanced bash scripting guide - it's a very useful document.

You can use grep to do what you want and you can call it a couple of ways. If you just want to search for a string in every single file:
grep -r 'search-string' /*
Or, if you only want to search particular files or locations, use the find command:
find / -type f -name '*java' -exec grep 'search-string' {} \;
find / -type f -name '*java' | xargs grep 'search-string'
I can't test those at the moment, so beware of typos...
Old 02-19-2008, 01:14 PM   #3
Registered: Aug 2003
Location: Belguim, Ostend and Ghent
Distribution: Ubuntu
Posts: 600

Rep: Reputation: 30
usually I use:
find <dir> -exec grep -l {} \;

the -l option to grep makes that grep only returns the filename and not the line it found in the file. If you don't use this option, you have all these lines, but you don't know which file it comes from.
Old 02-19-2008, 02:14 PM   #4
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 232Reputation: 232Reputation: 232
Don't forget that if you're searching for a fixed string, rather than a regex, the grep "-F" can speed things up a lot.

BTW, gilead, that is a very thoughtful warning, thanks.

Last edited by archtoad6; 04-24-2008 at 09:56 AM. Reason: your/you're typo arrrgh!
Old 06-23-2015, 12:43 PM   #5
Registered: Nov 2013
Posts: 187

Rep: Reputation: 45
concerning "grep -r", i would use "grep -Ir" to ignore binary files.
Old 06-23-2015, 03:31 PM   #6
Keith Hedger
Senior Member
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 1,783

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
I have this
alias search='find . \! -iname '\''*~'\'' -print |xargs grep -s --ignore-case --binary-files=without-match --line-number'
In my bashrc file so I just use
search searchterm
when in the terminal.
1 members found this post helpful.


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 text inside files alaios Linux - Newbie 7 03-12-2006 10:20 AM
Perl: Search and replace directories within text files Erhnam Programming 2 03-07-2006 05:07 AM
Search text in files remotely anjanesh Red Hat 2 08-15-2005 10:03 AM
Appending Text Files From Bash Script alts Programming 3 11-18-2004 07:36 PM
Search all text files? nextekcarl Linux - General 4 10-20-2004 01:18 PM

All times are GMT -5. The time now is 05:47 AM.

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