Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using grep to search files provided by find: what is wrong with find . | xargs grep '...'?

When I use the command:

find . | xargs grep '...' 

I get the wrong matches. I'm trying to search for the string ... in all files in the current folder.

like image 775
cupakob Avatar asked Jul 30 '09 06:07

cupakob


People also ask

How do I use grep to search for a file?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

What is find grep?

The grep command can search for a string in groups of files. When it finds a pattern that matches in more than one file, it prints the name of the file, followed by a colon, then the line matching the pattern.

What is the difference between grep and find?

The main difference between the two is that grep is used to search for a particular string in a file whereas find is used to locate files in a directory, etc. also you might want to check out the two commands by typing 'man find' and 'man grep'.

What is grep command explain with example?

The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for global search for regular expression and print out). Syntax: grep [options] pattern [files]


1 Answers

As Andy White said, you have to use fgrep in order to match for plain ., or escape the dots.

So you have to write (-type f is to only have the files : you obviously don't want the directories.) :

find . -type f | xargs fgrep '...' 

or if you still want to use grep :

find . -type f | xargs grep '\.\.\.' 

And if you only want the current directory and not its subdirs :

find . -maxdepth 1 -type f | xargs fgrep '...' 
like image 91
Steve Schnepp Avatar answered Nov 09 '22 12:11

Steve Schnepp