Created
January 10, 2021 03:09
-
-
Save alfredhot/f58c18a6890a03c3903145e0f7b85d98 to your computer and use it in GitHub Desktop.
打包文件夹较多的路径
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
##################### | |
# author: alfredhot | |
# date: 2021-01-10 | |
# desc: 打包较大的文件夹. 路径下文件夹和文件过多, 在转移位置的时候很容易出问题, 一个文件出错将导致整个复制过程中断. | |
# 所以将路径下包含文件过多的文件夹打包(不将所有文件夹都打包是因为有些文件夹下文件并不多, 打包浪费时间, 将来使用的时候挨个文件夹解压将十分繁琐.) | |
###################### | |
# FOLDER_ARR 装载的数组将包含空格, 所以分隔符临时换做\n | |
IFS=$'\n' | |
FOLDER_ARR=$() | |
idx=0 | |
# 获取当前路径下的文件夹名称 | |
for folder in * | |
do | |
if test -d "$folder" | |
then | |
FOLDER_ARR[$idx]="$folder" | |
((idx++)) | |
fi | |
done | |
IFS=$OIFS | |
# 打包文件夹并删除源文件的命令 | |
tarFolder(){ | |
tar -cvzf $1.gzip.tar $1 | |
rm -rf $1 | |
} | |
for folderName in ${FOLDER_ARR[@]}; | |
do | |
echo "$folderName" | |
if test -d $folderName | |
then | |
# 文件夹下文件夹的个数, 若存在文件夹就架设文件数比较多 | |
folderCount=$(ls -l $folderName | grep "^d" | wc -l | bc -l) | |
# 文件个数, 多于十个就打包 | |
fileCount=$(ls -l $folderName | wc -l | bc -l) | |
if [ $folderCount -gt 0 ] | |
then | |
echo "------tar $folderName" | |
tarFolder $folderName | |
elif [ $fileCount -gt 10 ] | |
then | |
echo "------tar $folderName ----------" | |
tarFolder $folderName | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment