grep recursively to look for occurrence of a word in a particular file

3

I know about

grep -rin 'text' /path/to/direcotry

But I dont want to look up in all the files . I just need to check for occurrence of 'text' in all files named wscript. How can that be done ?

Vihaan Verma

Posted 2012-09-23T04:38:53.987

Reputation: 329

Answers

4

man grep:

   --include=GLOB
          Search  only  files whose base name matches GLOB (using wildcard
          matching as described under --exclude).

so try this:

grep -rin --include=wscript 'text' /path/to/files 

Serge

Posted 2012-09-23T04:38:53.987

Reputation: 2 585

2

 grep -rin 'text' `find /path/to/directory -name wscript`

Isaac Rabinovitch

Posted 2012-09-23T04:38:53.987

Reputation: 2 645

1did'nt work man. – Vihaan Verma – 2012-09-23T04:46:09.177

Note that the quotes around the find command are backquotes not regular quotes. – Isaac Rabinovitch – 2012-09-23T05:02:11.847

1The backticks are hard to read, I'd suggest using $() instead. I'd also recommend not to use command substitution for parsing find output, it'll likely break. A safer alternative would be to pipe it to xargs in a null-delimited format. – slhck – 2012-09-23T08:14:07.890

I rolled this back to my version. @slhck 's version is fine, but it's completely different, and belongs in its own answer. – Isaac Rabinovitch – 2012-09-23T22:20:09.670

Note that @Serge's answer is better than mine. I even upvotted it! – Isaac Rabinovitch – 2012-09-23T22:21:53.677

Sure, but it's nothing completely different, just the same command in a different version. I've just seen a lot of examples online that are "good enough" or work for a certain situation but are not portable. In you case, if you didn't have the restriction on -name wscript, and there was a file named foo bar, this would lead to two errors and the file not being searched. – slhck – 2012-09-24T06:23:54.603

I'll agree that my version is not the best way to do it. But I see your version as a very basic change, not a revision. – Isaac Rabinovitch – 2012-09-24T16:19:40.707

0

grep -rin 'your-text' /path/to/text/file/text-file.txt

It works only with text files, though.

Jonathan Reno

Posted 2012-09-23T04:38:53.987

Reputation: 318

That doesn't restrict the files by name. – Isaac Rabinovitch – 2012-09-23T05:43:39.393