Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
3  
do you have more examples ? – Steve Schnepp Jul 30 '09 at 6:28
3  
then first, you have to learn how to ask the question, properly. technically, there is nothing wrong with it. the question is what you want to do. describe it properly and clearly – ghostdog74 Jul 30 '09 at 6:29
do you mean, what i get as answer? – cupakob Jul 30 '09 at 6:30
1  
Are you sure that grep -R isn't what you're looking for? – Dana the Sane Jul 30 '09 at 6:31
1  
I definitely just replaced "..." with "blah blah blah" in my head. Oh well. – Jeremy Powell Jul 30 '09 at 6:50
show 4 more comments

6 Answers

up vote 16 down vote accepted

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 '...'
share|improve this answer
grep -F '...' should work just as well – Hasturkun Jul 30 '09 at 7:38
@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 Jul 30 '09 at 8:46

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

share|improve this answer

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.

share|improve this answer

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 *
share|improve this answer
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 Jul 30 '09 at 6:39

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

grep -R "..." *
share|improve this answer

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

!

share|improve this answer
1  
More polite? Read The Friendly Manual? How could that be considered impolite? Polite freak. :) – Jeremy Powell Jul 30 '09 at 6:52
Man pages are indeed pretty friendly and are looking forward to being read again & again. (You made my day, Jeremy XD) – Isaac Clarke Jul 30 '09 at 7:00
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 Jul 30 '09 at 7:02
I assure you I will not sue you for having called (?) me a freak. – Isaac Clarke Jul 30 '09 at 7:07
Of course you won't. Then I'd counter sue for you negligent and distasteful use of acronomic foul language. – Jeremy Powell Jul 30 '09 at 7:11
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.