How to search through all files in a folder, recursively...
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
How to search through all files in a folder, recursively...
So I have a folder with files in it and these have files and folders which have folders with files... I have a complicated directory structure but I want to search through every file ending with a certain extension, they are text files and I want to find a certain word. What's the fastest way to do this through the command line?
With -exec, you are forking/execing as many processes as items found. This could be 10's or 100's or 1000's, and creates a substantially larger load on the system. Here's some proof that should convince you:
Code:
$ find . -type f | wc
4487 4487 152335
$ time find . -type f -exec grep -q fubar {} \;
real 0m11.974s
user 0m3.233s
sys 0m3.913s
$ time find . -type f -print0 | xargs -0 grep -q fubar
real 0m0.091s
user 0m0.000s
sys 0m0.097s
So, that's almost 12 seconds vs. about .1 second. Your method proves to be 120 times slower.
This is on a relatively unloaded system. With a system actually performing some work, the difference will be much greater.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.