Chinaunix首页 | 论坛 | 博客
  • 博客访问: 614719
  • 博文数量: 120
  • 博客积分: 2284
  • 博客等级: 大尉
  • 技术积分: 1330
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-25 10:49
个人简介

http://guliqun1983.blog.163.com/blog/static/501116852011730535314/

文章分类
文章存档

2013年(23)

2012年(23)

2011年(74)

分类: 嵌入式

2011-02-10 14:50:06

dd 命令详解
dd Linux/UNIX 下的一个非常有用的命令,作用是用指定大小的块拷贝一个文件,
并在拷贝的同时进行指定的转换。

dd 的主要选项:

指定数字的地方若以下列字符结尾乘以相应的数字:

b=512, c=1, k=1024, w=2, xm=number m

if=file

输入文件名,缺省为标准输入。

of=file

输出文件名,缺省为标准输出。

ibs=bytes

一次读入 bytes 个字节(即一个块大小为 bytes 个字节)

obs=bytes

一次写 bytes 个字节(即一个块大小为 bytes 个字节)

bs=bytes

同时设置读写块的大小为 bytes ,可代替 ibs obs

cbs=bytes

一次转换 bytes 个字节,即转换缓冲区大小。

skip=blocks

从输入文件开头跳过 blocks 个块后再开始复制。

seek=blocks

从输出文件开头跳过 blocks 个块后再开始复制。(通常只有当输出文件是磁盘或磁

带时才有效)

count=blocks

仅拷贝 blocks 个块,块大小等于 ibs 指定的字节数。

conv=conversion[,conversion...]

用指定的参数转换文件。

转换参数:

ascii 转换 EBCDIC ASCII

ebcdic 转换 ASCII EBCDIC

ibm 转换 ASCII alternate EBCDIC.

block 把每一行转换为长度为 cbs 的记录,不足部分用空格填充。

Unblock

使每一行的长度都为 cbs ,不足部分用空格填充。

lcase 把大写字符转换为小写字符。

ucase 把小写字符转换为大写字符。

swab 交换输入的每对字节。 Unlike theUnix dd, this works when an odd number

of bytes are read. If the input file contains an odd number of bytes, the

last byte is simply copied (since there is nothing to swap it with).

Noerror

出错时不停止。

Notrunc

不截短输出文件。

sync 把每个输入块填充到ibs个字节,不足部分用空(NUL)字符补齐。

由于 dd 命令允许二进制方式读写,所以特别适合在原始物理设备上进行输入/输出。

例如可以用下面的命令为软盘建立镜像文件:

dd if=/dev/fd0 of=disk.img bs=1440k

有趣的是,这个镜像文件能被 HD-Copy Winimage 等工具软件读出。再如把第一个硬盘

的前 512 个字节存为一个文件:

dd if=/dev/hda of=disk.mbr bs=512 count=1

 

The basic command is structured as follows:
dd if= of= bs=(some power of 2, not less than
512 bytes(ie, 512, 1024, 2048, 4096, 8192, 16384) conv=.
Source is the data being read. Target is where the data gets written. If you
mess up, and accidently reverse the source and target, you can wipe out a lot
of data.

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.
阅读(1530) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~