排除不適用於 grep 指令的子目錄

排除不適用於 grep 指令的子目錄

這對我有用,因為它從搜尋的根目錄中排除“foo”目錄:

grep -rn --exclude-dir=foo 'custom'

但這不起作用:

grep -rn --exclude-dir=foo/bar 'custom'

但仍搜尋“foo/bar”目錄。我也嘗試過用引號:

grep -rn --exclude-dir='foo/bar' 'custom'

我正在使用 Ubuntu 20。

更新

雖然不完美,但我使用了這個解決方法:

grep -rn 'custom'|grep -v 'foo/bar'

這將無法找到同時包含“foo/bar”和“custom”的行。

答案1

我剛剛遇到了同樣的問題,透過呼叫解決了

grep -rn --exclude="**/foo/**" 'custom'

答案2

使用此運算子"*variable*"作為通配符來匹配要從 grep 搜尋中排除的任何目錄。

就你而言,

grep -rn --exclude-dir={"*foo*"} 'custom'將排除名稱為“foo”的目錄或子目錄。

grep -rn --exclude-dir={"*foo*","*bar*"} 'custom'將排除名稱為“foo”和“bar”的任何目錄和子目錄。

相關內容