?
Solved

Search all files in a folder tree

Posted on 2004-11-08
8
Medium Priority
?
4,800 Views
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.
0
Comment
Question by:bob1010
8 Comments
 
LVL 2

Expert Comment

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

Accepted Solution

by:
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.
0
 
LVL 38

Expert Comment

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

man find
man xargs
to learn more details
0
 [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

by:brettmjohnson
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

0
 
LVL 12

Expert Comment

by:stefan73
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
0
 
LVL 4

Expert Comment

by:avatech
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!

Dean
0
 
LVL 16

Expert Comment

by:The--Captain
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...

Cheers,
-Jon

0
 

Expert Comment

by:soldato
ID: 12746898
find / *.zip

would be an example
0

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