Split and merge files in Linux -- DD and cat

Source: Internet
Author: User

Dd is used to convert and copy objects. We can use it to split objects. The related options are as follows:

If = filename: Input File Name

Of = finename: output file name

BS = Bytes: the number of bytes read/write at a time. The default value is 512 bytes.

Skip = blocks: the blocks block before the input file is skipped. The size of the block is determined by BS.

Count = blocks: only copy the first blocks block of the input file.

 

For example, there is a file with a size of 116616 Bytes:

[Plain]
View plaincopy
  1. [Root] # Du-B file
  2. 116616 File

Split it into two files: file1 and file2. Set each file to 1024 bytes, put the first 60 parts of the file into file1, and the remaining parts to 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

Then use Cat to merge the two files into file. Bak. Pay attention to the order of the files:

[Plain]
View plaincopy
  1. [Root] # Cat file1 file2> file. Bak

You can use md5sum to verify the file and file. Bak:

[Plain]
View plaincopy
  1. [Root] # md5sum File
  2. 3ff53f7c30421ace632eefff36148a70 File
  3. [Root] # md5sum file. Bak
  4. 3ff53f7c30421ace632eefff36148a70 file. Bak

It can be proved that the two files are identical.

 

To facilitate file splitting and merging, I wrote two scripts:

DDF. sh:

[Python]
View plaincopy
  1. # DDF. sh: Split the file. The split file ends with a number. For example, the file is divided into two files: file1 and file2.
  2. #! /Bin/sh
  3. # Using the script is the first parameter: the file name to be split
  4. Filename = $1
  5. Filesize = 0
  6. Path = 'pwd'
  7. # Verify that the file name is correct and then calculate the file size
  8. If [-Z $ filename]; then
  9. Echo "error: the file name can not be empty"
  10. Exit
  11. Fi
  12. If [-e $ filename]; then
  13. Filesize = 'du-B $ filename | awk '{print $1 }''
  14. If [$ filesize = 0]; then
  15. Echo "error: the file size is zero! "
  16. Exit
  17. Fi
  18. Echo "the file size is $ filesize Byte"
  19. Echo "plese enter the subfile size (Kb ):"
  20. Else
  21. Echo "error: $ filename does not exist! "
  22. Exit
  23. Fi
  24. # Input the size of each split file, in KB
  25. Read subfilesize
  26. If [-Z $ subfilesize]; then
  27. Echo "error: input can not be empty"
  28. Exit
  29. Fi
  30. Echo $ subfilesize | grep '^ [0-9] \ + $'>/dev/null
  31. If [$? -Ne 0]; then
  32. Echo "error: the input is not a number! "
  33. Exit
  34. Elif [$ subfilesize-EQ 0]; then
  35. Echo "error: the subfile size is zero! "
  36. Exit
  37. Fi
  38. # The calculation must be divided into several files.
  39. Subfilebyte = 'expr $ subfilesize \ * 1024'
  40. Subfilenum = 'expr $ filesize/$ subfilebyte'
  41. If ['expr $ filesize % $ subfilesize '-Ne 0]; then
  42. Subfilenum = 'expr $ subfilenum + 1'
  43. Fi
  44. # Splitting files
  45. Echo "$ filename will be divided into $ subfilenum"
  46. I = 1
  47. Skipnum = 0
  48. While [$ I-Le $ subfilenum]
  49. Do
  50. Echo "$ filename $ I"
  51. Dd If = $ filename of = "$ path/$ filename $ I" BS = 1024 COUNT = $ subfilesize skip = $ skipnum
  52. I = 'expr $ I + 1'
  53. Skipnum = 'expr $ skipnum + $ subfilesize'
  54. Done
  55. Echo "$ filename has been divided into $ subfilenum"
  56. Echo "done! "

 

CAF. sh:

[Python]
View plaincopy
  1. # Caf. sh: Merge files. The files to be merged must be placed in a folder.
  2. # The file name is divided into two parts, the first part is the same, the second part must be a continuous number starting from 1, such as file1, file2, file3
  3. # The merged file name is file. Bak.
  4. #! /Bin/sh
  5. # Enter the first part of the file name
  6. Echo "Please enter file name :"
  7. Read filename
  8. If [-Z $ filename]; then
  9. Echo "error: file name can not be empty"
  10. Exit
  11. Fi
  12. # Enter the number of files to be merged
  13. Echo "Please enter the number of subfiles :"
  14. Read subfilenum
  15. If [-Z $ subfilenum]; then
  16. Echo "error: the number of subfiles can not be empty"
  17. Exit
  18. Fi
  19. Echo $ subfilenum | grep '^ [0-9] \ + $'>/dev/null
  20. If [$? -Ne 0]; then
  21. Echo "error: input must be a number"
  22. Exit
  23. Fi
  24. If [$ subfilenum-EQ 0]; then
  25. Echo "error: the number of subfiles can not be zero"
  26. Exit
  27. Fi
  28. # Merge files
  29. I = 1
  30. Newfile = $ filename \. Bak
  31. While [$ I-Le $ subfilenum]
  32. Do
  33. Subfilename = $ filename $ I
  34. If [-e $ subfilename]; then
  35. Echo "$ subfilename done! "
  36. Cat $ subfilename> $ newfile
  37. I = 'expr $ I + 1'
  38. Else
  39. Echo "error: $ subfilename does not exist"
  40. Rm-RF $ newfile
  41. Exit
  42. Fi
  43. Done
  44. Echo "subfiles be merged into $ newfile"
  45. Echo "success! "

 

Use these two scripts to split and merge files:

[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. Read records of 60 + 0
  8. Records 60 + 0 writes
  9. 61440 bytes (61 KB) Copied, 0.0352612 seconds, 1.7 MB/second
  10. File2
  11. Records the reading of 53 + 1
  12. Records write of 53 + 1
  13. 55176 bytes (55 KB) Copied, 0.0316272 seconds, 1.7 MB/second
  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.