首页·终端命令

dd

压缩与传输

用法

dd if=<source> of=<target> bs=4M status=progress

它按块原样复制,装机镜像就是这样写进设备的;`of=` 指到哪里,那里就从第 0 块起被覆盖,不问一声也没法撤销,所以按下回车前一定要用 lsblk 再确认一次设备名,用 `bs=4M` 提速,用 `status=progress`(macOS 按 Ctrl+T)看进度。

常用选项

选项含义
if=<file>Input file or device. Leaving it out reads stdin.
of=<file>Output. Whatever this names is overwritten from block zero, with no confirmation.
bs=4MBlock size. Too small and the copy crawls.
count=<n>Copy only n blocks.
status=progressShow progress (GNU). On macOS press Ctrl+T instead.
conv=fsyncFlush to the device before exiting, so the numbers mean something.
conv=notruncWrite in place without truncating the output file.

示例

lsblk

Always confirm the device name immediately before the next command.

sudo dd if=ubuntu.iso of=/dev/sdb bs=4M status=progress conv=fsync

Write an installer image to a whole USB device (Linux).

diskutil unmountDisk /dev/disk4 && sudo dd if=ubuntu.iso of=/dev/rdisk4 bs=4m

On macOS the disk must be unmounted first, or dd fails with Resource busy; rdiskN is the fast raw path.

打包和压缩是两件事:tar 打包,gzip 压小,.tar.gz 是两样一起。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. dd 是做什么的?

它按块原样复制,装机镜像就是这样写进设备的;`of=` 指到哪里,那里就从第 0 块起被覆盖,不问一声也没法撤销,所以按下回车前一定要用 lsblk 再确认一次设备名,用 `bs=4M` 提速,用 `status=progress`(macOS 按 Ctrl+T)看进度。

Q. 怎么写?

dd if=<source> of=<target> bs=4M status=progress —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 7 个,完整列表在 man dd。这条命令属于压缩与传输。

相关命令

man 手册: man dd