封尘网

让学习成为一种习惯!

UNIX系统的有用的拆分命令示例

顾名思义,“ split ”命令用于在Linux和UNIX系统中将文件拆分或分解为多个部分。每当我们使用split命令分割大文件时,分割输出文件的默认大小为1000行,其默认前缀为'x'。

 

常用的参数也就几个,具体看帮助文件。

 

示例1

将文件拆分为小块,默认每个文件1000行

[root@backup02 ~]# split services    #services文件有11176,这样一拆分就有12个文件了
[root@backup02 ~]# ll
total 670293
-rw-r--r--  1 root root    670293 Jul  6 15:33 services
-rw-r--r--  1 root root     56446 Oct 11 17:49 xaa
-rw-r--r--  1 root root     58205 Oct 11 17:49 xab
-rw-r--r--  1 root root     58110 Oct 11 17:49 xac
-rw-r--r--  1 root root     57577 Oct 11 17:49 xad
-rw-r--r--  1 root root     56111 Oct 11 17:49 xae
-rw-r--r--  1 root root     57155 Oct 11 17:49 xaf
-rw-r--r--  1 root root     60009 Oct 11 17:49 xag
-rw-r--r--  1 root root     63535 Oct 11 17:49 xah
-rw-r--r--  1 root root     63379 Oct 11 17:49 xai
-rw-r--r--  1 root root     64313 Oct 11 17:49 xaj
-rw-r--r--  1 root root     63946 Oct 11 17:49 xak
-rw-r--r--  1 root root     11507 Oct 11 17:49 xal

 

输出拆分的文件过程

[root@c7-node1 test]# split services --verbose
creating file ‘xaa’
creating file ‘xab’
creating file ‘xac’
creating file ‘xad’
creating file ‘xae’
creating file ‘xaf’
creating file ‘xag’
creating file ‘xah’
creating file ‘xai’
creating file ‘xaj’
creating file ‘xak’
creating file ‘xal’

 

指定拆分的行数

[root@c7-node1 test]# split -l5000 services --verbose
creating file ‘xaa’
creating file ‘xab’
creating file ‘xac’

 

以大小方式拆分

此处的单位k,m,g大小写都可以

  • -b 以bytes字节方式

    split -b200000 文件名
    

     

  • -b nK 以KB方式

    split -b 50m 文件名
    

     

  • -b nM 以MB方式

    split -b 50m 文件名
    

     

  • -b nG 以GB方式

    split -b 50g 文件名
    

     

以数字结尾拆分文件

如果不喜欢拆分出来的文件是以字母结尾的可以使用参数-d来改成数字结尾,默认为字母。

split -d 文件名

 

使用自定义的文件名前缀

通过自定义拆分后的文件名前缀可以更方便了解到这几个文件是哪个文件拆分的,好比多个拆分的压缩文件。

  • 以字母结尾

    split 原文件名 [原文件名]_ -b 50M
    

    效果:

    [root@c7-node1 test]# split gogs.tar.gz gogs_ -d -b 50M
    [root@c7-node1 test]# ll
    total 204616
    -rw-r--r-- 1 root root  52428800 Oct 12 14:42 gogs_00
    -rw-r--r-- 1 root root  52333056 Oct 12 14:42 gogs_01
    

     

  • 以数字结尾

    split 原文件名 [原文件名]_ -d -b 50M
    

    效果:

    [root@c7-node1 test]# ll
    total 204616
    -rw-r--r-- 1 root root  52428800 Oct 12 14:43 gogs_aa
    -rw-r--r-- 1 root root  52333056 Oct 12 14:43 gogs_ab
    -rw------- 1 root root 104761856 Oct 12 14:42 gogs.tar.gz
    

     

指定拆分的份数

如果觉得指定行数,大小来拆分比较麻烦,我们可以直接指定份数;它会自动按比较拆分。

split -n5 文件名

 

注意

如果当文件大小已经为零了,就是文件是空的,还要再拆分的话,默认还是会分出来的。可以通过参数-e判断文件大小是否为零,如果为零时就不会再拆分了。

[root@c7-node1 test]# touch file 
[root@c7-node1 test]# split -n5 -e file 
[root@c7-node1 test]# ll
total 102308
-rw-r--r-- 1 root root         0 Oct 12 14:55 file
-rw------- 1 root root 104761856 Oct 12 14:42 gogs.tar.gz

 

实用的一个技巧

 

经常会遇到上传一个大文件到服务器,比如:上传一个系统你会发现超过了4G,无法上传。所以只能通过其它的方式上传。结合本次的split命令即可以很方便对大文件拆分,方便上传;上传到服务器再合并。

 

上传系统到Linux机器

  • 先做文件做一个md5值计算,本次在Windows下操作,上传到Linux机器

    Administrator@SYT-20190919QLI MINGW64 /d/Git_work
    $ md5sum.exe CentOS-7-x86_64-DVD-1908.iso
    dc5932260da8f26bcdce0c7ebf0f59ca *CentOS-7-x86_64-DVD-1908.iso
    

     

  • 拆分文件[5份]

    split.exe -n5 CentOS-7-x86_64-DVD-1908.iso
    

     

  • 上传文件并合并

    [root@c7-node1 test]# ll
    -rw-r--r-- 1 root root  932813209 Oct 12 15:09 xaa
    -rw-r--r-- 1 root root  932813209 Oct 12 15:09 xab
    -rw-r--r-- 1 root root  932813209 Oct 12 15:10 xac
    -rw-r--r-- 1 root root  932813209 Oct 12 15:10 xad
    -rw-r--r-- 1 root root  932813212 Oct 12 15:10 xae
    
    #合并成一个文件
    [root@c7-node1 test]# cat xa* >CentOS-7-x86_64-DVD-1908.iso
    
    [root@c7-node1 test]# ll CentOS-7-x86_64-DVD-1908.iso 
    -rw-r--r-- 1 root root 4664066048 Oct 12 15:43 CentOS-7-x86_64-DVD-1908.iso
    
    #检查Md5值
    [root@c7-node1 test]# md5sum CentOS-7-x86_64-DVD-1908.iso 
    dc5932260da8f26bcdce0c7ebf0f59ca  CentOS-7-x86_64-DVD-1908.iso
    

    可以看到合并后的文件Md5值 相同,说明文件是一致的。

提醒:本文最后更新于 1442 天前,文中所描述的信息可能已发生改变,请谨慎使用。