10

I have a Mac OS X snow leopard. I am using the xterm terminal to grep specific string using this command grep -R --color=auto setHeader *.js but when I run this command I get the error saying

grep: \*.js: No such file or directory

So my question is how can I grep for a specific filetype on Mac?

alex
  • 479,566
  • 201
  • 878
  • 984
Amrish
  • 679
  • 1
  • 8
  • 20

6 Answers6

8

grep --include=*.js -R . --color=auto setHeader

Since the example given uses "file type = file extension", this should just work.

Andy Finkenstadt
  • 3,547
  • 1
  • 21
  • 25
6

This one works for me on Mac OS 10.8:

grep --include=*.js -r setHeader .
yegor256
  • 102,010
  • 123
  • 446
  • 597
6

The best way to do that is to combine the find and the grep command:

find . -name "*.js" -exec grep --color=auto setHeader {} \;
Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
0

The problem is the "-R" flag. When using that, you need to specify a directory. You'd be better off using a find command:

find . -name "*.js" -exec grep "setHeader" '{}' \;

If you want to use grep, just use "." instead of "*.js" for the file pattern.

Jeremy Whitlock
  • 3,808
  • 26
  • 16
0

The error message says that there are no '*.js' files in your current directory. If you want to traverse a directory tree, then you could do this:

find <startdirectory> -name '*.js' -exec grep --color=auto setHeader '{}' ';'

Of course you need to fill in the correct startdirectory, you can use . to denote the directory you're currently in.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
0

This one works for me

mdfind -name .extn | grep .extn$

Where extn is the file type you are searching for (in my case .tre) and of course $ is end of line. The grep doesnt work inside quotes ''

M__
  • 614
  • 2
  • 10
  • 25