Utility Command DD

Source: Internet
Author: User

------------------------------------------------------------------------------------------

http://blog.csdn.net/ftafta/article/details/7680578

# dd If=/dev/zero of=50m.file bs=1m count=50

Generate a 50M file in the current directory


A more generic name for a virtual block device file is the hard disk image file, but not all hard disk image files are virtual block device files, for example, the current ghost Gho format of the image file cannot be a hard disk in the virtual machine.

The Linux DD command, which is used to generate virtual block device files. It can be used either to create an empty image file or to create a mirror image of a physical hard disk.

# dd if=/dev/hda0 of=/home/oldsong/hda0.image Records in4757130+0 Records out
Copy the contents of the/dev/hda0 to the/home/oldsong/hda0.image file.
The parameter if (input file) tells DD which file to read from, and the parameter of (output file) tells DD which file the read data is written to.
Note that for DD, both the input and output are files, and DD does just the file copy work, thanks to the unix/linux the device is also abstracted as a special file.

In general, there are two types of device files, one is a block device and one is a character device. Block devices are characterized by random Access, such as memory, hard disk, and so on. Character devices are characterized by sequential reading and writing (sequential Access), such as a mouse, keyboard, microphone, and so on.

It said earlier how to generate a mirror of a physical hard disk, and if you want to generate an empty image file (the main purpose of this article), you need a special device. /dev/zero is a special character device provided by Linux, which is characterized by the ability to read the file forever, with the result that each read is binary 0. The following command can generate a 100M empty image file:

# dd If=/dev/zero of=50m.file bs=1m count=50
Bs=1m indicates that every time you read and write 1M data, COUNT=50 represents 50 reads and writes, which specifies the size of the resulting file is 50M. The BS parameter can be further subdivided into IBS and OBS two, specifying different buffer sizes for both read and write operations.

This generates a 50M empty image file, the problem is, if you want to generate 1G of virtual block device files, you have to occupy 1G of hard disk space, and this image file is completely empty, is it a little wasted? Fortunately Linux supports sparse (sparse) files. Take a look at the following example

# dd If=/dev/zero of=1g.img bs=1m seek=1000 count=0
# ls-l 1g.img
-rw-r-r-1 root root 1048576000 Mar 15:32 1g.img
# du-m 1g.img
0 1g.img

Here is a new command to seek, which indicates that skipping 1000 blocks does not write (here The block according to BS is defined as 1M), count=0 means to write 0 blocks. Using the LS (view file size) command to see the newly generated file, the size can be seen as 1000M. However, with Du (viewing the file footprint), the actual drive size is only 0M.


Because you want to use the material to generate a fixed-size multimedia message, so you want to use a picture of about 10K plus a complementary text file, two files a set of standard 10K footage, and finally you can generate a 10~100k size of MMS.

After you have found the image, use the "dd" command to generate the corresponding text file.

Use the following format: (read 1024Byte bytes from the sina.html file to generate a text file mm0.txt)

"DD if=/home/sina.html of=/home/mms0.txt bs=1024 count=1"


If the parameter is the input file is Sina's homepage,

The parameter of the is the file to be generated,

The BS parameter is the block size to be read each time, this is set to the size of the file to be generated,

The count parameter is the number of blocks read, which is set to 1.

A file of the specified size can be generated by combining BS and Count, and the content can be random depending on the if input source.


Second, the file encoding conversion

There is a problem with the resulting text file being sent to the phone via MMS:

The phone prompts the text file to be unsupported footage, or the content cannot be displayed directly.

Later found is the problem of file encoding, packaging MMS when the text footage is UTF-8 encoded, but the generated file seems to be GB2312 encoding, and the MMS header specified encoding format is not the same, so the phone is not recognized.

Once the problem is discovered, it is resolved by using the "iconv" command to convert the file encoding.

The format used is as follows:

"Iconv-f gb2312-t utf-8 mms0.txt > Mm0.txt"

It's just a simple application.

The parameter after-f is the input file encoding

The parameter after-T is the output file encoding

Mms0.txt is the input file

Mm0.txt is the output file

Third, appendix

DD Command usage:

DD [Options]
if = input file (or device name).
of = output file (or device name).
IBS = Bytes Reads bytes bytes At a time, that is, the number of bytes read into the buffer.
Skip = Blocks skips the ibs*blocks block that reads into the beginning of the buffer.
Obs = bytes Writes bytes bytes At a time, that is, the number of bytes written to the buffer.
BS = bytes Sets the number of bytes in the read/write buffer (equal to the set IBS and OBS).
CBS = Byte converts bytes bytes at a time.
The count=blocks only copies the input blocks blocks.
CONV = ASCII Converts the EBCDIC code to Ascil code.
Conv = EBCDIC converts ascil code to EBCDIC code.
CONV = IBM converts ascil code to alternate EBCDIC code.
CONV = Block Converts the change bit to a fixed character.
CONV = Ublock Converts the fixed bit into a change bit.
CONV = UCase Converts the letter from lowercase to uppercase.
CONV = LCase converts letters from uppercase to lowercase.
CONV = Notrunc does not truncate the output file.
CONV = swab swaps each pair of input bytes.
CONV = NoError does not stop processing when an error occurs.
Conv = sync puts the size of each input record to the size of the IBS (filled with nul).

Iconv Command usage:

Iconv [Options ...] [File ...]
Input/output format specification:
-F,--from-code= original text encoding
-T,--to-code= output text encoding
Information:
-L,--list enumeration of all known character sets
Output control:
-C ignores invalid characters from output
-O,--output=file output file
-S,--silent off warning
--verbose Printing Progress Information
-?,--Help gives a list of the systems
--usage gives a brief usage information
-V,--version print program version number

Utility Command DD

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.