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


当前回答

解决方案1(结合find和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 \;

解决方案2(使用grep的——exclude-dir选项):

你已经知道这个解了,但我加上它,因为它是最新的和有效的解。注意,这是一个不太可移植的解决方案,但更适合人类阅读。

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

要排除多个目录,使用——exclude-dir as:

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

方案3(银)

如果你经常在代码中搜索,Ag(银色搜索器)是一个比grep更快的替代品,它是为搜索代码而定制的。例如,它会自动忽略.gitignore中列出的文件和目录,因此您不必一直向grep或find传递同样麻烦的排除选项。

其他回答

一个简单的工作命令:

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

上面我grep文本“creativecommons.org”在当前目录“dspace”和排除dirs{日志,资产存储}。

完成了。

你可以试试grep -R搜索。| grep -v '^node_modules/.*'

这个对我很有用:

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

如果你在git存储库中grep代码,而node_modules在你的.gitignore中,你可以使用git grep。Git grep在工作树中搜索被跟踪的文件,忽略来自.gitignore的所有文件

git grep "STUFF"

解决方案1(结合find和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 \;

解决方案2(使用grep的——exclude-dir选项):

你已经知道这个解了,但我加上它,因为它是最新的和有效的解。注意,这是一个不太可移植的解决方案,但更适合人类阅读。

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

要排除多个目录,使用——exclude-dir as:

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

方案3(银)

如果你经常在代码中搜索,Ag(银色搜索器)是一个比grep更快的替代品,它是为搜索代码而定制的。例如,它会自动忽略.gitignore中列出的文件和目录,因此您不必一直向grep或find传递同样麻烦的排除选项。