系统与基础设施eklitzke.org
dd 命令的膜拜
The cult of dd (2017)
2017 — 2022,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这篇文章讨论 Unix 工具 dd 为何形成一种“邪教式”使用习惯:作者认为它的参数晦涩,许多日常复制或生成文件的场景用 cat、head 等更直观,并吐槽 bs 等参数常被赋予并不存在的魔力。评论区则补充了 dd 在低成本 seek、设备写入、坏盘恢复等场景中仍难以替代的理由。
为什么经久不衰:它把 Unix 工具哲学的争论落到一条具体命令上:dd 晦涩却掌握底层 I/O 控制,今天读仍能帮人判断何时该用 dd、何时只是习惯。
HN 评论精选
dd 的块大小会影响性能,且 seek 创建稀疏文件等能力是 cat/head 没有的。
“但谁在乎?为什么不让命令自动算出正确的缓冲区大小?”——因为它在过去并不自动这么做。至少我用的 Linux 版本默认是 512 字节块之类的小块,而且每块单独提交,遇到控制器不擅长合并写入的 U 盘时性能很差;我也不会惊讶这会在闪存层造成严重写放大。也许现在它够智能能选更好的块大小,但这个习惯就是这么来的。另外,用 seek 选项创建稀疏文件(文件里的零不实际写盘、不占空间,但读出来都是零)也是 cat 或 head 替代不了的。我喜欢 dd 的一点是它几乎能用一个工具完成所有磁盘操作,值得完整学习。
GekkePrutser查看英文原文
> But who cares? Why not just let the command figure out the right buffer size automatically? Well, because it doesn't. At least on the Linux version I used in the past, it defaulted to 512 byte blocks or something similarly small and it commits every block individually, leading to really slow performance with USB sticks with controllers not smart enough to bundle writes together. I wouldn't be surprised if that incurs some heavy write amplification at flash level too. Perhaps it's smart enough to figure a better block size now but this is where that habit comes from. Another thing, creating sparse files with the seek option (simply put files containing zeros that are not actually written to the disk nor taking space, but do turn up all these zeroes when you read the file). Also something not duplicated with cat or head. What I like about dd is that it can do pretty much all disk operations in one simple tool. Definitely worth learning all of it IMO.
dd 能低成本 seek 文件描述符,是 cat/head 难以替代的独特能力。
对文件描述符进行 seek 是基础且有用的操作,而 dd 是唯一能在不实际付出读写代价的情况下 seek fd 的 POSIX 工具。假设你有 2 小时 UHD RGBA32 视频,需要从中间取 5 分钟素材:dd if=in.raw bs=$(( 3840 * 2160 * 4 * 24 )) skip=3600 count=300 | ffmpeg ... 这比无意义地 cat 前 3TB 快得多。变体:文件有 1M 头需要跳过:{ dd bs=1M skip=1 count=0; dd bs=796262400 skip=3600 count=300; } < in.raw | ffmpeg ... 第一个 dd 只把 stdin 向前 seek 1M,第二个就能按整秒块处理。有用!当然,dd 也有“不该用的用法”,就像 cat 的“无用之用”一样。
sigil查看英文原文
Seeking a file descriptor is a basic, useful operation, and dd is the only POSIX tool that can seek an fd without actually paying the read / write costs. Suppose you have 2 hours of UHD RGBA32 video, and need 5 minutes of footage from the halfway mark: dd if=in.raw bs=$(( 3840 * 2160 * 4 * 24 )) skip=3600 count=300 | ffmpeg ... This will be a lot faster than pointlessly catting the first 3 terabytes! Here's a variation. Suppose the video file has a 1M header you need to skip: { dd bs=1M skip=1 count=0; dd bs=796262400 skip=3600 count=300; } < in.raw | ffmpeg ... The first dd invocation does nothing more than seek stdin ahead 1M, so the second can operate on full 1 second chunks of video. Useful! Now, are there Uncalled For Uses of dd, just like there are Useless Uses of cat? You bet.
dd 能对输出设备 seek,复制引导程序而不破坏分区表;这种联想很自然。
作者没有提到 dd 真正最佳的一些用例。比如需要从现有镜像复制 bootloader 又不破坏分区表时:# 分区表在 sector 0,Linux 分区从 sector 62500 开始 dd if=random-linux-image.img of=/dev/mmcblk0 bs=512 count=62499 seek=1 skip=1 你怎样让 cat 在写 stdout 前 seek 512 字节?做不到。即使使用临时文件,我认为这也比 head/tail 管道加上 $(()) 做扇区到字节换算更好。用过几次后,你会把 dd 和“磁盘操作”联系起来,于是简单场景也用它,还教别人;我觉得这并不奇怪,也没什么不好。
colonwqbang查看英文原文
The author neglects to mention any of the use cases where "dd" really is the best alternative. Such as when you need to copy a bootloader from an existing image without damaging your partition table: # My partition table is in sector 0 and my Linux partition starts at sector 62500 dd if=random-linux-image.img of=/dev/mmcblk0 bs=512 count=62499 seek=1 skip=1 How do you make "cat" seek 512 bytes into its stdout before writing? You can't. And even if you used a temporary file, I think the above is better than a pipeline of "head" and "tail" with uses of $(()) to convert from sector to byte units. Once you have learned to use the tool in such situations a few times, you mentally start to associate it with "disk operations". So you start to use it also in simpler situations where simpler tools would be sufficient. You start teaching its use to others. I don't think it's that strange, or bad in any way.
dd 的 of= 让 sudo 直接打开设备目标,避免了重定向权限问题。
使用 dd 操作设备有一个不错(?)的理由:它在同一条命令里指定目标。写设备通常需要 root,所以很容易写 sudo dd .... of=/dev/...;但 cat 没有简单等价:sudo cat ... > target 仍会以当前用户身份打开 target。当然可以用 tee 和重定向绕,但那比原命令更复杂。
viraptor查看英文原文
There's one good (?) reason to use dd with devices: it specifies target in the same command. For devices, writing to them usually requires root privileges, so it's easy to: sudo dd .... of=/dev/... But there's no trivial cat equivalent: sudo cat ... > target Will open target as your current user anyway. You can play around with tee and redirection of course. But that's getting more complicated than the original.
历次讨论
同类还值得读
TTY 揭秘(2008)
The TTY demystified (2008)
经典文章深入剖析了 Unix/Linux 中 TTY 子系统的工作原理,包括线路规程、终端驱动、信号与控制字符等机制。
系统与基础设施上首页 11 次 · 2009–2025linusakesson.net编辑精选为什么 GNU grep 这么快
Why GNU grep is Fast
FreeBSD 邮件列表上一篇广为流传的帖子,解释 GNU grep 为什么快:核心在于 Boyer-Moore 字符串搜索算法配合精心优化的内部循环和 I/O,让程序在搜索时“…
系统与基础设施上首页 9 次 · 2010–2022lists.freebsd.orgXplain:为普通人讲解 X11
Xplain – Explaining X11 for the rest of us
《Xplain》是作者(HN 用户 Jasper_)写的一系列 X11 长文,用他在浏览器中重建的 X11 绘图模型做交互演示,讲解 X11 的绘图模型与窗口系统设计。
系统与基础设施上首页 6 次 · 2013–2020magcius.github.io编辑精选旋风教程:为 Linux 创建极小 ELF 可执行文件(1999)
A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux (1999)
breadbox 写于 1999 年的经典教程,主题是如何为 Linux 手工构造并裁剪出体积极小的 ELF 可执行文件。
系统与基础设施上首页 6 次 · 2007–2025muppetlabs.comPostgreSQL 内部原理
The internals of PostgreSQL
《The Internals of PostgreSQL》是一本免费在线书籍,系统讲解 PostgreSQL 的内部实现,包括查询处理、并发控制、WAL、缓冲区管理等核心机制。
系统与基础设施上首页 5 次 · 2017–2025interdb.jp编辑精选