Search all files in a folder tree

Posted on 2004-11-08
Medium Priority
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

Expert Comment

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

Accepted Solution

nochkin earned 1000 total points
ID: 12528966
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

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

man find
man xargs
to learn more details
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

LVL 23

Expert Comment

ID: 12529496
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

ID: 12535621
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

Expert Comment

ID: 12549260
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

ID: 12591531
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

ID: 12746898
find / *.zip

would be an example

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Fine Tune your automatic Updates for Ubuntu / Debian
This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

864 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