国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > dd 以获得更好的体验。

# Bash 的 dd 命令

dd [OPERAND]...

!subtitle:功能

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

!subtitle:类型

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

!subtitle:参数

  • 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 说明
ascii EBCDIC 转换为 ASCII
ebcdic ASCII 转换为 EBCDIC
ibm ASCII 转换为另一种 IBM EBCDIC 变体
block 将以换行符结尾的记录填充空格,使其达到 cbs 指定大小。
unblock cbs 大小记录中的尾随空格替换为换行符。
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 不跟随符号链接。

# 示例

$ dd if=/dev/zero of=file.bin bs=1M count=100
$ 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
$ 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-27 09:38:14 创建于: 2025-11-27 09:38:14