56

How do I recursively grep files within a given folders except a couple file types?

For example, I'm looking for a string within my workspace folder but it ends up searching inside sql files and generates serialized strings.

So in this case, I'd like to grep the workspace folder except sql files.

I'm preferably looking for a one-liner if possible.

Kevin
  • 40,767
chrisjlee
  • 8,523
  • 6
    Be aware that Linux and Unix and unix-a-likes don't really have "file types" in the way you seem to want. It's only by convention that file names have a ".c" or ".txt" or ".sql" suffix - those suffixes aren't even Windows-style "extensions", much less indicators that SQL or text or C source code live inside the files. –  Feb 08 '12 at 23:35

2 Answers2

76

If you have GNU grep you can use the --exclude=GLOB option, like

grep -r --exclude='*.sql' pattern dir/
jw013
  • 51,212
11

This will do that for you and exclude .sql and .txt files:

find /some/dir -type f ! -name '*\.sql' ! -name '*.txt' -print0 | xargs -0 grep 'foobar'

However it sounds like ack would be a far better tool for what you're trying to do:

ack -a --nosql 'foobar' /some/dir