Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all files matching 'name' on linux system, and search with them for 'text'

I need to find all instances of 'filename.ext' on a linux system and see which ones contain the text 'lookingfor'.

Is there a set of linux command line operations that would work?

like image 702
siliconpi Avatar asked Sep 24 '10 11:09

siliconpi


People also ask

How do I find all files containing specific text?

Without a doubt, grep is the best command to search a file (or files) for a specific text. By default, it returns all the lines of a file that contain a certain string. This behavior can be changed with the -l option, which instructs grep to only return the file names that contain the specified text.

How do you search for a word in all files in a directory in Linux?

Search All Files in Directory To search all files in the current directory, use an asterisk instead of a filename at the end of a grep command. The output shows the name of the file with nix and returns the entire line.


1 Answers

find / -type f -name filename.ext -exec grep -l 'lookingfor' {} + 

Using a + to terminate the command is more efficient than \; because find sends a whole batch of files to grep instead of sending them one by one. This avoids a fork/exec for each single file which is found.

A while ago I did some testing to compare the performance of xargs vs {} + vs {} \; and I found that {} + was faster. Here are some of my results:

time find . -name "*20090430*" -exec touch {} + real    0m31.98s user    0m0.06s sys     0m0.49s  time find . -name "*20090430*" | xargs touch real    1m8.81s user    0m0.13s sys     0m1.07s  time find . -name "*20090430*" -exec touch {} \; real    1m42.53s user    0m0.17s sys     0m2.42s 
like image 179
dogbane Avatar answered Nov 07 '22 02:11

dogbane