LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-19-2008, 12:18 AM   #1
purveshk
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, 12:29 AM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
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:
Code:
grep -r 'search-string' /*
Or, if you only want to search particular files or locations, use the find command:
Code:
find / -type f -name '*java' -exec grep 'search-string' {} \;
or
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, 12:14 PM   #3
elluva
Member
 
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, 01:14 PM   #4
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
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 08:56 AM. Reason: your/you're typo arrrgh!
 
Old 06-23-2015, 11:43 AM   #5
sudowtf
Member
 
Registered: Nov 2013
Posts: 205

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

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


Reply



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

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

All times are GMT -5. The time now is 02:03 AM.

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
Open Source Consulting | Domain Registration