[英]How to delete directories where files within directories don't have a certain regex match?
Say I have the following directories:假设我有以下目录:
dir > subdir1 > k.txt > "Keep please"
> subdir2 > l.txt > "Keep me"
> subdir3 > m.txt > "Delete me"
> subdir4 > n.txt > "remove"
I want to find an expression that will let me do a regex match on the contents within the file, and delete the subdirectories that do not have that string match.我想找到一个表达式,让我对文件中的内容进行正则表达式匹配,并删除没有该字符串匹配的子目录。 So in the example above, I would use something like
grep -r "Keep.*
for the files and them only rm
subdir3. I want to end up with:因此,在上面的示例中,我将使用
grep -r "Keep.*
之类的文件,并且它们只有rm
subdir3。我想结束:
dir > subdir1 > k.txt > "Keep please"
> subdir2 > l.txt > "Keep me"
Here is a shell script approach:-这是一个 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
I commented rm
statement, you can first run the script and carefully verify the print
statement output to make sure it is picking the right directory names.我注释了
rm
语句,您可以先运行脚本并仔细验证print
语句 output 以确保它选择了正确的目录名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.