首頁·終端機命令

dd

壓縮與傳輸

用法

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

它按區塊原樣複製,裝機映像就是這樣寫進裝置的;`of=` 指到哪裡,那裡就從第 0 塊起被覆蓋,不問一聲也無法還原,所以按下 Enter 前一定要用 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 塊起被覆蓋,不問一聲也無法還原,所以按下 Enter 前一定要用 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