删除指定后缀的文件脚本

!/bin/bash

使用方法: ./delete_files.sh /path/to/directory with|without extension

给脚本赋予可执行权限:chmod +x delete_files.sh

DIRECTORY="1" CONDITION="2"
EXTENSION="$3"

检查参数

if [ -z "DIRECTORY" ] || [ -z "CONDITION" ] || [ -z "EXTENSION" ]; then echo "Usage:0 /path/to/directory with|without extension"
echo "'with' : Delete files with the specified extension."
echo "'without' : Delete files without the specified extension."
exit 1
fi

根据条件删除文件

if [ "CONDITION" == "with" ]; then # 删除指定后缀的文件 find "DIRECTORY" -type f -name "*.EXTENSION" -exec rm -f {} + echo "Deleted all *.EXTENSION files in DIRECTORY" elif [ "CONDITION" == "without" ]; then
# 删除非指定后缀的文件
find "DIRECTORY" -type f ! -name "*.EXTENSION" -exec rm -f {} +
echo "Deleted all files NOT with *.EXTENSION inDIRECTORY"
else
echo "Invalid condition: $CONDITION. Use 'with' or 'without'."
exit 1
fi

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容