views:

1245

answers:

6

When i use the command above, i get wrong matches.....can someone explain me, what is wrong?

I'm trying to search for the string "..." in all files in the current folder.

+2  A: 

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.

Andy White
+1  A: 

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 *
Jeremy Powell
yes, I'm looking for files that contains "..." as string, but:1. find . | xargs grep "string pattern"or 2. grep "string pattern" -R *doesn't help.
cupakob
+7  A: 

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 '...'
Steve Schnepp
`grep -F '...'` should work just as well
Hasturkun
@Hasturkun: Yes. I just have the tendency to prefer different command names than extra options (like using `gunzip` instead of `gzip -d`). Just a matter of taste usually.
Steve Schnepp
+5  A: 

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

Laurence Gonsalves
A: 

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

!

Isaac Clarke
More polite? Read The Friendly Manual? How could that be considered impolite? Polite freak. :)
Jeremy Powell
Isaac Clarke
The later it gets, the more obnoxious my comments seem to get. I think I'm peaking now. It's all downhill from here.
Jeremy Powell
I assure you I will not sue you for having called (?) me a freak.
Isaac Clarke
Of course you won't. Then I'd counter sue for you negligent and distasteful use of acronomic foul language.
Jeremy Powell
Aaaaaand now we're off-topic. :P
Jeremy Powell
Back on topic. When using -exec the command needs to end with a \;
Mark Thalman
I tested this command before posting, and it worked well. (Running a shell on OpenSuSE11). Could you explain, please ? ;)
Isaac Clarke
A: 

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

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