How can I exclude directories from grep

我想遍历除“node_modules”目录外的所有子目录。


SOLUTION 1 (combine find and grep )

The purpose of this solution is not to deal with grep performance but to show a portable solution : should also work with busybox or GNU version older than 2.5.

Use find , for excluding directories foo and bar :

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

Then combine find and the non-recursive use of grep , as a portable solution :

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

SOLUTION 2 (recursive use of grep ):

You know this solution already, but I add it since it's the most recent and efficient solution. Note this is a less portable solution but more human-readable.

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

SOLUTION 3 (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 .


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!


    If you want to exclude multiple directories :

    "r" for recursive, "l" to print only names of files containing matches and "i" to ignore case distinctions :

    
    grep -rli --exclude-dir={dir1,dir2,dir3} keyword /path/to/search
    
    

    Example : I want to find files that contain the word 'hello'. I want to search in all my linux directories except proc directory, boot directory, sys directory and root directory :

    
    grep -rli --exclude-dir={proc,boot,root,sys} hello /
    
    

    Note : The example above needs to be root

    Note 2 (according to @skplunkerin) : do not add spaces after the commas in {dir1,dir2,dir3}

    链接地址: http://www.djcxy.com/p/8292.html

    上一篇: 如何从git grep搜索中排除某些目录/文件

    下一篇: 我如何从grep中排除目录