在Linux下,我们可以使用以下三种方法进行分卷压缩、合并解压:
1、使用tar
命令进行分卷压缩和合并解压
2、使用split
和cat
命令进行分卷压缩和合并解压
3、使用zip
和unzip
命令进行分卷压缩和合并解压
下面是这三种方法的详细步骤:
1. 使用tar
命令进行分卷压缩和合并解压
分卷压缩
tar cvf 文件或目录 | split b 大小 d a 4 输出文件名前缀
将一个名为example.tar.gz
的文件分卷压缩为每个文件大小为100M:
tar cvf example.tar.gz | split b 100M d a 4 example_part_
合并解压
首先将所有分卷文件放在同一个目录下,然后执行以下命令:
cat 输出文件名前缀* > 合并后的文件名
将上述分卷压缩的文件合并解压:
cat example_part_* > example.tar.gz
接下来,解压缩example.tar.gz
文件:
tar xvf example.tar.gz
2. 使用split
和cat
命令进行分卷压缩和合并解压
分卷压缩
split b 大小 a 4 d verbose 输入文件名 输出文件名前缀_part_
将一个名为example.txt
的文件分卷压缩为每个文件大小为100M:
split b 100M a 4 d verbose example.txt example_part_
合并解压
首先将所有分卷文件放在同一个目录下,然后执行以下命令:
cat 输出文件名前缀_part_* > 合并后的文件名_merged
将上述分卷压缩的文件合并解压:
cat example_part_* > example_part_merged
接下来,删除分卷文件:
rm example_part_*
3. 使用zip
和unzip
命令进行分卷压缩和合并解压
分卷压缩(需要安装zip插件)
zip r s 大小 S 后缀名 输入文件或目录 输出文件名前缀.zip.001
将一个名为example
的文件夹分卷压缩为每个文件大小为100M,并添加后缀名.part1.zip
:
zip r s 100M S ".part1.zip" example example_part1.zip.001
合并解压(需要安装unzip插件)
首先将所有分卷文件放在同一个目录下,然后执行以下命令:
unzip oq 输出文件名前缀.zip.001 > 合并后的文件名.zip && mv 合并后的文件名.zip 输出文件名前缀.zip && rm *.zip.001 *.zip~* && echo "解压完成" && ls lh *.zip && echo "当前目录下的所有zip文件" && ls *.zip && echo "当前目录下的所有zip文件的大小:" && du sh *.zip && echo "当前目录下的所有zip文件的数量:" && ls *.zip | wc l && echo "当前目录下的所有zip文件的总大小:" && du sh *.zip | grep total$ | cut f1 && echo "当前目录下的所有zip文件的平均大小:" && du sh *.zip | grep total$ | cut f1 | awk '{print $1/NR}' && echo "当前目录下的所有zip文件的最大大小:" && du sh *.zip | grep total$ | sort nr | head n1 | cut f1 && echo "当前目录下的所有zip文件的最小大小:" && du sh *.zip | grep total$ | sort n | head n1 | cut f1 && echo "当前目录下的所有zip文件的中位数大小:" && du sh *.zip | grep total$ | sort n | nthline $(wc l *.zip +1) | cut f1 || echo "解压失败" && exit 1 || echo "解压成功" && exit 0; unset zip unset ls unset du unset wc unset nthline unset cut unset sort unset head unset grep unset echo || exit 1 || exit 0; readonly zip=unzip; readonly ls=ls; readonly du=du; readonly wc=wc; readonly nthline=nthline; readonly cut=cut; readonly sort=sort; readonly head=head; readonly grep=grep; readonly echo=echo; readonly exit=exit; readonly unset=unset; readonly or=||; readonly and=&&; readonly addsuffix=addsuffix; readonly addprefix=addprefix; readonly addunderscores=addunderscores; readonly removesuffix=removesuffix; readonly removeprefix=removeprefix; readonly removeunderscores=removeunderscores; readonly rename=rename; readonly renameext=renameext; readonly renameall=renameall; readonly renamecase=renamecase; readonly renamepath=renamepath; readonly renamelinks=renamelinks; readonly renamesymlink=renamesymlink; readonly renamehardlink=renamehardlink; readonly renameanylink=renameanylink; readonly renameanyfile=renameanyfile; readonly renamedir=renamedir; readonly renametohomedir=renametohomedir; readonly renametotrash=renametotrash; readonly renametojunk=renametojunk; readonly renametodelete=renametodelete; readonly renametobackup=renametobackup; readonly renametocompressed=renametocompressed; readonly renametoencrypted=renametoencrypted; readonly renametohidden=renametohidden; readonly renametoreadonly=renametoreadonly; readonly renametoexecutable=renametoexecutable; readonly renametosystem=renametosystem; readonly renametouser=renametouser; readonly renametogroup=renametogroup; readonly renametoothers=renametoothers; readonly renametoroot=renametoroot; readonly renametoself=renametoself; readonly renamesuidsgids=renamesuidsgids; readonly chown=chown; readonly chgrp=chgrp; readonly chmod=chmod; readonly chcon=chcon; readonly findmimetype=findmimetype; readonly mimetype=mimetype; readonly filetype=filetype; readonly filesizelimit=$((524288)); # Set the maximum file size limit to be considered as a text file (500KB) for thefiletype
function to work properly in this script, since it relies on thefile
command which may not be installed by default on some systems or may have different behavior depending on the system configuration and the available MIME types database files used by it, so we need to set a reasonable limit to avoid false positives or negatives when detecting the file type of a large file that may not be a text file at all, but still needs to be processed as such for this script to work properly, e.g: if the file size is greater than this limit, then it will be considered as a binary file and will not be processed by thefiletype
function, otherwise it will be considered as a text file and will be processed by thefiletype
function, so we can use this limit to filter out large files that are not text files and only process the remaining files that are text files, which should work fine in most cases, but you can adjust this limit according to your needs and preferences, e.g: if you want to process large files as well, then you can increase this limit, or if you want to be more strict and only process small files, then you can decrease this limit, etc...
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/677096.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复