利用split切割文件

演练场景

今天往客户服务器上传一个3GB文件的时候失败了,经确认是由于客户的vpn限制不允许上传200MB以上的文件,只能对文件进行切割成n个100MB文件,然后逐个上传。下面是大致的模拟过程,仅供日后参考。

处理过程

  • 第1步,利用 fallocate 快速创建一个大文件,比如 980MB 的 test.dat
[root@localhost ~]# fallocate -l 980M test.dat

[root@localhost ~]# du -sh test.dat 
980M    test.dat
  • 第2步,利用 split 对文件进行切割,示例:指定 -b 按文件大小切割,每个100MB,指定前缀_
[root@localhost ~]# split -b 100M -de test.dat test.dat_

[root@localhost ~]# ll test.dat_*
-rw-r--r--. 1 root root  104857600 Jun 27 16:32 test.dat_00
-rw-r--r--. 1 root root  104857600 Jun 27 16:32 test.dat_01
-rw-r--r--. 1 root root  104857600 Jun 27 16:32 test.dat_02
-rw-r--r--. 1 root root  104857600 Jun 27 16:32 test.dat_03
-rw-r--r--. 1 root root  104857600 Jun 27 16:32 test.dat_04
-rw-r--r--. 1 root root  104857600 Jun 27 16:32 test.dat_05
-rw-r--r--. 1 root root  104857600 Jun 27 16:32 test.dat_06
-rw-r--r--. 1 root root  104857600 Jun 27 16:32 test.dat_07
-rw-r--r--. 1 root root  104857600 Jun 27 16:32 test.dat_08
-rw-r--r--. 1 root root   83886080 Jun 27 16:32 test.dat_09
#.split参数详解
  -b,按照大小切割
  -n,按照分数切割
  -d,文件名后缀使用数字而不是默认的字母
  -e,禁止生成长度为0的文件,由于切割的最小单位是1kb,比如5kb的文件切割成8份就会产生3个0kb的文件
  • 注:也可以指定 -n 切割成n份
[root@localhost ~]# split -n 5 -de test.dat test.dat_

[root@localhost ~]# ll test.dat_*
-rw-r--r--. 1 root root  205520896 Jun 27 16:35 test.dat_00
-rw-r--r--. 1 root root  205520896 Jun 27 16:35 test.dat_01
-rw-r--r--. 1 root root  205520896 Jun 27 16:35 test.dat_02
-rw-r--r--. 1 root root  205520896 Jun 27 16:35 test.dat_03
-rw-r--r--. 1 root root  205520896 Jun 27 16:35 test.dat_04
  • 第3步,将切割后的小文件同步到目标机器

  • 第4步,在目标机器上,将切割后的文件合并成一个文件,并比对切割前后的md5值(必须一致)

[root@localhost ~]# cat test.dat_* > test.dat2

[root@localhost ~]# md5sum test.dat
b8286e3ede44b7fbca124714d3272f07  test.dat

[root@localhost ~]# md5sum test.dat2
b8286e3ede44b7fbca124714d3272f07  test.dat2
Copyright © www.sqlfans.cn 2023 All Right Reserved更新时间: 2023-06-27 16:49:24

results matching ""

    No results matching ""