效果图
?
步骤
- 创建多个非空的文件夹:
/tmp/project/
/tmp/project/d1/ /tmp/project/d1/index.html /tmp/project/d1/logo.png
/tmp/project/d2/ /tmp/project/d2/py.mp4 /tmp/project/d2/php.mp4 - 创建脚本文件zipSubFolders.sh,并赋予执行权限sudo chmod +x ./zipSubFolders.sh,内容如下:
#!/bin/bash
# Check for the parameter
if [[ ! -n $1 ]] || [[ ! -d $1 ]];then
echo '[ERROR] An invalid full path given'
echo ''
exit
fi
# Remove the slash in the end
baseDir=$1
if [[ $baseDir =~ ^.*\/+$ ]];then
baseDir=${baseDir%/*}
fi
echo '[Base-Folder] '$baseDir
echo ''
# Loop and zip
cd $baseDir
dirList=`ls -p $baseDir | grep / | tr -d /`
index=1
#total=${#dirList[*]}
total=`ls -p $baseDir | grep / | wc -l`
for dir in $dirList
do
echo '[Zip] ('$index' / '$total') ./'$dir/
tar -cvzf ./$dir.tar.gz ./$dir/
echo ''
let index++
done
# Done
echo '[Done]'
echo '' - 加上参数,运行脚本:
./zipSubFolders.sh /tmp/project/ - OK
?
|