压缩多个文件夹到一个ZIP
#!/bin/bash
# 定义目标目录
target_dir="/Users/yuqiu/****/"
output_dir="$target_dir/ZIPDIR"
# 获取当前日期
current_date=$(date +"%Y-%m-%d")
# 定义压缩文件名
zip_filename="XX_${current_date}.zip"
# 检查目标目录是否存在
if [ ! -d "$target_dir" ]; then
echo "目标目录不存在: $target_dir"
exit 1
fi
# 检查要压缩的目录是否存在
if [ ! -d "$target_dir/needZipFolder1" ] || [ ! -d "$target_dir/needZipFolder2" ]; then
echo "要压缩的目录不存在"
exit 1
fi
# 检查并创建输出目录
if [ ! -d "$output_dir" ]; then
mkdir -p "$output_dir"
if [ $? -ne 0 ]; then
echo "无法创建输出目录: $output_dir"
exit 1
fi
fi
# 切换到需要压缩文件的目录
cd "$target_dir"
# 创建压缩文件,压缩指定的两个文件夹
zip -r "$output_dir/$zip_filename" "needZipFolder1" "needZipFolder2"
# 检查压缩是否成功
if [ $? -eq 0 ]; then
echo "压缩成功: $output_dir/$zip_filename"
else
echo "压缩失败"
exit 1
fi
标签:常用,target,zip,echo,Mac,sh,fi,output,dir
From: https://www.cnblogs.com/aeolian/p/18253651