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


当前回答

步骤1:

vim ~ / . bash_profile

search() {
    grep -InH -r --exclude-dir=*build*  -e "$1" .
}

步骤2:

源~ / . bash_profile

用法:

搜索”< string_to_be_searched >”

其他回答

步骤1:

vim ~ / . bash_profile

search() {
    grep -InH -r --exclude-dir=*build*  -e "$1" .
}

步骤2:

源~ / . bash_profile

用法:

搜索”< string_to_be_searched >”

这个语法

--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??'}

一个简单的工作命令:

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

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

完成了。

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

find . ! -name "node_modules" -type d