Finding a File containing a particular text string in Linux server

by Vivek Gite on March 24, 2007 · 26 comments

Q. I've been using Linux for a while on my server and have a large collection of text file everywhere. I’m interested in learning about searching a text string. Can you tell me - how do I find a file containing a particular text string in Linux server?

A. I have to admit that there are tens and thousands of text files in Linux server. Finding and locating those files can be done with find command. Unfortunately find command cannot look inside a text file for a string.

You need to use grep command. grep searches the given input FILEs for lines containing a match or a text string.

grep command form (syntax)

grep “text string to search” directory-path

Examples

For example search for a string called redeem reward in all text files located in /home/tom/*.txt directory, use
$ grep "redeem reward" /home/tom/*.txt

Task: Search all subdirectories recursively

You can search for a text string all files under each directory, recursively with -roption:
$ grep -r "redeem reward" /home/tom

Task: Only print filenames

By default, grep command prints the matching lines You can pass -H option to print the filename for each match.
$ grep -H -r “redeem reward” /home/tom
Output:

...
filename.txt: redeem reward
...

To just print the filename use cut command as follows:
$ grep -H vivek /etc/* -R | cut -d: -f1
Output:

...
filename.txt
...

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 26 comments… read them below or add one }

1 memals January 5, 2008

grep -l ‘redeem reward’ /path
gives just the file names and -lr would do it recursively

Reply

2 Raja Mohammed August 4, 2009

If you want to find the exact string use the -w option.
for Example to find the whole word “Redeem Reward”,
grep -H -r -w “Redeem Reward” /

Reply

3 Florian September 16, 2009

Ok, this is a difficult one:
How would you look for all files named “file1″ OR “file2″ which are somewhere (recursively) in “/home/tom” and contain the string “Redeem Reward” ?

Reply

4 Prashant Kumashi October 7, 2009

>Ok, this is a difficult one:
>How would you look for all files named “file1″ OR “file2″ which are somewhere >(recursively) in “/home/tom” and contain the string “Redeem Reward” ?

You can use following command:
grep -r "redeem reward" /home/tom | grep file1

Reply

5 Anonymous May 9, 2010

find . -type f -exec grep -i “redeem reward” {} \; -print

Reply

6 clark September 30, 2011

for my version of linux, I had to move the -print in front. plus i added some params for grep

find . -type f -print -exec grep -inH “redeem reward” {} \;

Reply

7 anon May 9, 2010

find . -type f -exec grep -i “redeem reward” {} \; -print

Reply

8 Vatar May 13, 2010

find . -type f -exec grep -i “redeem reward” {} \; -print 2>/dev/null
to get rid of
grep: can’t open ./var/adm/log/secret.log

Reply

9 Albert December 13, 2010

how can i search for a specific file content and delete them like a:

all file who have the word “hello” for example

Reply

10 Vivek December 14, 2010

Let’s assume:-
files to search = *.txt
Directory to be searched=/tmp
search string=hello
Let’s also assume that file names *.txt also contain blank spaces as
“/tmp/1st file.txt”
First verify that you get the names of the correct files to be deleted as follows:

# find /tmp -type f -name ‘*.txt*’ | sed -e ‘s/.*/\”&\”/’ |xargs -n 1 grep -l hello|sed -e ‘s/.*/\”&\”/’

( Here sed is used to deal with blank spaces within file names )

After confirming that the results are ok, files can be deleted by piping & xargs as follows:

# find /tmp -type f -name ‘*.txt*’ | sed -e ‘s/.*/\”&\”/’ |xargs -n 1 grep -l hello|sed -e ‘s/.*/\”&\”/’|xargs -n 1 rm -f

Note: If the search string has blank spaces, place the entire “search string” within “Double quotes” .

Reply

11 Albert December 14, 2010

thanks!!!

i found a nother way to do that:

find /home/ -exec grep -l “mp3″ {} \; | xargs rm

i hope this help someone

Reply

12 Vivek December 15, 2010

Welcome !

Yes it’s short & better for sure. Did not try with -exec earlier.
Perfect if there are no blank spaces / special characters in file names.

Take care.
Vivek M Garg

Reply

13 Russ December 15, 2010

great people, helpful but what Vivek write is for me to difficult. May be it’s time to start learning sed or awk :-)
Thx2all

Reply

14 Me December 25, 2010

grep -Hr “TEXT_TO_FIND”

i.e. grep -Hr “Me” /home/earth

Reply

15 lindsey March 31, 2011

hi all, kind of a continuation of the original Q but need text from the “process table” in AIX not a file:
java process & need to parse out anything “-Xm”, appreciate any help, been banging my head trying: awk, cut, sed (noob here), perl (noob here) and it kinda works but i have to figure out the “positions of the fields” i need to make them work ($15,$16,$NF), i need a way to parse an unknown ‘position in a string’. hope this makes sense, thx much.

Reply

16 lindsey March 31, 2011

this works but again, i have to know hte positions to make it work correctly:
# ps -aefk | awk ‘/-Xm/ {print $14,$15,$NF}’
-Xms256m -Xmx1024m server1
-Xms50m -Xmx256m nodeagent

Reply

17 Tlucz-huba April 23, 2011

Hi guys,

can you please help me out with a grep command to find a 16 chars long string that starts with number ’2012′ and then followed by any characters?

Thank you,

T.

Reply

18 Carsten June 16, 2011

Hi T.,

use: grep “\b2012.\{12\}\b”

That means: any word (marked by \b) starting with 2012 followed by any character (“.”) repeated 12 times.

Carsten

Reply

19 chris pepper August 1, 2011

how do i search for a string containing “-g” ?? whenever i do
grep -r “-g” /home/user/src

it says “invalid option -g” how do i stop it picking it up as an option? I also tried

grep -r “cc” /home/user/src/*.mak and it says:
” /home/user/src/*.mak: no such file or directory” is this because of the -r option?

I appreciate any help!

-Thanks, Chris

Reply

20 Vivek Gite August 2, 2011
grep -r -- '-g' /home/user/src

Reply

21 Vivek M Garg August 2, 2011

Can also try grep -r “\-g” /home/user/src

It works in Linux.

– ‘-g’ also works as sugested by Vivek Gite

Reply

22 Karthick K September 1, 2011

Hi

I have to move n number of files from one path to another path. No matter what the file name is. Can I get the command for doing this? Unix as well as in Windows.

Any help is appreciated.

Thanks,
Karthick K

Reply

23 alan October 29, 2011

I am totally green in this… can anyone tell me how to find files which contain the same name of file and the same string in file? [name of file = file contain the same string/ text as name of the file]

Reply

24 Search_it November 11, 2011

Hello,
I want to search string ,but i dont know that string present in which file name or where it is present.Is there any unix command for searching such kind of string.?

Reply

25 da_bull97 November 13, 2011

I have an interesting question that ive been searching for a long time. would there be any way, to use a grep command to search for files based on words found in a text file?

for example, lets say i have a text file with the names of a bunch of songs i want to delete from directory /home/music. I want to be able to create a script that would search for the songs in /home/music from the text file.

any ideas?

Reply

26 shawn December 15, 2011

re: da_bull97, i’ve recently been hacked, and I used this command to find a specific word on my server throughout 100+ sites > and save the result to a text file.

grep -r “base64_decode” /home/ > /home/domain/public_html/results.txt

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">



Previous post:

Next post: