Chinaunix首页 | 论坛 | 博客
  • 博客访问: 636057
  • 博文数量: 205
  • 博客积分: 7891
  • 博客等级: 少将
  • 技术积分: 2168
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-29 13:16
文章分类

全部博文(205)

文章存档

2015年(4)

2014年(5)

2013年(1)

2012年(4)

2011年(51)

2010年(86)

2009年(45)

2008年(9)

分类: LINUX

2011-02-15 14:47:12

linux dd 命令 语法,功能介绍
用途:转换并复制文件
语法
dd [ bs=BlockSize ][ cbs=BlockSize ] [ conv= [ ascii | block | ebcdic | ibm | unblock ] [ lcase | ucase ] [ iblock ] [ noerror ] [ swab ] [ sync ] [ oblock ] [ notrunc ] ] [ count=InputBlocks ] [ files=InputFiles ] [ fskip=SkipEOFs ] [ ibs=InputBlockSize ] [ if=InFile ] [ obs=OutputBlockSize ][ of=OutFile ] [ seek=RecordNumber ] [ skip=SkipInputBlocks ][ span=yes|no ]

dd [ Option=Value ]

描述
dd 命令读取 InFile 参数或者标准输入,进行指定的转换,然后将转换后的结果复制到OutFile 参数或者标准输出中。可以指定输入输出块的大小以利用原始的物理 I/O。
注:
Block 项指的是 dd 命令在一个操作中读取或者写的数据的数量,并且无需和磁盘块的大小相等。
指定了大小的地方需要一定字节数。由 w、b 或者 k 结尾的数字表示分别与 2、512 或者 1024 相乘;由 x 或者 * (星号)隔开的两个数字说明是乘积。count 参数表示待复制的块的数量,而不是字节的数量。
由 conv=ascii 和 conv=ebcdic 标志关联的字符集映射是求补运算。这些标志在 ASCII 字符和大多数工作站和键控穿孔机上找到的 EBCDIC 字符子集间进行映射。
如 果指定了任何 block、unblock、ascii、ebcdic 或 ibm 转换,则使用 cbs 参数值。如果指定了 unblock 或者 ascii 参数,那么 dd 命令将会执行一个固定长度到可变长度的转换。否则它将执行从可变长度到固定长度的转换。cbs 参数决定了该固定长度。
注意: 如果 cbs 参数指定的值小于最小输入块的值,则转换块将会被截短。

转换完成后,dd 命令将报告完整和部分输入输出块的数量。
标志
bs=BlockSize

指定输入输出块的大小,取代 ibs 和 obs 标志。由 bs 标志指定的块大小值必须始终是使用的媒介物理块大小的整数倍。
cbs=BlockSize 指定由可变长度到固定长度和从固定长度到可变长度(如 conv=block)的转换块大小。
count=InputBlocks 仅复制由 InputBlocks 变量指定的输入块的数量。
conv= Conversion,.... 指定一个或者多个转换选项。多个转换之间用逗号隔开。

以下清单描述了可能的选项:

ascii 由 EBCDIC 转换为 ASCII。该选项与 ebcdic、ibm、block 和 unblock 选项冲突。
block 将变长记录转换到固定长度。长度由转换块大小(cbs)决定。该选项与 ascii、ebcdic、ibm 和 unblock 选项冲突。
ebcdic 由 ASCII 转换到标准 EBCDIC。该选项与 ascii、ibm、block 和 unblock 选项冲突。
ibm 将 ASCII 转换为 IBM 版本的 EBCDIC。该选项与 ascii、ebcdic、block 和 unblock 选项冲突。
iblock、 oblock 在直接访问设备发生读写错误时使数据损失最小。如果您指定了 iblock 变量而在块读取期间发生了错误(其中块大小为 512 或者由 ibs=InputBlockSize 变量指定大小),dd 命令试图以较小的大小单元重新读取数据块。如果 dd 命令可以确定输入设备的扇区大小,它将每次读取损坏块的一个扇区。否则它将每次读取 512 字节。输入块大小(ibs)必须是重试大小的整数倍。该选项包含与读入单个扇区错误有关的数据丢失。oblock 转换在输出时进行类似工作。
lcase
 使得所有的字母字符转换为小写。
noerror
 发生错误也不停止进程。
notrunc
 不截取输出文件。相反,将保留不显式写入输出的块。
ucase
 使得所有的字母字符转换为大写。
swab
 交换每对字节。
sync
 将每个输入块填充到由 ibs 值指定的长度。
unblock
 将固定长度的块转化为可变长度。长度由转换块大小(cbs)决定。该选项与 ascii、ebcdic、ibm 和 block 选项冲突。

files=InputFiles 在结束之前复制由 InputFiles 变量值指定的文件数(只对磁带或类似设备输入的有用)。
fskip=SkipEOFs 在开始复制之前跳过由 SkipEOFs 变量指定数量的文件结束符;该 SkipEOFs 变量在多文件磁带中进行定位时很有用。
ibs=InputBlockSize 指定输入块的大小;缺省值为 512 字节或者一个块。由 ibs 标志指定的块大小值必须始终是使用的媒介物理块大小的整数倍。
if=InFile 指定输入文件的名称;标准输入是缺省值。
obs=OutputBlockSize 指定输出块的大小;缺省值为 512 字节或者一个块。由 obs 标志指定的块大小值必须始终是使用的媒介物理块大小的整数倍。
of=OutFile 指定输出文件的名称;标准输出是缺省值。
seek=RecordNumber 在复制之前从输出文件的开始寻找由 RecordNumber 变量指定的记录。
skip=SkipInputBlocks 在开始复制之前跳过指定的 SkipInputBlocks 值的输入块。
span=yes|no 如果指定为 yes 则允许设备范围而如果指定为 no则按缺省工作。请参阅 Spanning Across Devices 以获得更多信息。


Examples::

Copy one hard disk partition to another hard disk:

dd if=/dev/sda2 of=/dev/sdb2 bs=4096 conv=notrunc,noerror

sda2, sdb2 are partitions. You want to copy sda2 to sdb2. If sdb2 doesn't exist, dd will start at the beginning of the disk, and create it. Be careful with order of if and of. You can write a blank disk to a good disk if you get confused.

Make an iso image of a CD:

dd if=/dev/hdc of=/home/sam/mycd.iso bs=2048 conv=notrunc

CD sectors are 2048 bytes, so this copies sector for sector. The result will be a hard disk image file of the CD. You can use "chmod a+rwx mycd.iso" to make the image writable. You can mount the image with "mkdir /mnt/mycd", this line in fstab: "/home/sam/mycd.iso /mnt/mycd iso9660 rw,user,noauto 0 0", save fstab, "mount -o loop /mnt/mycd". Then the file system will be viewable as files and directories in the directory /mnt/mycd. You can edit the image as you wish, and the new file will be "/home/sam/mycd.iso" dd does not write to CD's. You need to use a burning utility, or the cdrdao command

Copy a floppy disk:

dd if=/dev/fd0 of=/home/sam/floppy.image bs=2x80x18b conv=notrunc

The 18b specifies 18 sectors of 512 bytes, the 2x multiplies the sector size by the number of heads, and the 80x is for the cylinders--a total of 1474560 bytes. This issues a single 1474560-byte read request to /dev/fd0 and a single 1474560 write request to /tmp/floppy.image. This makes a hard drive image of the floppy, with bootable info intact.

Copy a hard drive image of a floppy to a floppy:

dd if=/home/sam/floppy.image of=fd0 bs=2x80x18b conv=notrunc

Copy just the MBR and boot sector of a floppy to hard drive image:

dd if=/dev/fd0 of=/home/sam/MBRboot.image bs=512 count=2

This copies the first 2 sectors of the floppy

Cloning an entire hard disk:

dd if=/dev/sda of=/dev/sdb conv=notrunc,noerror

in this example, sda is the source. sdb is the target. Do not reverse the intended source and target. Surprisingly many people do. notrunc means to not truncate. noerror means to keep going if there is an error. Normally dd stops at any error. if you have a question about a hard drive on whether or not it works, you can try to use it as the source drive for the dd command. You should get an error if it is not working. target drives need to be really messed up to give an error in dd.

Copy MBR only of a hard drive:

dd if=/dev/sda of=/home/sam/MBR.image bs=446 count=1

this will copy the first 446 bytes of the hard drive to a file. If you haven't already guessed, reversing the objects of if and of, in the dd command line reverses the direction of the write.

Wipe a hard drive of all data (you would want to boot from a cd to do this)
is a good boot cd

the helix boot environment contains the DoD version of dd called dcfldd. It works the same way, but is has a progress bar.

dd if=/dev/zero of=/dev/sda conv=notrunc

This is useful for getting rid of viruses, DRM trojans and the like.

It would be useful, at this time to interject a tip:

I have several machines, but on the one I use a lot I have two SATA hard drives. They are exactly the same. Before I do anything dangerous, I boot from the helix CD, run

dcfldd if=/dev/sda of=/dev/sdb bs=4096 conv=notrunc,noerror

and copy my present working sda drive system to the sdb drive. If I wreck the installation on sda, I just boot with the helix cd and

dcfldd if=/dev/sdb of=/dev/sda bs=4096 conv=notrunc,noerror

Please note: bs=4096 works fast for machines with at least 128 MB of ram. dd uses a lot of buffers. At bs=4096, on modern machines, the optimal transfer rate is reached for hard drives.

To make a file of 100 random bytes

dd if=/dev/urandom of=/home/sam/myrandom bs=1 count=100

Here, urandom is the linux random byte device. myrandom is a file. Byte size equals 1 and there are 100 of them. Gpg requires a random seed to generate keys. Generating a file of say 4096 random bytes, which can be passed to Gpg, will allow a truly random seed.

Write random data over a file before deleting it:
first do an ls -l to find filesize. In this case it is 3769

ls -l afile
-rw------- ... 3769 Nov 2 13:41
dd if=/dev/urandom of= \ bs=3769 count=1 conv=notrunc

This will write random characters over the entire file.

Copy a disk partition to a file on a different partition. Do not copy a partition to the same partition.

dd if=/dev/sdb2 of=/home/sam/partition.image bs=4096 conv=notrunc,noerror

This will make a file that is an exact duplicate of the sdb2 partition. You can substitue hdb, sda, hda, or whatever the disk is called.

Restore a disk partition from an image file.

dd if=/home/sam/partition.image of=/dev/sdb2 bs=4096 conv=notrunc,noerror

This way you can get a bazonga hard drive and partition it so you can back up your root partition. If you mess up your root partition, you just boot from the helix cd and restore the image.

To covert a file to all uppercase:

dd if=filename of=filename conv=ucase

Copy ram memory to a file:

dd if=/dev/mem of=/home/sam/mem.bin bs=1024

The device /dev/mem is your system memory. You can actually copy any blobk or character device to a file with dd. Memory capture on a fast system, with bs=1024 takes about 60 seconds. Copying a 120 GB HDD takes about an hour. Copying a CD to hard drive takes about 10 minutes. Copying a floppy to a hard drive takes about 2 minutes. With dd, your floppy drive images will not change at all. If you have a bootable DOS diskette, and you save it to your HDD as an image file, when you restore that image to another floppy it will be bootable. dd is an excellent way to make an image of MS Windows XP install CD's. When you make a copy of such a cd, there is one sector that is of nonstandard length. It is the last sector. dd doesn't pad this sector, making the copy indistinguishable from the original. If you burn the CD using cdrdao, the resulting disk will be an absolutely exact copy of the original.
阅读(2020) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-03-06 08:40:51

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com