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

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

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

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

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 选项来禁止生成空文件,请看下面的实例

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

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

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

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

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

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

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

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

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

从上图可以看出,加上 -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文件,最快的方法是?

Original: https://www.cnblogs.com/wanng/p/split-and_merge-file.html
Author: Linux开发那些事儿
Title: Linux下如何切割与合并大文件

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/606810/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • [数据库]ACID、CAP、BASE特性

    SQL与noSQL 对比项 关系型SQL 非关系型SQL 关系表 数据集(键值/JSON文档/哈希表/其它) 结构化、提前定义表结构 动态调整模式,非结构化 纵向扩展,提高处理能力…

    Linux 2023年6月13日
    087
  • CentOS 7.6 安装 MySQL-5.7.31(RPM方式安装)

    准备工作: 注:5.7.31版本安装步骤及初始化和之前版本有较大区别 CentOS 7.6 系统: 带GUI的服务器 默认安装 MySQL 5.7.31 安装包: 1.RPM安装包…

    Linux 2023年6月8日
    085
  • JVM 配置参数 -D,-X,-XX 的区别

    转载请注明出处: 最近在安全护网行动,需要针对服务进行不断的安全加固,如 对服务的 log4j 的安全配置进行防护,对 fastjson 的漏洞进行安全加固等,最快的防护方法就是通…

    Linux 2023年6月14日
    0102
  • LeetCode-16. 最接近的三数之和

    题目来源 题目详情 给你一个长度为 n 的整数数组 nums和 一个目标值 target。请你从 nums 中选出三个整数,使它们的和与 target 最接近。 返回这三个数的和。…

    Linux 2023年6月7日
    0100
  • 前端奇奇怪怪的CSS样式

    使用inline-block相当于将元素介于块级元素与行内元素之间,将换行符转换成了空格,因此各个元素之间会有空隙 各个元素没有间隙,且元素自身大小不会改变,若一行容不下,则会换行…

    Linux 2023年6月13日
    091
  • Linux 服务器安全(基本)

    Original: https://www.cnblogs.com/libin-linux/p/16656644.htmlAuthor: LB_运维技术Title: Linux 服…

    Linux 2023年6月13日
    094
  • TCP 粘包-拆包问题及解决方案

    歧义在”TCP”上,这个”粘包”跟TCP其实没关系。这里的”粘包”其实是应用程序中没有处理好数据包分割,两个…

    Linux 2023年6月13日
    0105
  • Debian 9.4 安装教程

    我们这里选择install安装,不装桌面,因为是做服务器,装桌面没意义。 我们这里选择装英文版,你也可以装中文版本。 手动配置网络-manually 设置IP 设置 子网掩码 设置…

    Linux 2023年6月13日
    093
  • Redis阻塞操作实现原理(转)

    原文:https://www.jianshu.com/p/xsMzfn 作者:Haiger 最近一位朋友问到:既然Redis是单线程的工作模式,那像 _BLPOP_这样的阻塞操作又…

    Linux 2023年5月28日
    094
  • Quartus II 13.0 sp1的官方下载页面

    今天为了下个ModelSim跑到网上去找下载资源,清一色的百度网盘,下载速度60k/s,简直有病,于是跑到Intel官网上把连接挖出来了,供各位直接下载 实测使用IDM多线程下载速…

    Linux 2023年6月13日
    0170
  • 离线版centos8安装docker笔记

    嗨嗨哈哈,已经很久没有坐下来胡编乱造一点笔记了,平时云服务器搞惯了,一个命令就安装好了docker了的,但这次生不逢时的新机房就没那么幸运了,有多不逢时超乎想象,不仅仅服务器没有外…

    Linux 2023年5月27日
    0131
  • 解决报错 Microsoft Visual C++ 14.0 is required

    环境:Surface Windows 10 专业版 问题:安装 Python3 的第三方库 py7zr 时不成功。而报错的是另外一个依赖库 pycryptodomex distut…

    Linux 2023年6月14日
    0110
  • gitlab备份迁移与升级

    bash;gutter:false; 升级计划: https://docs.gitlab.com/ee/update/index.html#upgrade-paths</p&…

    Linux 2023年6月7日
    068
  • Linux磁盘管理

    一、磁盘管理 Linux 磁盘管理好坏直接关系到整个系统的性能问题。 Linux 磁盘管理常用的三个命令为 df、 du 和 fdisk。 df(英文全称:disk full):列…

    Linux 2023年5月27日
    096
  • 1:文件与目录

    CD 切换当前工作目录 mkdir 创建目录 re -dir 删除目录 pwd 打印当前工作目录 绝对路径和相对路径 硬链接 和软链接 CP拷贝 MV 移动 dirname 和 b…

    Linux 2023年6月7日
    0139
  • MySQL的主从复制+双主模式

    部署环境: MySQL master 192.168.40.21 MySQL slave 192.168.40.22 思路: I/O线程是对主MySQL上二进制日志文件进行读取,读…

    Linux 2023年6月8日
    0106
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球