dd的做用是轉換和拷貝文件,咱們能夠利用它來分割文件,相關的選項以下:
if=filename:輸入的文件名
of=finename:輸出的文件名
bs=bytes:一次讀寫的字節數,默認是512bytes
skip=blocks:拷貝前,跳過的輸入文件的前blocks塊,塊的大小有bs決定
count=blocks:只拷貝輸入文件的前blocks塊
例如,如今有一個文件file,大小爲116616字節:
[root]# du -b file
116616 file
將其分割爲兩文件file1和file2,那咱們就設置每塊爲1024字節,將file的前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
而後用cat將兩個文件合併爲file.bak,要注意文件的順序:
[root]# cat file1 file2 > file.bak
能夠用md5sum驗證一下file和file.bak:
[root]# md5sum file
3ff53f7c30421ace632eefff36148a70 file
[root]# md5sum file.bak
3ff53f7c30421ace632eefff36148a70 file.bak
能夠證實兩個文件時徹底相同的。
爲了方便分割、合併文件,我寫了兩個腳本:
ddf.sh
#ddf.sh:分割文件,分割後的文件以數字結尾,例如file分割爲兩個文件:file1和file2
#!/bin/sh
#使用腳本是第一參數是要分割的文件名
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 "Plese enter the subfile size(KB):"
else
echo "Error:$Filename does not exist!"
exit
fi
#輸入分割後每一個文件的大小,單位是KB
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 % $Subfilesize` -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
#caf.sh:合併文件,須要合併的文件要放在一個文件夾裏
# 文件名分爲兩個部分,第一部分都相同,第二部分必須是從1開始的連續數字,例如file1,file2,file3
# 合併後的文件名爲file.bak
#!/bin/sh
#輸入文件名的第一部分
echo "Please enter file name:"
read Filename
if [ -z $Filename ];then
echo "Error:The 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 be merged into $Newfile"
echo "Success!"
用這兩個腳本完成對file的分割、合併:
[root]# ./ddf.sh file
The file size is 116616 Byte
Plese 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/秒
file has been divided into 2
Done !
[root]# ls
caf.sh ddf.sh file file1 file2
[root]# ./caf.sh
Please enter file name:
file
Please enter the number of subfiles:
file1 done!
file2 done!
Subfiles be merged into file.bak
Success!
[root]# ls
caf.sh ddf.sh file file1 file2 file.bakide