Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.

Loading

@conorliv
Copy link

conorliv commented Aug 25, 2015

Nice, thanks!

Loading

@danbower
Copy link

danbower commented Oct 21, 2015

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

Thanks!

Loading

@c9n
Copy link

c9n commented Nov 5, 2015

won't work on Mac OS X

Loading

@medington
Copy link

medington commented Jan 22, 2016

@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

Loading

@gwgundersen
Copy link

gwgundersen commented Jun 22, 2016

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

Loading

@joaomilho
Copy link

joaomilho commented Jul 29, 2016

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

Loading

@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.

Loading

@Rani-Kheir
Copy link

Rani-Kheir commented Oct 6, 2016

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

Loading

@jeromecovington
Copy link

jeromecovington commented Nov 30, 2017

Much appreciated!

Loading

@robertsheacole
Copy link

robertsheacole commented Apr 19, 2018

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.

Loading

@popovich-ai
Copy link

popovich-ai commented Jul 13, 2018

Thanks!!!

Loading

@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

Loading

@paulresdat
Copy link

paulresdat commented Oct 15, 2018

Gold! thanks buddy!

Loading

@shawnkendrick
Copy link

shawnkendrick commented Dec 18, 2018

Awesome, Thanks!

Loading

@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} .

Loading

@nadim
Copy link

nadim commented Jan 28, 2019

muchos gracias.

Loading

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