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 選項來禁止生成空文件,請看下面的實例

上圖中 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文件,最快的方法是?


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM