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:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- My 10 UNIX Command Line Mistakes
- Linux: 20 Iptables Examples For New SysAdmins

- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
Facebook it - Tweet it - Print it -


{ 26 comments… read them below or add one }
grep -l ‘redeem reward’ /path
gives just the file names and -lr would do it recursively
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” /
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” ?
>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 file1find . -type f -exec grep -i “redeem reward” {} \; -print
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” {} \;
find . -type f -exec grep -i “redeem reward” {} \; -print
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
how can i search for a specific file content and delete them like a:
all file who have the word “hello” for example
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” .
thanks!!!
i found a nother way to do that:
find /home/ -exec grep -l “mp3″ {} \; | xargs rm
i hope this help someone
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
great people, helpful but what Vivek write is for me to difficult. May be it’s time to start learning sed or awk :-)
Thx2all
grep -Hr “TEXT_TO_FIND”
i.e. grep -Hr “Me” /home/earth
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.
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
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.
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
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
Can also try grep -r “\-g” /home/user/src
It works in Linux.
– ‘-g’ also works as sugested by Vivek Gite
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
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]
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.?
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?
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