5.8 使用split分割大文件
早几年前,“文件分割”这4个字还是比较流行的,当时受限制于移动存储设备的限制,大文件的转移往往需要通过分割成小文件分别存储来实现,之后会再使用合并的方式还原成原始文件。虽然随着现代移动存储、网络存储的发展,分割大文件的做法已经不那么流行了,但是了解一下还是必要的。在Linux下使用split命令来实现文件的分割,支持按照行数分割和按照大小分割这两种模式。要说明的是,二进制文件因为没有“行”的概念,所以二进制文件无法使用行分割,而只能按照文件大小进行分割。相关命令如下所示:
#
假设文件中有一个512MB
的大文件
[root@localhost ~]# ll -h big_file.txt -rw-r—r— 1 root root 512M Jan 24 14:16 big_file.txt #
按照行进行分割,-l
参数指定每500
行为一个小文件
[root@localhost ~]# split -l 500 bigfile.txt small_file
#
分割完成后,当前目录下会生成很多小文件
[root@localhost ~]# ls smallfile*
small_file_aa small_file_ab small_file_ac small_file_ad ……(
略去内容)……
#
如果文件是二进制的,则只能按照文件大小分割
[root@localhost ~]# ll -h bigbin -rw-r—r— 1 root root 512M Jan 24 14:51 big_bin [root@localhost ~]# split -b 64m big_bin small_bin
#
分割完成后,当前目录下会生成很多大小为64MB
的文件
[root@localhost ~]# ll -h smallbin*
-rw-r—r— 1 root root 64M Jan 24 14:53 small_bin_aa -rw-r—r— 1 root root 64M Jan 24 14:53 small_bin_ab -rw-r—r— 1 root root 64M Jan 24 14:53 small_bin_ac -rw-r—r— 1 root root 64M Jan 24 14:53 small_bin_ad -rw-r—r— 1 root root 64M Jan 24 14:53 small_bin_ae -rw-r—r— 1 root root 64M Jan 24 14:53 small_bin_af -rw-r—r— 1 root root 64M Jan 24 14:53 small_bin_ag -rw-r—r— 1 root root 64M Jan 24 14:53 small_bin_ah ……(
略去内容)……