首页 文章

如何从grep -R中排除目录?

提问于
浏览
458

我想遍历所有子目录,除了“node_modules”目录 .

12 回答

  • 2

    SOLUTION 1 (combine find and grep)

    此解决方案的目的不是为了处理 grep 性能,而是为了展示一个可移植的解决方案:还应该使用busybox或早于2.5的GNU版本 .

    使用 find ,排除目录foo和bar:

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

    然后结合 find 和非递归使用 grep ,作为便携式解决方案:

    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):

    您已经知道这个解决方案,但我添加它,因为它是最新且最有效的解决方案 . 请注意,这是一种不太便携的解决方案,但更易于阅读 .

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

    SOLUTION 3 (Ag)

    如果您经常搜索代码,Ag (The Silver Searcher)是grep的一个更快的替代品,它是为搜索代码而定制的 . 例如,它会自动忽略 .gitignore 中列出的文件和目录,因此您不必继续将相同的繁琐排除选项传递给 grepfind .

  • 858

    最新版本的GNU Grep(> = 2.5.2)提供:

    --exclude-dir=dir
    

    这将从递归目录搜索中排除与模式 dir 匹配的目录 .

    所以你可以这样做:

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

    有关语法和用法的更多信息,请参阅

    对于较旧的GNU Greps和POSIX Grep,请按照其他答案中的建议使用 find .

    或者只是使用ackEdit :或The Silver Searcher)并完成它!

  • 0

    如果要排除多个目录:

    "r"表示递归,"l"只打印包含匹配项的文件名,"i"表示忽略大小写区别:

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

    示例:我想查找包含单词'hello'的文件 . 我想搜索我所有的linux目录 except proc 目录, boot 目录, sys 目录和 root 目录:

    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}

  • 13

    这个语法

    --exclude-dir={dir1,dir2}
    

    由shell(例如Bash)而不是 grep 扩展为:

    --exclude-dir=dir1 --exclude-dir=dir2
    

    引用会阻止shell扩展它,所以这不起作用:

    --exclude-dir='{dir1,dir2}'    <-- this won't work
    

    --exclude-dir 一起使用的模式与 --exclude 选项的手册页中描述的模式类型相同:

    --exclude=GLOB
        Skip files whose base name matches GLOB (using wildcard matching).
        A file-name glob can use *, ?, and [...]  as wildcards, and \ to
        quote a wildcard or backslash character literally.
    

    shell通常会尝试自己扩展这样的模式,所以为了避免这种情况,你应该引用它:

    --exclude-dir='dir?'
    

    您可以像这样使用花括号和引用排除模式:

    --exclude-dir={'dir?','dir??'}
    

    模式可以跨越多个路径段:

    --exclude-dir='some*/?lse'
    

    这将排除像 topdir/something/else 这样的目录 .

  • 1

    Frequently use this:

    grep 可以与 -r (递归), i (忽略大小写)和 -o (仅打印匹配的部分行)一起使用 . 要排除 files 使用 --exclude 并排除目录,请使用 --exclude-dir .

    把它放在一起你会得到类似的东西:

    grep -rio --exclude={filenames comma separated} \
    --exclude-dir={directory names comma separated} <search term> <location>
    

    描述它使它听起来比实际复杂得多 . 用一个简单的例子更容易说明 .

    Example:

    假设我正在为调试会话期间显式设置字符串值 debugger 的所有地方搜索当前项目,现在希望查看/删除 .

    我编写了一个名为 findDebugger.sh 的脚本,并使用 grep 查找所有出现的内容 . 然而:

    对于文件排除 - 我希望确保忽略 .eslintrc (这实际上有一个关于 debugger 的linting规则,因此应该被排除) . 同样,我不希望在任何结果中引用我自己的脚本 .

    对于目录排除 - 我希望排除 node_modules ,因为它包含许多引用 debugger 的库,我对这些结果不感兴趣 . 另外我只想省略 .idea.git 隐藏目录,因为我也不关心那些搜索位置,并希望保持搜索性能 .

    所以这是结果 - 我创建了一个名为 findDebugger.sh 的脚本:

    #!/usr/bin/env bash
    grep -rio --exclude={.eslintrc,findDebugger.sh} \
    --exclude-dir={node_modules,.idea,.git} debugger .
    
  • 2

    你可以尝试像 grep -R search . | grep -v '^node_modules/.*' 这样的东西

  • 167

    非常有用,特别是那些处理Node.js的人,我们想避免在"node_modules"内搜索:

    find ./ -not -path "*/node_modules/*" -name "*.js" | xargs grep keyword
    
  • 1

    这个适合我

    grep <stuff> -R --exclude-dir=<your_dir>

  • 4

    一个简单的工作命令:

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

    上面我在当前目录“dspace”中找到文本“creativecommons.org”并排除dirs {log,assetstore} .

    完成 .

  • 20
    find . ! -name "node_modules" -type d
    
  • 9

    如果您正在使用git存储库中的代码并且 node_modules 位于 .gitignore 中,则可以使用 git grep . git grep 搜索工作树中的跟踪文件,忽略 .gitignore 中的所有内容

    git grep "STUFF"
    
  • 61

    更简单的方法是使用“grep -v”过滤结果 .

    grep -i needle -R * | grep -v node_modules

相关问题