grep specific filetypes on MAC
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 f开发者_运维问答ile or directory
So my question is how can I grep for a specific filetype on Mac?
grep --include=*.js -R . --color=auto setHeader
Since the example given uses "file type = file extension", this should just work.
The best way to do that is to combine the find
and the grep
command:
find . -name "*.js" -exec grep --color=auto setHeader {} \;
This one works for me on Mac OS 10.8:
grep --include=*.js -r setHeader .
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.
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.
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 ''
精彩评论