我遇到了使用shell脚本的场景。如果它具有相同的名称,我需要删除该文件夹和Zip。任何人都可以帮助我。Shell脚本删除包含相同名称的文件夹中的文件夹和Zip文件
例
下面是该脚本需要在目录中搜索相同的名称(这里需要打印和删除例子,example.zip
)的目录路径
Path:/tmp/test/
/tmp/test/example
/tmp/test/example.zip
/tmp/test/zack
我遇到了使用shell脚本的场景。如果它具有相同的名称,我需要删除该文件夹和Zip。任何人都可以帮助我。Shell脚本删除包含相同名称的文件夹中的文件夹和Zip文件
例
下面是该脚本需要在目录中搜索相同的名称(这里需要打印和删除例子,example.zip
)的目录路径
Path:/tmp/test/
/tmp/test/example
/tmp/test/example.zip
/tmp/test/zack
您可以使用脚本如下:
#!/bin/bash
parent_dir=/tmp/test/
for i in `ls /tmp`
do
if [[ $i != *.zip ]]; then
if [[ -f /tmp/$i.zip ]]; then
array=$array" "$i
fi
fi
done
array=($array)
for i in ${array[@]}
do
rm -r $parent_dir$i
rm -r $parent_dir$i".zip"
done
这应该做一个技巧,即使适用于cronjob:
find $DIR_PATH -type d -exec sh -c '[ -f "{}.zip" ] && rm -fr {}.zip {}' \;
只需设置DIR_PATH到您选择搜索
谢谢你的帮助 – user3156326
谢谢你的脚本 – user3156326