LinuxQuestions.org
Visit Jeremy's Blog.
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 10-29-2017, 12:10 PM   #1
Linux.Girl
Member
 
Registered: Aug 2010
Posts: 53

Rep: Reputation: 2
Searching for a word in several different files and directories at the same time


Hello friends,

I know how to search for filenames containing a certain word.

What I need is different - I need a command that searches for a word in a directory that contains many files - but note - the word is not in the title of the file, it is in the text.

Is there a way to find a command that scans the contents of all files in a directory searching for a specific word?

Thanks,

linux.girl
 
Old 10-29-2017, 01:03 PM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
grep!
Code:
grep -Hw word *
prints filename:line
Code:
grep -hw word *
prints the lines only.
Code:
grep -lw word *
prints the filenames only.
Code:
man grep
for other options.
 
Old 10-29-2017, 01:06 PM   #3
Linux.Girl
Member
 
Registered: Aug 2010
Posts: 53

Original Poster
Rep: Reputation: 2
Yes, but I want to search several files at the same time - what you wrote me will search only in one specific file, or I got you wrong?
 
Old 10-29-2017, 01:41 PM   #4
Patrick59
Member
 
Registered: Apr 2007
Location: North France
Distribution: Slackware64 15.0
Posts: 74

Rep: Reputation: 19
Hello,

Is this something like that ? :
Code:
find . -type f -exec grep -lR mytext {} \;
 
Old 10-29-2017, 02:08 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The shell replaces the * with all files in the current directory.
So grep will search the "word" in all of them.
You can restrict this to certain files.
For example *.txt means all files whose filenames end with .txt

Last edited by MadeInGermany; 10-29-2017 at 02:10 PM.
 
Old 10-30-2017, 02:24 PM   #6
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by MadeInGermany View Post
grep!
Code:
grep -Hw word *
prints filename:line
Code:
grep -hw word *
prints the lines only.
Code:
grep -lw word *
prints the filenames only.
Code:
man grep
for other options.
One additionally interesting option might be -r ... to search directories recursively.
 
Old 10-30-2017, 07:09 PM   #7
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Maybe:
Code:
$ grep -r word /etc
will search every file in /etc, even subdirectories.
 
Old 10-31-2017, 01:03 PM   #8
Linux.Girl
Member
 
Registered: Aug 2010
Posts: 53

Original Poster
Rep: Reputation: 2
Many thanks for all the suggestions, thread marked as solved!
 
Old 10-31-2017, 02:18 PM   #9
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Unless the file you're searching is definitely binary, I'd suggest using the -I flag

Quote:
-I Process a binary file as if it did not contain matching data; this is
equivalent to the --binary-files=without-match option.
 
  


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
[SOLVED] Exclude directories when searching for files Jakkie Linux - Newbie 6 02-14-2012 11:02 PM
Searching Directories for files containing blah linuxbird Linux - Desktop 1 07-16-2010 11:11 AM
How can i read two files word by word at a time using any loop by shell script? vaibhavs17 Programming 16 03-19-2010 03:48 AM
Slow response time with word and excel files on Samba tjweaver Linux - Networking 7 08-22-2006 11:03 AM
Searching multiple directories and sub directories for a file jeep99899 Linux - Newbie 2 10-13-2005 12:23 PM

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

All times are GMT -5. The time now is 12:29 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
Open Source Consulting | Domain Registration