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.

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 '...'

'.' matches any character, so you'll be finding all lines that contain 3 or more characters.

You can either escape the dots, like this:

find . | xargs grep '\.\.\.'

Or you can use fgrep, which does a literal match instead of a regex match:

find . | xargs fgrep '...'

(Some versions of grep also accept a -F flag which makes them behave like fgrep.)


@OP, if you are looking for files that contain ...,

grep -R "\.\.\." *

If you are literally typing grep '...' you'll match just about any string. I doubt you're actually typing '...' for your grep command, but if you are, the ... will match any three characters.

Please post more info on what you're searching for, and maybe someone can help you out more.


If you're looking for a filename that matches, try:

find . -name "filename pattern" 

or

find . | grep "filename pattern"

If your looking for looking for files that match (ie it contains the grep string)

find . | xargs grep "string pattern"

works fine. or simply:

grep "string pattern" -R *

To complete Jeremy's answer, you may also want to try

find . -type f | xargs grep 'your_pattern'

or

find . -type f -exec grep 'your_pattern' {} +

Which is similar to a xargs

I might add : RTFM ! Or in a more polite way : use & abuse of

man command

!


Need Your Help

Hide all modal view controllers

iphone ios objective-c cocoa-touch modalviewcontroller

i've a login view presented as a ModelViewController and i have a register view presented as a NavigationControlloer on top of it:

File Upload in Elm

elm

How does one upload a file (image or excel) in Elm?