Linux常用命令-dd

命令

dd

描述

convert and copy a file
转换和复制文件

用法

1
dd [OPERAND]...

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Options:
bs=BYTES 每次读取或写入的最大字节数
cbs=BYTES 每次转换的字节数
conv=CONV 按逗号分隔符转换文件
count=N 复制N个输入块
ibs=BYTES 每次读取的最大字节数,默认为512
if=FILE 从指定文件中读取
iflag=FLAGS 按逗号分隔符读取文件
obs=BYTES 每次写入的字节数,默认为512
of=FILE 写入到指定文件
oflag=FLAGS 按逗号分隔符写入文件
seek=N 输出时跳过N个obs大小的块
skip=N 输入时跳过N个ibs大小的块
status=LEVEL 指定标准错误信息级别,'none'只显示错误信息;'noxfer'不显示最后的统计信息;'progress'显示统计信息

BYTES:
kB=1000, K=1024, MB=1000*1000, M=1024*1024
GB=1000*1000*1000, G=1024*1024*1024, T, P, E, Z, Y

CONV:
ascii from EBCDIC to ASCII
ebcdic from ASCII to EBCDIC
ibm from ASCII to alternate EBCDIC
block pad newline-terminated records with spaces to cbs-size
unblock replace trailing spaces in cbs-size records with newline
lcase 把大写字母转换为小写
ucase 把小写字母转换为大写
sparse try to seek rather than write the output for NUL input blocks
swab swap every pair of input bytes
sync pad every input block with NULs to ibs-size; when used
with block or unblock, pad with spaces rather than NULs
excl 如果输出文件已存在则退出
nocreat do not create the output file
notrunc 不截断输出文件
noerror 读取错误时仍然继续
fdatasync physically write output file data before finishing
fsync likewise, but also write metadata

FLAG:
append append mode (makes sense only for output; conv=notrunc suggested)
direct use direct I/O for data
directory fail unless a directory
dsync use synchronized I/O for data
sync likewise, but also for metadata
fullblock accumulate full blocks of input (iflag only)
nonblock use non-blocking I/O
noatime do not update access time
nocache discard cached data
noctty do not assign controlling terminal from file
nofollow do not follow symlinks
count_bytes treat 'count=N' as a byte count (iflag only)
skip_bytes treat 'skip=N' as a byte count (iflag only)
seek_bytes treat 'seek=N' as a byte count (oflag only)

注意

dd命令执行前一定要确认输入和输出路径,否则可能导致数据丢失
不同硬盘之间的数据备份和恢复需注意盘符顺序;不建议恢复数据到非空硬盘或分区

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 生成一个1G的空文件,测试系统IO
$ dd if=/dev/zero of=bigfile bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 3.38315 s, 310 MB/s
# 更为准确的IO测试可以增加fdatasync参数
$ dd if=/dev/zero of=file bs=1M count=1000 conv=fdatasync
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 1.61011 s, 651 MB/s
# 仅第一次执行输出的结果可用,后续结果因为缓存的原因并不准确
$ dd if=/dev/zero of=file bs=1M count=1000 conv=fdatasync
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 0.733227 s, 1.4 GB/s
# 测试系统IO性能
$ dd if=/dev/zero of=/dev/null& pid=$!
[1] 15131
$ kill -USR1 $pid; sleep 1; kill $pid
6948825+0 records in
6948824+0 records out
3557797888 bytes (3.6 GB) copied, 8.28411 s, 429 MB/s
[1]+ Terminated dd if=/dev/zero of=/dev/null
# 转换文件中的小写字母为大写字母
$ dd if=a.sh of=ab conv=ucase,excl
# 使用随机数据填充分区
$ dd if=/dev/urandom of=/dev/sdb1

# 备份硬盘sda的主引导记录mbr
$ dd if=/dev/sda of=mbr bs=512 count=1
# 恢复mbr
$ dd if=mbr of=/dev/sda
$ file mbr
mbr: x86 boot sector;
partition 1: ID=0x83, active, starthead 32, startsector 2048, 2097152 sectors;
partition 2: ID=0x8e, starthead 170, startsector 2099200, 39843840 sectors, code offset 0x63
$ fdisk -l /dev/sda
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000ac371
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
$ hexdump mbr
0000000 63eb 1090 d08e 00bc b8b0 0000 d88e c08e
0000010 befb 7c00 00bf b906 0200 a4f3 21ea 0006
...
0000180 6547 6d6f 4800 7261 2064 6944 6b73 5200
0000190 6165 0064 4520 7272 726f 0a0d bb00 0001
00001a0 0eb4 10cd 3cac 7500 c3f4 0000 0000 0000
00001b0 0000 0000 0000 0000 c371 000a 0000 2080
00001c0 0021 aa83 8228 0800 0000 0000 0020 aa00
00001d0 8229 fe8e ffff 0800 0020 f800 025f 0000
00001e0 0000 0000 0000 0000 0000 0000 0000 0000
00001f0 0000 0000 0000 0000 0000 0000 0000 aa55
0000200

# 将sda整盘备份到sdb
$ dd if=/dev/sda of=/dev/sdb
# 备份sda1分区到文件
$ dd if=/dev/sda1 of=boot
# 压缩备份sda1分区到文件
$ dd if=/dev/sda1 | gzip > boot.gz
$ du -sh boot*
1.0G boot
128M boot.gz
# 恢复压缩分区数据
$ gzip -dc boot.gz | dd of=/dev/sdb1

# 增加swap分区
$ dd if=/dev/zero of=/swapfile bs=1M count=1024
$ mkswap /swapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=8b967ebc-a025-47d4-bdc5-d2609231feec
$ swapon /swapfile
swapon: /swapfile: insecure permissions 0644, 0600 suggested.
$ free -m
total used free shared buff/cache available
Mem: 3931 537 153 19 3240 3123
Swap: 3071 0 3071
$ echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
# 取消临时增加的swap分区
$ swapoff /swapfile
$ free -m
total used free shared buff/cache available
Mem: 3931 529 161 19 3240 3132
Swap: 2047 0 2047