How to search a directory recursively for specific files containing a text string in linux / unix

Posted on 2011-10-13
Last Modified: 2012-05-12

I would like a command to search a directory recursivly for all .txt files containing the string "string".  I have tried to accomplish this using grep, but it is searching more than just .txt files.  Here is the command I have tried, but its not working correctly:

grep -lr "string" /root/volumes/ *txt
Question by:electricd7
    LVL 7

    Expert Comment


    I would go first for "find".

    I would search the files with the command find and then use xargs to grep the string you want.

    I attached a code snippet.

    Good Luck!
    find /path/ -type f -name "*.txt" | xargs grep "string"

    Open in new window

    LVL 37

    Expert Comment

    by:Gerwin Jansen
    Hi, I use a script called "mgrep" for this:

    for a in `find . -type f`; do file -bi $a | grep "^text" 2>&1 >/dev/null && grep -l "$1" $a; done;

    Open in new window

    Searches from current folder in all subfolders for string given as a parameter, like this:

    mgrep <string>

    It will print all text files that match
    LVL 48

    Expert Comment

    Problem is the space, so your grep is searching

    /root/volumes AND *txt

    should be
    grep -lr "string" /root/volumes/*txt

    Open in new window

    LVL 5

    Expert Comment

    #grep -i string */*.txt

    If you want to put the results on a file add >myoutput.log at the end of the command...

    #grep -i string */*.txt>myoutput.log

    -hope helps-
    LVL 23

    Expert Comment

    find . -type f | xargs grep -l "string"

    Open in new window

    LVL 19

    Accepted Solution

    There are two options - you only want to search files with the ".txt" extension, or you want to search any files that have text in (so don't check binary files).

    For the first:
    find /root/volumes -name "*.txt" -print0 | xargs -0 grep "string" /dev/null

    Open in new window

    (the "-print-" and "-0" options are to support filenames with spaces in them)

    To search *any* text files
    find /root/volumes -type f | xargs file | awk -F: '$2 ~ /text/{print $1}' | xargs grep "string" /dev/null

    Open in new window

    By the way, the extra /dev/null after the grep command is because grep only prints the filename for found strings if there is more than one filename after the grep.  If you have
        grep "string" file1 file2
    grep will preced each "string" line with the filename (here, file1 or file2).  If you have:
        grep "string" file1
    it will just print the text it finds, without a preceding filename.  This might happen if, for example, there *is* only one text file found, or if xargs splits the list of files up into chunks, and the last chunk contains just one name.

    One problem with the second command is that it will fail to handle filenames with spaces. If you need that, there is a fix I have for the awk command - let me know if you want it.
    LVL 19

    Expert Comment

    Thanks for the points!

    By the way, a version of the second command which handles filenames with spaces in is:
    find /root/volumes -type f -print0 | xargs -0 file | awk -F: '$2 ~ /text/{printf "%s%c",$1,0}' | xargs -0 grep "string" /dev/null

    Open in new window


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
    The purpose of this article is to demonstrate how we can use conditional statements using Python.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now