gpt4 book ai didi

bash 根据内容删除目录

转载 作者:行者123 更新时间:2023-12-04 06:19:24 24 4
gpt4 key购买 nike

目前我有多个目录

Directory1 Directory2 Directory3 Directory4

这些目录中的每一个都包含文件(这些文件有点神秘)

我希望做的是扫描文件夹中的文件以查看某些文件是否存在,如果存在,则不要理会该文件夹,如果某些文件不存在,则只需删除整个目录。这就是我的意思:

我正在搜索带有 .pass 字样的文件。在文件名中。
说目录 4 有我正在寻找的那个文件
Direcotry4:    
file1.temp.pass.exmpl
file1.temp.exmpl
file1.tmp

其余的目录没有该特定文件:
file.temp
file.exmp
file.tmp.other

所以我想删除 Directory1、2 和 3 但只保留 Directory 4 ...

到目前为止,我已经想出了这个代码

(arr 是所有目录名称的数组)
for x in ${arr[@]}
do
find $x -type f ! -name "*pass*" -exec rd {} $x\;
done

我想到的另一种方法是这样的:
for x in ${arr[@]}
do

cd $x find . -type f ! -name "*Pass*" | xargs -i rd {} $x/
done

到目前为止,这些似乎都不起作用,我害怕我可能做错了什么并删除了我所有的文件......(我已经备份)

有什么办法可以做到这一点吗?记住我希望目录 4 保持不变,我想保留其中的所有内容

最佳答案

要查看您的目录是否包含传递文件:

if [ "" = "$(find directory -iname '*pass*' -type f |  head -n 1)" ]
then
echo notfound
else
echo found
fi

要在循环中执行此操作:
for x in "${arr[@]}"
do
if [ "" = "$(find "$x" -iname '*pass*' -type f | head -n 1)" ]
then
rm -rf "$x"
fi
done

关于bash 根据内容删除目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6786616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com