TIMEX TIMEX - 5 months ago 8
Linux Question

How can I exclude directories from grep -R?

I want to traverse all subdirectories, except the "node_modules" directory.

Answer

FIND excluding directories foo and bar :

find /dir \( -name foo -prune \) -o \( -name bar -prune \) -o -name "*.sh" -print

GREP -R:
you know already

Combine FIND and GREP :

find /dir \( -name node_modules -prune \) -o -name "*.sh" -exec grep --color -Hn "your text to find" {} 2>/dev/null \;

Ag

If you frequently search through code, Ag (The Silver Searcher) is a much faster alternative to grep, that's customized for searching code. For instance, it automatically ignores files and directories listed in .gitignore, so you don't have to keep passing the same cumbersome exclude options to grep or find.

Comments