9320

47 分钟

#Bash 的 dd 命令

dd [OPERAND]...

功能

复制文件并进行格式转换。

类型

可执行文件(/usr/bin/dd),属于 coreutils

参数

  • OPERAND 操作数

#操作数

操作数说明
bs=BYTES每次读写最多 BYTES 字节(默认 512)。会覆盖 ibsobs 的设置。
cbs=BYTES每次转换 BYTES 字节。
conv=CONVS根据逗号分隔的符号列表对文件进行转换
count=N只复制 N 个输入块。
ibs=BYTES每次读取最多 BYTES 字节(默认 512)。
if=FILE从 FILE 读取,而不是从标准输入读取。
iflag=FLAGS按逗号分隔的标志列表控制读取行为。
obs=BYTES每次写入 BYTES 字节(默认 512)。
of=FILE写入到 FILE,而不是标准输出。
oflag=FLAGS按逗号分隔的标志列表控制写入行为。
seek=N(或 oseek=N跳过 N 个 obs 大小的输出块。
skip=N(或 iseek=N跳过 N 个 ibs 大小的输入块。
status=LEVEL输出到 stderr 的信息级别:none 只显示错误;noxfer 隐藏最终传输统计;progress 显示周期性传输统计。

NBYTES 可用使用以下单位:c=1, w=2, b=512, kB=1000, K=1024, MB=1000*1000, M=1024*1024, xM=M 以及 G, T, P, E, Z, Y, R, Q 等二进制单位前缀.

#转换参数

转换参数(CONV说明
asciiEBCDIC 转换为 ASCII
ebcdicASCII 转换为 EBCDIC
ibmASCII 转换为另一种 IBM EBCDIC 变体
block将以换行符结尾的记录填充空格,使其达到 cbs 指定大小。
unblockcbs 大小记录中的尾随空格替换为换行符。
lcase将大写字母转换为小写字母。
ucase将小写字母转换为大写字母。
sparse遇到全 NUL 的输出块时尝试使用 seek,而不是实际写入。
swab每两个字节交换顺序(字节对调)。
sync将每个输入块填充 NUL,使其达到 ibs 大小;与 blockunblock 一起使用时填充空格而非 NUL。
excl如果输出文件已存在则失败。
nocreat不创建输出文件。
notrunc不截断输出文件。
noerror读取出错后继续。
fdatasync结束前将输出文件的数据物理写入磁盘。
fsync行为类似,但同时写入文件元数据。

#Flag 参数

Flag说明
append追加模式(仅对输出有意义,建议搭配 conv=notrunc)。
direct使用直接 I/O(跳过缓存)。
directory失败,除非目标是目录。
dsync数据使用同步 I/O。
sync同上,但也同步元数据。
fullblock累积完整的输入块(仅适用于 iflag)。
nonblock使用非阻塞 I/O。
noatime不更新访问时间。
nocache请求丢弃缓存(同时可参考 oflag=sync)。
noctty不从文件获取控制终端。
nofollow不跟随符号链接。

#示例

创建 100MB 的文件

$ dd if=/dev/zero of=file.bin bs=1M count=100

创建 10GB 的稀疏文件(不实际占用硬盘空间)

$ dd if=/dev/zero of=file.bin bs=1M count=0 seek=10G

将硬盘保存到镜像文件

$ dd if=/dev/sda of=/path/disk.img bs=4M

测试硬盘读取速度

$ dd if=/dev/sda of=/dev/null bs=4M status=progress

清空硬盘(危险)

$ dd if=/dev/zero of=/dev/sdb bs=1M

制作可启动 U 盘(危险)

$ dd if=ubuntu.iso of=/dev/sdb bs=4M status=progress oflag=sync

#推荐阅读

#手册

DD(1) User Commands DD(1) NAME dd - convert and copy a file SYNOPSIS dd [OPERAND]... dd OPTION DESCRIPTION Copy a file, converting and formatting according to the operands. bs=BYTES read and write up to BYTES bytes at a time (default: 512); over‐ rides ibs and obs cbs=BYTES convert BYTES bytes at a time conv=CONVS convert the file as per the comma separated symbol list count=N copy only N input blocks ibs=BYTES read up to BYTES bytes at a time (default: 512) if=FILE read from FILE instead of stdin iflag=FLAGS read as per the comma separated symbol list obs=BYTES write BYTES bytes at a time (default: 512) of=FILE write to FILE instead of stdout oflag=FLAGS write as per the comma separated symbol list seek=N (or oseek=N) skip N obs-sized output blocks skip=N (or iseek=N) skip N ibs-sized input blocks status=LEVEL The LEVEL of information to print to stderr; 'none' suppresses everything but error messages, 'noxfer' suppresses the final transfer statistics, 'progress' shows periodic transfer statis‐ tics N and BYTES may be followed by the following multiplicative suffixes: c=1, w=2, b=512, kB=1000, K=1024, MB=1000*1000, M=1024*1024, xM=M, GB=1000*1000*1000, G=1024*1024*1024, and so on for T, P, E, Z, Y, R, Q. Binary prefixes can be used, too: KiB=K, MiB=M, and so on. If N ends in 'B', it counts bytes not blocks. Each CONV symbol may be: 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 change upper case to lower case ucase change lower case to upper case sparse try to seek rather than write all-NUL output 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 fail if the output file already exists nocreat do not create the output file notrunc do not truncate the output file noerror continue after read errors fdatasync physically write output file data before finishing fsync likewise, but also write metadata Each FLAG symbol may be: append append mode (makes sense only for output; conv=notrunc sug‐ gested) 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 Request to drop cache. See also oflag=sync noctty do not assign controlling terminal from file nofollow do not follow symlinks Sending a USR1 signal to a running 'dd' process makes it print I/O sta‐ tistics to standard error and then resume copying. Options are: --help display this help and exit --version output version information and exit AUTHOR Written by Paul Rubin, David MacKenzie, and Stuart Kemp. REPORTING BUGS GNU coreutils online help: <https://www.gnu.org/software/coreutils/> Report any translation bugs to <https://translationproject.org/team/> COPYRIGHT Copyright © 2023 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. SEE ALSO Full documentation <https://www.gnu.org/software/coreutils/dd> or available locally via: info '(coreutils) dd invocation' GNU coreutils 9.4 April 2024 DD(1)

创建于 2025/11/14

更新于 2025/11/16