我有几个包含数千个文件的文件夹,每个文件夹的大小约为3-10GB。现在,我想将这些文件放在文件夹中,并且每个tar文件的大小应约为1GB。在后端,我想使用Python处理这些tar文件。
#!/bin/bash
dirlist=$(find $1 -mindepth 1 -maxdepth 1 -type d)
stored_date=$(date +%Y-%m-%d --date="-1 day")
#stored_date='2019-10-23'
for dir in $dirlist
do
(
cd $dir
tar_file=${PWD##*/}
tar_file="${tar_file}_${stored_date}.tar"
echo "${tar_file}"
tar -c $stored_date*.html --tape-length=1000M -f ${tar_file} --remove-files
)
done
创建1GB的块效果很好,但是使用``--tape-length''选项,Python遇到了各种问题
tarfile.ReadError:数据意外结束
(加上:我也想使用Python处理在tar归档文件边缘分割的文件)
是否有Linux解决方案?我找到了星星而不是焦油,但还没有尝试过-如果可能的话,我宁愿选择标准焦油。
如何在每个目录循环中嵌套第二个循环以跟踪每个文件的大小,然后再将其附加到tar文件中?这是我的意思的示意伪代码:
max_size=$((1024*1024*1024))
total_size=0
for dir in $dirlist ; do
for foo in $dir/*; do
this_size="$(stat -c"%s" $foo)"
if [ $(($total_size + $this_size)) -le $max_size ] ; then
tar --append ... $foo
total_size="$(($total_size + $this_size))"
else
# start new tar file here
tar -c ... $foo
total_size="$this_size"
fi
done
done
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句