我有一个名为的目录dir1
,其中包含约800个子目录disp-001, disp-002, ... disp-800
。我需要找到子目录
stdout
或str1
。标识不包含文件的子目录将在另一个问题中回答
$ find . -type d \! -exec test -e '{}/stdout' \; -print
但是,如果我尝试在上述命令中包含grep,它将无法正常工作
$ find . -type d \! -exec test -e 'grep str1 {}/stdout' \; -print
如何包含字符串搜索以将感兴趣的目录返回给我?
您可以在那里修改任何解决方案,例如
使用( -exec
或 -exec )
与slm或patrick的解决方案配合使用(第二个exec
仅在第一个返回false
时-print
执行,仅在其中一个返回时执行true
):
find . -type d \( ! -exec test -f '{}/stdout' \; -o ! -exec grep -q str1 '{}/stdout' \; \) -print
甚至更短,如Costas所建议的:
find . -type d \! -exec grep -q 'str1' {}/stdout 2>/dev/null \; -print
与terdon解决方案一起使用条件:
for d in **/
do
if [[ ! -f "$d"stdout ]] then
printf '%s\n' "$d"
else
grep -q str1 "$d"stdout || printf '%s\n' "$d"
fi
done
或者,使用zsh
:
print -rl **/*(/e_'[[ ! -f $REPLY/stdout ]] || ! grep -q str1 $REPLY/stdout'_)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句