Linux下分割、合并檔案——dd和cat

來源:互聯網
上載者:User

dd的作用是轉換和拷貝檔案,我們可以利用它來分割檔案,相關的選項如下:

if=filename:輸入的檔案名稱

of=finename:輸出的檔案名稱

bs=bytes:一次讀寫的位元組數,預設是512bytes

skip=blocks:拷貝前,跳過的輸入檔案的前blocks塊,塊的大小有bs決定

count=blocks:只拷貝輸入檔案的前blocks塊

 

例如,現在有一個檔案file,大小為116616位元組:

[plain]
view plaincopy
  1. [root]# du -b file  
  2. 116616  file  

將其分割為兩檔案file1和file2,那我們就設定每塊為1024位元組,將file的前60塊放入file1,餘下的放入file2:

[plain]
view plaincopy
  1. [root]# dd if=file bs=1024 count=60 skip=0  of=file1  
  2. [root]# dd if=file bs=1024 count=60 skip=60 of=file2  

然後用cat將兩個檔案合并為file.bak,要注意檔案的順序:

[plain]
view plaincopy
  1. [root]# cat file1 file2 > file.bak  

可以用md5sum驗證一下file和file.bak:

[plain]
view plaincopy
  1. [root]# md5sum file  
  2. 3ff53f7c30421ace632eefff36148a70  file  
  3. [root]# md5sum file.bak  
  4. 3ff53f7c30421ace632eefff36148a70  file.bak  

可以證明兩個檔案時完全相同的。

 

為了方便分割、合并檔案,我寫了兩個指令碼:

ddf.sh:

[python]
view plaincopy
  1. #ddf.sh:分割檔案,分割後的檔案以數字結尾,例如file分割為兩個檔案:file1和file2  
  2. #!/bin/sh  
  3.   
  4. #使用指令碼是第一參數是要分割的檔案名稱  
  5. Filename=$1     
  6. Filesize=0  
  7.   
  8. Path=`pwd`  
  9.   
  10. #驗證檔案名稱是否正確,然後計算檔案的大小  
  11. if [ -z $Filename ];then  
  12.     echo "Error:The file name can not be empty"  
  13.     exit  
  14. fi  
  15. if [ -e $Filename ];then  
  16.     Filesize=`du -b $Filename | awk '{print $1}'`  
  17.     if [ $Filesize == 0 ];then  
  18.         echo "Error:The File size is zero!"  
  19.         exit  
  20.     fi  
  21.     echo "The file size is $Filesize Byte"  
  22.     echo "Plese enter the subfile size(KB):"  
  23. else  
  24.     echo "Error:$Filename does not exist!"  
  25.     exit   
  26. fi  
  27.   
  28. #輸入分割後每個檔案的大小,單位是KB  
  29. read Subfilesize  
  30. if [ -z $Subfilesize ];then  
  31.     echo "Error:Input can not be empty"  
  32.     exit  
  33. fi  
  34.   
  35. echo $Subfilesize | grep '^[0-9]\+$' >> /dev/null  
  36. if [ $? -ne 0 ];then  
  37.     echo "Error:The Input is not a number!"  
  38.     exit  
  39. elif [ $Subfilesize -eq 0 ];then  
  40.     echo "Error:The Subfile size is zero!"  
  41.     exit  
  42. fi  
  43.   
  44. #計算需要分割為幾個檔案  
  45. SubfileByte=`expr $Subfilesize \* 1024`  
  46. Subfilenum=`expr $Filesize / $SubfileByte`  
  47. if [ `expr $Filesize % $Subfilesize` -ne 0 ];then  
  48.     Subfilenum=`expr $Subfilenum + 1`  
  49. fi  
  50.   
  51. #將檔案分割  
  52. echo "$Filename will be divided into $Subfilenum"   
  53. i=1  
  54. skipnum=0  
  55. while [ $i -le $Subfilenum ]  
  56. do  
  57.     echo "$Filename$i"  
  58.     dd if=$Filename of="$Path/$Filename$i" bs=1024 count=$Subfilesize skip=$skipnum  
  59.     i=`expr $i + 1`  
  60.     skipnum=`expr $skipnum + $Subfilesize`  
  61. done  
  62. echo "$Filename has been divided into $Subfilenum"  
  63. echo "Done !"  

 

caf.sh:

[python]
view plaincopy
  1. #caf.sh:合并檔案,需要合并的檔案要放在一個檔案夾裡  
  2. #       檔案名稱分為兩個部分,第一部分都相同,第二部分必須是從1開始的連續數字,例如file1,file2,file3  
  3. #       合并後的檔案名稱為file.bak  
  4. #!/bin/sh  
  5.   
  6. #輸入檔案名稱的第一部分  
  7. echo "Please enter file name:"  
  8. read Filename  
  9. if [ -z $Filename ];then  
  10.     echo "Error:File name can not be empty"  
  11.     exit  
  12. fi  
  13.   
  14. #輸入待合并檔案的個數  
  15. echo "Please enter the number of subfiles:"   
  16. read Subfilenum  
  17. if [ -z $Subfilenum ];then  
  18.     echo "Error:The number of subfiles can not be empty"  
  19.     exit  
  20. fi  
  21. echo $Subfilenum | grep '^[0-9]\+$' > /dev/null  
  22. if [ $? -ne 0 ];then  
  23.         echo "Error:Input must be a number"  
  24. exit  
  25. fi  
  26. if [ $Subfilenum -eq 0 ];then  
  27.         echo "Error:The number of subfiles can not be zero"  
  28. exit  
  29. fi  
  30.   
  31.   
  32. #合并檔案  
  33. i=1  
  34. Newfile=$Filename\.bak  
  35. while [ $i -le $Subfilenum ]  
  36. do  
  37.     Subfilename=$Filename$i  
  38.         if [ -e $Subfilename ];then  
  39.             echo "$Subfilename done!"  
  40.             cat $Subfilename >> $Newfile  
  41.             i=`expr $i + 1`  
  42.         else  
  43.             echo "Error:$Subfilename does not exist"  
  44.             rm -rf $Newfile  
  45.             exit  
  46.         fi  
  47. done  
  48.   
  49.   
  50. echo "Subfiles be merged into $Newfile"  
  51. echo "Success!"  

 

用這兩個指令碼完成對file的分割、合并:

[python]
view plaincopy
  1. [root]# ./ddf.sh file   
  2. The file size is 116616 Byte  
  3. Plese enter the subfile size(KB):  
  4. 60  
  5. file will be divided into 2  
  6. file1  
  7. 記錄了60+0 的讀入  
  8. 記錄了60+0 的寫出  
  9. 61440位元組(61 kB)已複製,0.0352612 秒,1.7 MB/秒  
  10. file2  
  11. 記錄了53+1 的讀入  
  12. 記錄了53+1 的寫出  
  13. 55176位元組(55 kB)已複製,0.0316272 秒,1.7 MB/秒  
  14. file has been divided into 2  
  15. Done !  
  16. [root]# ls  
  17. caf.sh  ddf.sh  file  file1  file2  
  18. [root]# ./caf.sh   
  19. Please enter file name:  
  20. file  
  21. Please enter the number of subfiles:  
  22. 2  
  23. file1 done!  
  24. file2 done!  
  25. Subfiles be merged into file.bak  
  26. Success!  
  27. [root]# ls  
  28. caf.sh  ddf.sh  file  file1  file2  file.bak 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.