[英]How to delete directories where files within directories don't have a certain regex match?
假设我有以下目录:
dir > subdir1 > k.txt > "Keep please"
> subdir2 > l.txt > "Keep me"
> subdir3 > m.txt > "Delete me"
> subdir4 > n.txt > "remove"
我想找到一个表达式,让我对文件中的内容进行正则表达式匹配,并删除没有该字符串匹配的子目录。 因此,在上面的示例中,我将使用grep -r "Keep.*
之类的文件,并且它们只有rm
subdir3。我想结束:
dir > subdir1 > k.txt > "Keep please"
> subdir2 > l.txt > "Keep me"
这是一个 shell 脚本方法:-
#!/bin/ksh
for dir in <your directory name here>/*
do
printf "Checking dir: ${dir}..."
if grep -q "Keep" ${dir}/*
then
print " [Keep]"
# do nothing
else
print " [Remove]"
# rm -rf ${dir}
fi
done
我注释了rm
语句,您可以先运行脚本并仔细验证print
语句 output 以确保它选择了正确的目录名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.