Grep specific file types on MAC

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

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

So my question is: how can I grep for a specific file type on Mac?

+7
grep macos
source share
6 answers

The best way to do this is to combine the find and grep commands:

 find . -name "*.js" -exec grep --color=auto setHeader {} \; 
+5
source share

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

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

+6
source share

This works for me on Mac OS 10.8:

 grep --include=*.js -r setHeader . 
+2
source share

The problem is the value of "-R". When using this, you need to specify the directory. You will be better off using the find command:

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

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

0
source share

The error message indicates that there are no * .js files in your current directory. If you want to traverse the directory tree, you can do this:

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

Of course, you need to fill out the correct source directory, you can use . to indicate the directory in which you are currently located.

0
source share

This one works for me

 mdfind -name .extn | grep .extn$ 

Where extn is the type of file you are looking for (in my case .tre), and of course $ is the end of the line. Grep does not work inside quotes. ''

0
source share

All Articles