Skip to content

Instantly share code, notes, and snippets.

@a1phanumeric
Created April 9, 2013 14:35
Show Gist options
  • Star 67 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save a1phanumeric/5346170 to your computer and use it in GitHub Desktop.
Save a1phanumeric/5346170 to your computer and use it in GitHub Desktop.
Grep exclusions. Demonstrates how to exclude multiple directories, and files.
grep -r --color --exclude-dir={custom,lib,scripts} --exclude={*.xml,error_log} "beta" .
@gbutiri
Copy link

gbutiri commented Jul 3, 2015

Thanks! much appreciated.

@conorliv
Copy link

Nice, thanks!

@danbower
Copy link

This is the first hit on google for "grep exclude multiple directories".

Thanks!

@c9n
Copy link

c9n commented Nov 5, 2015

won't work on Mac OS X

@medington
Copy link

@c9n (or any OS X user)- If you use Homebrew, you can get the standard GNU grep with:

 brew install grep --with-default-names

If you want to go "all-in" on GNU tools see: Install and Use GNU Command Line Tools on Mac OS X

@gwgundersen
Copy link

I was tired of always typing --exclude-dir= and wrote a small wrapper for grep that reads excludes from config files. Maybe others will find it helpful: https://github.com/gwgundersen/gp

@joaomilho
Copy link

@gwgundersen gp sounds nice, but it is very similar to git grep in practice.

@jpuccino
Copy link

jpuccino commented Sep 8, 2016

The --exclude-dir={...} now works on OSX, as far as I can tell. I didn't manually override it with brew as previously suggested, and this is a new machine on El Capitan.

@Rani-Kheir
Copy link

Rani-Kheir commented Oct 6, 2016

Thanks! First result for "grep ignore multiple directories" ^_^

@jeromecovington
Copy link

Much appreciated!

@robertsheacole
Copy link

Just a heads up if you didn't notice that you can't have spaces between those folders after the comma. It throws a parse error.

@popovich-ai
Copy link

Thanks!!!

@tsboh
Copy link

tsboh commented Sep 12, 2018

"The --exclude-dir={...} now works on OSX, as far as I can tell." from @jpucinno same for me.
on OSX it works

@paulresdat
Copy link

Gold! thanks buddy!

@shawnkendrick
Copy link

Awesome, Thanks!

@zhoulaosan0
Copy link

zhoulaosan0 commented Jan 9, 2019

This works both on Mac OS and Linux :)

# --color: highlight matched string
# -r: recursive search
# -i: ignore case
# --exclude: exclude specified files
# --exclude-dir: exclude specified dir

grep --color -r -i "foo\|bar" --exclude={\*.csv,\*.txt} --exclude-dir={.git,.idea,vendor} .

@nadim
Copy link

nadim commented Jan 28, 2019

muchos gracias.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment