Search all files in a folder tree

Posted on 2004-11-08
Last Modified: 2007-10-18
What is the command line input to search for a word in a folder tree, in other words, in all the files in all the folders under a specified folder.
Question by:bob1010
    LVL 2

    Expert Comment

    find /path/where/you/want/to/look -name "word"               for looking for a filename. (you can use wildcards)
    LVL 3

    Accepted Solution

    If you are looking for this word inside of your files (contents) then you can do the following:

    find /path/to/your/folder -exec grep -l "word" {} \;

    That will give you a list of files which contains your "word" inside starting with /path/to/your/folder and deeper.
    If you want to filter and check inside of *.txt files then you can do like this:

    find /path/to/your/folder -name "*.txt" -exec grep -l "word" {} \;

    That willd do the same thing, but will search in *.txt files only in all subdirectories starting with /path/to/your/folder and deeper.
    LVL 38

    Expert Comment

    find /path/to/your/folder  -type f | xargs grep -l "word"

    man find
    man xargs
    to learn more details
    LVL 23

    Expert Comment

    This is the Linux forum, and Linux uses gnu grep which supports recursive search.
    So you can simply do:

    grep -r "word" /path/to/your/folder

    LVL 12

    Expert Comment

    Depending whether you're looking for an entire word, you could also use the -w option, perhaps paired with -i:

    find folder -type f | xargs grep -wil word
    LVL 4

    Expert Comment

    Most linux installs also have the find-utils and updatedb installed by default.  You can use these for simple file name searching and pipe the results through grep =)  If you are looking for keyword dearches within the files then someone above already covered the recursive grep.  Otherwise if just searching for file names, you may have to first make sure that updatedb (run as root) has been run recently and it will create a file table database.  I think that SuSE and Redhat by default ran the updatedb every night at midnight in a cron ... but if not take a minute to run updatedb as root.  Once it's done, here are some eample searches:

    locate *.doc | grep "path/you/want"
    locate word
    locate word | grep "/specific/path/to/list"

    You can use pattern matching like "locate file*" or "locate *file*" or even "locate *.xl?"

    Hope that helps!

    LVL 16

    Expert Comment

    Are you looking for files whose names match a particular string, or files that *contain* a particular string?  The "solutions" above are mixed regarding this, and require clarification...



    Expert Comment

    find / *.zip

    would be an example

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    In this tutorial I will explain how to make squid prevent malwares in five easy steps: Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-…
    Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now