How can I exclude directories from grep -R?

前端 未结 13 2142
感动是毒
感动是毒 2020-12-04 04:11

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

13条回答
  •  眼角桃花
    2020-12-04 05:03

    Recent versions of GNU Grep (>= 2.5.2) provide:

    --exclude-dir=dir
    

    which excludes directories matching the pattern dir from recursive directory searches.

    So you can do:

    grep -R --exclude-dir=node_modules 'some pattern' /path/to/search
    

    For a bit more information regarding syntax and usage see

    • The GNU man page for File and Directory Selection
    • A related StackOverflow answer Use grep --exclude/--include syntax to not grep through certain files

    For older GNU Greps and POSIX Grep, use find as suggested in other answers.

    Or just use ack (Edit: or The Silver Searcher) and be done with it!

提交回复
热议问题