How can I exclude directories from grep -R?

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

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

相关标签:
13条回答
  • 2020-12-04 04:48

    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}

    0 讨论(0)
  • 2020-12-04 04:49

    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 (using the --exclude-dir option 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
    

    To exclude multiple directories, use --exclude-dir as:

    --exclude-dir={node_modules,dir1,dir2,dir3}

    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.

    0 讨论(0)
  • 2020-12-04 04:51

    You could try something like grep -R search . | grep -v '^node_modules/.*'

    0 讨论(0)
  • 2020-12-04 04:51

    Very useful, especially for those dealing with Node.js where we want to avoid searching inside "node_modules":

    find ./ -not -path "*/node_modules/*" -name "*.js" | xargs grep keyword
    
    0 讨论(0)
  • 2020-12-04 04:51

    A simple working command:

    root/dspace# grep -r --exclude-dir={log,assetstore} "creativecommons.org"
    

    Above I grep for text "creativecommons.org" in current directory "dspace" and exclude dirs {log,assetstore}.

    Done.

    0 讨论(0)
  • 2020-12-04 04:57

    This one works for me:

    grep <stuff> -R --exclude-dir=<your_dir>
    
    0 讨论(0)
提交回复
热议问题