Linux下如何切割与合并大文件

x33g5p2x  于2022-02-07 转载在 Linux  
字(5.8k)|赞(0)|评价(0)|浏览(244)

我们传输一个大文件时,有时网络比较慢,需要花费很长时间才能传输完成,或者传输的过程中,网络不稳定,有可能导致此次传输失败,针对这种情况,我们可以把大文件切分成小文件,再逐个的传输到目的地,最后再把它们合并成一个文件

小文件传输有什么优点呢?当出现网络闪断导致传输失败了,也只需要重新传输失败的一个文件,由于文件比较小,重新传输相对大文件要快很多,另外,切割成小文件,可以增加传输的并发量,也就是说多个小文件同时传输,比传输单个文件速度更快

Linux下切割文件的命令是 split 合并文件可以使用 cat 命令,下面将介绍这两个命令的使用以及切割和合并文件的方法

语法

split 命令的语法如下:

split [OPTION]... [INPUT [PREFIX]]

INPUT 表示标准输入或者文件

PREFIX 表示大文件分割后产生的小文件名字的前缀,默认是小写字母 x,前缀后跟一组字符 , 按照类似 aaabac 字母顺序组成一个一个的文件名,比如:切割成三个文件,它们的文件名默认就是 xaa、 xab、 xac

OPTION 表示命令的选项,比如:按字节切割文件,按文件行切割文件等,下面列出了一些常用的选项

选项说明
-l每个文件都包含指定的行数
-b生成指定大小的文件,单位:字节
-a文件名后缀的长度,默认是2
-d文件名后缀使用数字而不是默认的字母
-n生成指定数量的文件
-e禁止生成长度为 0 的文件
--verbose创建文件时输出日志

切割文件实例

下面来看几组 split 命令的使用实例吧

  • 按文件大小切割

首先创建一个 10M 大小的文件

关于如何创建指定大小的文件可以参考 1s 创建100G文件,最快的方法是?

[root@localhost split_test]# fallocate -l 10M myfile
[root@localhost split_test]# ls -lh
总用量 10M
-rw-r--r-- 1 root root 10M 9月  30 11:18 myfile
[root@localhost split_test]#

myfile 文件分割成若干个小文件,每个文件大小为 2M

[root@localhost split_test]# split -b 2M myfile 
[root@localhost split_test]# ls -lh
总用量 20M
-rw-r--r-- 1 root root  10M 9月  30 11:18 myfile
-rw-r--r-- 1 root root 2.0M 9月  30 11:23 xaa
-rw-r--r-- 1 root root 2.0M 9月  30 11:23 xab
-rw-r--r-- 1 root root 2.0M 9月  30 11:23 xac
-rw-r--r-- 1 root root 2.0M 9月  30 11:23 xad
-rw-r--r-- 1 root root 2.0M 9月  30 11:23 xae

从上述结果可以看出,输入文件 myfile 大小为 10M , 选项 " -b 2M " 表示每个输出文件 2M, 总共切割成 5 个文件,文件名分别是 xaa、xab、xac、xad、xae

  • 按文件行数切割

首先创建一个 10K 大小的文件, 文件的每一行内容都是 "this is a test file"

[root@localhost split_test]# yes "this is a test file" | head -c 10K > numfile 
[root@localhost split_test]# ls -lh
总用量 12K
-rw-r--r-- 1 root root 10K 9月  30 11:46 numfile
[root@localhost split_test]# wc -l numfile 
512 numfile

从结果可以得知,numfile 文件大小为 10K, 总共有 512 行, 命令 wc -l numfile 是查询 numfile 文件的总行数

numfile 文件切割成若干文件,每个文件 100 行, 并且新生成的文件名字前缀为 "split_file_", 具体的命令以及执行结果如下:

[root@localhost split_test]# split -l 100 numfile split_file_
[root@localhost split_test]# ls -lh
总用量 36K
-rw-r--r-- 1 root root  10K 9月  30 11:46 numfile
-rw-r--r-- 1 root root 2.0K 9月  30 11:54 split_file_aa
-rw-r--r-- 1 root root 2.0K 9月  30 11:54 split_file_ab
-rw-r--r-- 1 root root 2.0K 9月  30 11:54 split_file_ac
-rw-r--r-- 1 root root 2.0K 9月  30 11:54 split_file_ad
-rw-r--r-- 1 root root 2.0K 9月  30 11:54 split_file_ae
-rw-r--r-- 1 root root  240 9月  30 11:54 split_file_af
[root@localhost split_test]# wc -l split_file_aa
100 split_file_aa
[root@localhost split_test]# wc -l split_file_ab
100 split_file_ab
[root@localhost split_test]# wc -l split_file_ac
100 split_file_ac
[root@localhost split_test]# wc -l split_file_ad
100 split_file_ad
[root@localhost split_test]# wc -l split_file_ae
100 split_file_ae
[root@localhost split_test]# wc -l split_file_af
12 split_file_af

从结果可以知道,总共512行的文件 numfile 被分成了 6 个文件,文件名分别是 split_file_aa、 split_file_ab、 split_file_ac、 split_file_ad、 split_file_ae、 split_file_af , 其中前5个文件每个文件都是 100 行,最后一个文件只有剩下的 12 行

  • 按文件数量切割

选项 -n 可以控制文件切割成小文件的数量

[root@localhost split_test]# fallocate -l 5M cntfile
[root@localhost split_test]# ls -lh
总用量 5.0M
-rw-r--r-- 1 root root 5.0M 9月  30 12:51 cntfile
[root@localhost split_test]# split -d -n 5 cntfile 
[root@localhost split_test]# ls -lh
总用量 10M
-rw-r--r-- 1 root root 5.0M 9月  30 12:51 cntfile
-rw-r--r-- 1 root root 1.0M 9月  30 12:58 x00
-rw-r--r-- 1 root root 1.0M 9月  30 12:58 x01
-rw-r--r-- 1 root root 1.0M 9月  30 12:58 x02
-rw-r--r-- 1 root root 1.0M 9月  30 12:58 x03
-rw-r--r-- 1 root root 1.0M 9月  30 12:58 x04

fallocate -l 5M cntfile 命令是创建一个 5M 大小的文件 cntfile

split -d -n 5 cntfile 命令是把 cntfile 文件切割成 5 个小文件, -d 选项表示文件名使用数字后缀

通过切割后的结果可以知道,切割后生成了 5 个文件,他们分别是 x00、x01、x02、x03、x04 ,每个文件大小是 1M

  • 禁止生成 0 长度的文件

在上面 按文件数量切割 小节中,存在一种特殊情况,文件的大小不足以分成指定数量的小文件,比如:一个 5 字节的文件,要切割成 8 个文件,切割的最小单位是 1 字节,所以最多只能切割成 5 个文件,要切割成 8 个文件的话,那么剩下的 3 个文件大小只能是 0 字节

上述空文件即使生成了,也没什么意义,我们可以用 -e 选项来禁止生成空文件,请看下面的实例

上图中 fallocate -l 5 testfile 表示创建一个大小为 5 字节大小的文件 testfile

split --verbose -n 8 testfile 表示把 testfile 文件切割成 8 个小文件, --verbose 选项是输出创建新文件的日志

从上图可以看出,执行命令后,共创建了 8 个文件,它们分别是 xaa、 xab、 xac 、xad 、xae、 xaf、 xag、 xah , 每个文件的大小是怎样的呢, 继续看下图

上图中 ls -lh 命令结果输出了切割之后各个文件的详细信息, 从中可以得出, 前 5 个文件 ( xaa、 xab、 xac 、xad 、xae ) 大小均为 1 字节, 后三个文件,也就是图中红框中的文件 ( xaf、 xag、 xah ) 大小均为 0 字节

0 字节的文件并不包含任何内容,也不需要进行传输,所以,不需要生成它们, 我们可以用 -e 选项来禁止生成 0 字节的文件

我们先删除切割之后的小文件,再执行 split --verbose -e -n 8 testfile命令,具体的结果如下:

从上图可以看出,加上 -e 选项之后,只生成了 5 个文件,分别是 xaa、 xab、 xac 、xad 、xae, 每个文件的大小为 1 字节, 没有出现 0 字节大小的文件了

切割与合并

大文件切割成许多小文件,通过网络全部传输到远程机器上之后,需要把它们合并成一个大文件,并且合并之后的大文件与原始的大文件要一模一样,下面我们通过一个实例来说明整个过程

1、在本地生成一个 1G 大小的文件

[root@localhost split_test]# dd if=/dev/urandom of=bigfile bs=1M count=1024
记录了1024+0 的读入
记录了1024+0 的写出
1073741824字节(1.1 GB)已复制,87.5173 秒,12.3 MB/秒
[root@localhost split_test]# ls -lh
总用量 1.0G
-rw-r--r-- 1 root root 1.0G 9月  30 14:41 bigfile

2、计算出本地文件 bigfile 的 MD5, 用于后面与远程机器上大文件的校验

[root@localhost split_test]# md5sum bigfile 
4b06ddf4eeecbf26f36fd3ddad331deb  bigfile

3、把 bigfile 文件切割成 100M 大小的小文件

[root@localhost split_test]# split -b 100M bigfile 
[root@localhost split_test]# ls -lh
总用量 2.0G
-rw-r--r-- 1 root root 1.0G 9月  30 14:41 bigfile
-rw-r--r-- 1 root root 100M 9月  30 14:44 xaa
-rw-r--r-- 1 root root 100M 9月  30 14:44 xab
-rw-r--r-- 1 root root 100M 9月  30 14:44 xac
-rw-r--r-- 1 root root 100M 9月  30 14:44 xad
-rw-r--r-- 1 root root 100M 9月  30 14:44 xae
-rw-r--r-- 1 root root 100M 9月  30 14:44 xaf
-rw-r--r-- 1 root root 100M 9月  30 14:44 xag
-rw-r--r-- 1 root root 100M 9月  30 14:44 xah
-rw-r--r-- 1 root root 100M 9月  30 14:44 xai
-rw-r--r-- 1 root root 100M 9月  30 14:44 xaj
-rw-r--r-- 1 root root  24M 9月  30 14:44 xak

4、将切割之后的文件 xaa、 xab、 xac、 xad、 xae、 xaf、 xag、 xah、 xai、 xaj、 xak 逐个传输到远程机器的 merge_test 目录中,这里省略了传输过程

5、进入远程机器的 merge_test 目录,把 xaa、 xab、 xac、 xad、 xae、 xaf、 xag、 xah、 xai、 xaj、 xak 合并成一个文件

[root@localhost merge_test]# cat x* > remote_bigfile
[root@localhost merge_test]# ls -lh
总用量 2.0G
-rw-r--r-- 1 root root 1.0G 9月  30 14:54 remote_bigfile
-rw-r--r-- 1 root root 100M 9月  30 14:53 xaa
-rw-r--r-- 1 root root 100M 9月  30 14:53 xab
-rw-r--r-- 1 root root 100M 9月  30 14:53 xac
-rw-r--r-- 1 root root 100M 9月  30 14:53 xad
-rw-r--r-- 1 root root 100M 9月  30 14:53 xae
-rw-r--r-- 1 root root 100M 9月  30 14:53 xaf
-rw-r--r-- 1 root root 100M 9月  30 14:53 xag
-rw-r--r-- 1 root root 100M 9月  30 14:53 xah
-rw-r--r-- 1 root root 100M 9月  30 14:53 xai
-rw-r--r-- 1 root root 100M 9月  30 14:53 xaj
-rw-r--r-- 1 root root  24M 9月  30 14:53 xak

6、计算合并后 remote_bigfile 文件的 MD5

[root@localhost merge_test]# md5sum remote_bigfile 
4b06ddf4eeecbf26f36fd3ddad331deb  remote_bigfile

7、比较本地机器上 bigfile 文件和 远程机器上 remote_bigfile 文件的 MD5, 如果相同,表示传输成功,如果不一样,表示传输失败

根据 步骤 2 和 步骤 6 的结果, bigfileremote_bigfile 的 MD5 都是 4b06ddf4eeecbf26f36fd3ddad331deb, 所以此次传输成功

小结

本文介绍了文件切割命令的用法,以及切割、传输、合并、校验的整个流程,文中实例中用到的 利用 fallocatedd 以及 yes 创建文件可以参考 1s 创建100G文件,最快的方法是?

相关文章