Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

Linux 檔案分割與合併:dd 搭配 cat 的實務做法

需要把大檔案切成小塊以便分卷攜帶或上傳時,dd 加上 cat 是最不需要安裝任何額外套件的組合。

dd 的常用參數

  • if=filename:輸入檔案
  • of=filename:輸出檔案
  • bs=bytes:單次讀寫的位元組數,預設 512
  • skip=blocks:讀取前先跳過幾個區塊(區塊大小由 bs 決定)
  • count=blocks:只複製前幾個區塊

分割實例

假設 file 大小為 116616 位元組:

[root]# du -b file
116616  file

以 1024 位元組為一塊,前 60 塊放 file1,其餘放 file2:

[root]# dd if=file bs=1024 count=60 skip=0  of=file1
[root]# dd if=file bs=1024 count=60 skip=60 of=file2

合併時 順序至關重要:

[root]# cat file1 file2 > file.bak

用 md5 驗證兩者一致:

[root]# md5sum file
3ff53f7c30421ace632eefff36148a70  file
[root]# md5sum file.bak
3ff53f7c30421ace632eefff36148a70  file.bak

自動化腳本:ddf.sh(分割)

#!/bin/sh
# ddf.sh:分割檔案,輸出檔名以數字編號結尾,例如 file -> file1、file2
Filename=$1
Filesize=0
Path=`pwd`

if [ -z $Filename ]; then
    echo "Error: The file name can not be empty"
    exit
fi

if [ -e $Filename ]; then
    Filesize=`du -b $Filename | awk '{print $1}'`
    if [ $Filesize == 0 ]; then
        echo "Error: The file size is zero!"
        exit
    fi
    echo "The file size is $Filesize Byte"
    echo "Please enter the subfile size(KB):"
else
    echo "Error: $Filename does not exist!"
    exit
fi

read Subfilesize
if [ -z $Subfilesize ]; then
    echo "Error: Input can not be empty"
    exit
fi

echo $Subfilesize | grep '^[0-9]\+$' >> /dev/null
if [ $? -ne 0 ]; then
    echo "Error: The input is not a number!"
    exit
elif [ $Subfilesize -eq 0 ]; then
    echo "Error: The subfile size is zero!"
    exit
fi

SubfileByte=`expr $Subfilesize \* 1024`
Subfilenum=`expr $Filesize / $SubfileByte`
if [ `expr $Filesize % $SubfileByte` -ne 0 ]; then
    Subfilenum=`expr $Subfilenum + 1`
fi

echo "$Filename will be divided into $Subfilenum"
i=1
skipnum=0
while [ $i -le $Subfilenum ]; do
    echo "$Filename$i"
    dd if=$Filename of="$Path/$Filename$i" bs=1024 count=$Subfilesize skip=$skipnum
    i=`expr $i + 1`
    skipnum=`expr $skipnum + $Subfilesize`
done
echo "$Filename has been divided into $Subfilenum"
echo "Done !"

自動化腳本:caf.sh(合併)

#!/bin/sh
# caf.sh:合併檔案。待合併檔案需放在同一資料夾,
# 檔名前半相同、後半為從 1 開始的連續數字,輸出為 file.bak
echo "Please enter file name:"
read Filename
if [ -z $Filename ]; then
    echo "Error: File name can not be empty"
    exit
fi

echo "Please enter the number of subfiles:"
read Subfilenum
if [ -z $Subfilenum ]; then
    echo "Error: The number of subfiles can not be empty"
    exit
fi
echo $Subfilenum | grep '^[0-9]\+$' > /dev/null
if [ $? -ne 0 ]; then
    echo "Error: Input must be a number"
    exit
fi
if [ $Subfilenum -eq 0 ]; then
    echo "Error: The number of subfiles can not be zero"
    exit
fi

i=1
Newfile=$Filename.bak
while [ $i -le $Subfilenum ]; do
    Subfilename=$Filename$i
    if [ -e $Subfilename ]; then
        echo "$Subfilename done!"
        cat $Subfilename >> $Newfile
        i=`expr $i + 1`
    else
        echo "Error: $Subfilename does not exist"
        rm -rf $Newfile
        exit
    fi
done
echo "Subfiles merged into $Newfile"
echo "Success!"

實際操作

[root]# ./ddf.sh file
The file size is 116616 Byte
Please enter the subfile size(KB):
60
file will be divided into 2
file1
記錄了60+0 的讀入
記錄了60+0 的寫出
61440位元組(61 kB)已複製,0.0352612 秒,1.7 MB/秒
file2
記錄了53+1 的讀入
記錄了53+1 的寫出
55176位元組(55 kB)已複製,0.0316272 秒,1.7 MB/秒
Done !

[root]# ./caf.sh
Please enter file name:
file
Please enter the number of subfiles:
2
file1 done!
file2 done!
Subfiles merged into file.bak
Success!

小提醒:若日後要處理更大型或需要跨平台的分割,split 與 cat 的組合會比手動計算區塊數更省事,也能用 split -b 直接指定大小。

Copyright © Linux教程網 All Rights Reserved