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)

大家都在看

  • Tensorflow

    1.什么是Tensorflow? Tensor(张量)意味着 N 维数组,Flow(流)意味着基于数据流图的计算,TensorFlow即为张量从图的一端流动到另一端。 它支持CNN…

    Linux 2023年6月6日
    0101
  • redis服务器

    这一次主要讲下redis中服务器这个结构体相关代码,主要从是代码层面进行讲解 redis服务器 redis服务器结构体主要代码在 redis.h/redisServer,下面给出该…

    Linux 2023年6月13日
    0106
  • linux 添加java环境

    将下载的jdk解压到/usr/local/下 设置软链接 在/etc/profile.d/java.sh 添加如下内容(建议在/etc/profile.d/添加环境变量) 生效配置…

    Linux 2023年6月8日
    077
  • 如何使用 systemctl 管理服务

    systemd是一个服务管理器,目前已经成为Linux发行版的新标准。它使管理服务器变得更加容易。了解并利用组成systemd的工具将有助于我们更好地理解它提供的便利性。 syst…

    Linux 2023年6月13日
    091
  • [20220303]oracle如何定位使用library cache mutex 3.txt

    [20220303]oracle如何定位使用library cache mutex 3.txt –//这个问题实际上困扰我很久,我开始以为library cache b…

    Linux 2023年6月13日
    088
  • cache和内存屏障

    1 cache简介 1.1 cache缓存映射规则 tag查看cache是否匹配,set index |tag |set index |block offset ||20-bit …

    Linux 2023年6月6日
    0123
  • 继承、封装、多态的实现原理

    欢迎来到Java学习之继承、封装、多态的实现原理 目录 从JVM结构开始谈多态 JVM 的结构 Java 的方法调用方式 常量池(constant pool) 图 2. 常量池各表…

    Linux 2023年6月13日
    0127
  • redis限流的3种实现方式

    Redis限流的实现方式有3种,分别是:1、基于Redis的setnx的操作,给指定的key设置了过期实践;2、基于Redis的数据结构zset,将请求打造成一个zset数组;3、…

    Linux 2023年5月28日
    0104
  • .NET服务治理之限流中间件-FireflySoft.RateLimit

    FireflySoft.RateLimit自2021年1月发布第一个版本以来,经历了多次升级迭代,目前已经十分稳定,被很多开发者应用到了生产系统中,最新发布的版本是3.0.0。 它…

    Linux 2023年6月13日
    0110
  • IDEA maven项目导包报红线

    原因:没有导入maven包 最后有设置一次,以后就无需设置 1、问题 2、打开Setting 3、将下载好的maven包导入 配置一次,就不用每次都需要配置 1、如果在这个界面先关…

    Linux 2023年6月14日
    0126
  • Xshell Win10不能直接拖文件进行文件传输解决办法

    centos安装了 lrzsz后, xshell还是无法直接从win10中拖文件 到centos。 需要修改win10的注册表。 见: https://blog.csdn.net/…

    Linux 2023年5月28日
    0103
  • 缓存提升性能的关键性手段

    提高「性能」的主要方式是优化,而优化的其中一个主要手段就是添加缓存! 在软件工程里有这么一句话:「没有银弹」!就是说由于软件工程的复杂性,没有任何一种技术或方法能解决所有问题!软件…

    Linux 2023年6月14日
    0102
  • MIT6.828——Lab2(麻省理工操作系统实验)

    Lab2 Lab2 是关于操作系统存储管理的细节。主要是建立内存模型,页表,物理地址映射等。在Lab2之前,请复习好前序知识: Lab2内存管理准备知识 MIT6.828——Lab…

    Linux 2023年5月27日
    0120
  • 大数库GMP测试

    任务详情 在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务 用自己8位学号建两个文件夹xxxxxxxxsrc,xxxxxxxx,到GMP官网htt…

    Linux 2023年6月8日
    0105
  • JavaScript快速入门-07-异常处理与调试

    7、异常处理与调试 7.1 异常处理 7.1.1 try/catch语句 try/catch语句常用于处理JavaScript中的异常,其基本语法如下所示: try { // 可能…

    Linux 2023年6月7日
    0119
  • Red Hat Enterprise Linux (RHEL) 9 更新了什么,即 Rocky Linux 9 和 AlmaLinux 9 展望

    请访问原文链接:https://sysin.org/blog/rhel-9-vision/,查看最新版。原创作品,转载请保留出处。 作者:gc(at)sysin.org,主页:ww…

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