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
- [Root] # Du-B file
- 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
- [Root] # dd If = file BS = 1024 COUNT = 60 skip = 0 of = file1
- [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
- [Root] # Cat file1 file2> file. Bak
You can use md5sum to verify the file and file. Bak:
[Plain]
View plaincopy
- [Root] # md5sum File
- 3ff53f7c30421ace632eefff36148a70 File
- [Root] # md5sum file. Bak
- 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
- # DDF. sh: Split the file. The split file ends with a number. For example, the file is divided into two files: file1 and file2.
- #! /Bin/sh
- # Using the script is the first parameter: the file name to be split
- Filename = $1
- Filesize = 0
- Path = 'pwd'
- # Verify that the file name is correct and then calculate the file size
- 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
- # Input the size of each split file, in 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
- # The calculation must be divided into several files.
- Subfilebyte = 'expr $ subfilesize \ * 1024'
- Subfilenum = 'expr $ filesize/$ subfilebyte'
- If ['expr $ filesize % $ subfilesize '-Ne 0]; then
- Subfilenum = 'expr $ subfilenum + 1'
- Fi
- # Splitting files
- 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:
[Python]
View plaincopy
- # Caf. sh: Merge files. The files to be merged must be placed in a folder.
- # 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
- # The merged file name is file. Bak.
- #! /Bin/sh
- # Enter the first part of the file name
- Echo "Please enter file name :"
- Read filename
- If [-Z $ filename]; then
- Echo "error: file name can not be empty"
- Exit
- Fi
- # Enter the number of files to be merged
- 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
- # Merge files
- 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! "
Use these two scripts to split and merge files:
[Python]
View plaincopy
- [Root] #./DDF. Sh File
- The file size is 116616 byte
- Plese enter the subfile size (Kb ):
- 60
- File will be divided into 2
- File1
- Read records of 60 + 0
- Records 60 + 0 writes
- 61440 bytes (61 KB) Copied, 0.0352612 seconds, 1.7 MB/second
- File2
- Records the reading of 53 + 1
- Records write of 53 + 1
- 55176 bytes (55 KB) Copied, 0.0316272 seconds, 1.7 MB/second
- 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:
- 2
- File1 done!
- File2 done!
- Subfiles be merged into file. Bak
- Success!
- [Root] # ls
- CAF. Sh DDF. Sh file file1 file2 file. Bak