TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The Cult of DD

269 点作者 eklitzke大约 8 年前

38 条评论

cat199大约 8 年前
&quot;This is a strange program of obscure provenance that somehow, still manages to survive in the 21st century.&quot;<p>-&gt; links to wikipedia page with direct discription of lineage back to 5th ed research unix<p>&quot;That weird bs=4M argument in the dd version isn’t actually doing anything special—all it’s doing is instructing the dd command to use a 4 MB buffer size while copying. But who cares? Why not just let the command figure out the right buffer size automatically?&quot;<p>Um -<p>a) it is &#x27;doing the special thing&#x27; of changing the <i>block</i> size (not buffer size)<p>b) Because the command probably doesn&#x27;t figure out the right size automatically, much like your &#x27;cat&#x27; example above which also doesn&#x27;t<p>c) And this can mean massive performance differences between invocations<p>&gt; Another reason to prefer the cat variant is that it lets you actually string together a normal shell pipeline. For instance, if you want progress information with cat you can combine it with the pv command<p>Umm:<p><pre><code> dd if=file bs=some-optimal-block-size | rest-of-pipeline </code></pre> that was hard.<p>&gt;If you want to create a file of a certain size, you can do so using other standard programs like head. For instance, here are two ways to create a 100 MB file containing all zeroes:<p><pre><code> $ uname -sr OpenBSD 6.0 $ head -c 10MB &#x2F;dev&#x2F;zero head: unknown option -- c usage: head [-count | -n count] [file ...] </code></pre> well.. guess that wasn&#x27;t so &#x27;standard&#x27; after all.. I must be using some nonstandard version...<p><pre><code> $ man head |sed -ne 47,51p HISTORY The head utility first appeared in 1BSD. AUTHORS Bill Joy, August 24, 1977. $ sed -ne 4p &#x2F;usr&#x2F;src&#x2F;usr.bin&#x2F;head&#x2F;head.c * Copyright (c) 1980, 1987 Regents of the University of California. </code></pre> Hmm..<p>&gt; So if you find yourself doing that a lot, I won’t blame you for reaching for dd. But otherwise, try to stick to more standard Unix tools.<p>Like &#x27;pv&#x27;?<p>edit: added formatting, sector size note, head manpage&#x2F;head.c stuffs.. apologies.
评论 #13900618 未加载
评论 #13900349 未加载
评论 #13900484 未加载
评论 #13900274 未加载
viraptor大约 8 年前
There&#x27;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&#x27;s easy to:<p><pre><code> sudo dd .... of=&#x2F;dev&#x2F;... </code></pre> But there&#x27;s no trivial cat equivalent:<p><pre><code> sudo cat ... &gt; target </code></pre> Will open target as your current user anyway. You can play around with tee and redirection of course. But that&#x27;s getting more complicated than the original.
评论 #13899452 未加载
评论 #13899490 未加载
评论 #13899230 未加载
评论 #13900033 未加载
评论 #13908136 未加载
评论 #13900362 未加载
colemannugent大约 8 年前
One thing I&#x27;ll often use dd for is recovering data from a failing drive. Can head ignore read errors? dd can.<p>As far as I&#x27;m concerned, dd is lower-level than most of the other utilities and provides more control over what&#x27;s happening.<p>The author does have a point that the syntax is strange though.
评论 #13899232 未加载
评论 #13898918 未加载
评论 #13899003 未加载
评论 #13993719 未加载
评论 #13900777 未加载
wwalexander大约 8 年前
This article is full of Useless Uses of Cat[1] that could just use redirection operators. For instance,<p><pre><code> cat image.iso | pv &gt;&#x2F;dev&#x2F;sdb </code></pre> could be rewritten as<p><pre><code> pv &lt; image.iso &gt; &#x2F;dev&#x2F;sdb </code></pre> A related mistake is the Useless Use of Echo, since any command of the form<p><pre><code> echo &quot;foo&quot; | bar </code></pre> can be written using here strings as<p><pre><code> bar &lt;&lt;&lt; &quot;foo&quot; </code></pre> or even<p><pre><code> bar &lt;&lt;WORD foo WORD </code></pre> [1] <a href="http:&#x2F;&#x2F;porkmail.org&#x2F;era&#x2F;unix&#x2F;award.html" rel="nofollow">http:&#x2F;&#x2F;porkmail.org&#x2F;era&#x2F;unix&#x2F;award.html</a>
评论 #13901056 未加载
评论 #13900997 未加载
评论 #13901001 未加载
评论 #13900984 未加载
hvs大约 8 年前
For those of you that are blissfully unaware of what the JCL DD command looks like, here&#x27;s a example (with only the DD section of the JCL shown):<p><pre><code> &#x2F;&#x2F;SYSPRINT DD SYSOUT=* &#x2F;&#x2F;SYSLIN DD DSN=&amp;&amp;OBJAPBND, &#x2F;&#x2F; DISP=(NEW,PASS),SPACE=(TRK,(3,3)), &#x2F;&#x2F; DCB=(RECFM=FB,LRECL=80,BLKSIZE=3200), &#x2F;&#x2F; UNIT=&amp;SAMPUNIT &#x2F;&#x2F;SYSLIB DD DSN=SYS1.MACLIB,DISP=SHR &#x2F;&#x2F;SYSIN DD DSN=&amp;SAMPLIB(IEWAPBND),DISP=SHR</code></pre>
评论 #13900269 未加载
tambourine_man大约 8 年前
<i>But who cares? Why not just let the command figure out the right buffer size automatically?</i><p>Because it can be a lot slower. dd is low level, hence powerful and dangerous.<p>And, if we are going down that rabbit hole, you don&#x27;t need cat[1]<p>“The purpose of cat is to concatenate (or &quot;catenate&quot;) files. If it&#x27;s only one file, concatenating it with nothing at all is a waste of time, and costs you a process.”<p>[1]<a href="http:&#x2F;&#x2F;porkmail.org&#x2F;era&#x2F;unix&#x2F;award.html#cat" rel="nofollow">http:&#x2F;&#x2F;porkmail.org&#x2F;era&#x2F;unix&#x2F;award.html#cat</a>
评论 #13899546 未加载
gens大约 8 年前
The Ignorance Of Err Ignorant People<p>dd is a tool. dd can do a lot more then cat. dd can count, seek, skip (seek&#x2F;drop input), and do basic-ish data conversion. dd <i>is</i> standard, even more standard then cat (the GNU breed). I even used it to flip a byte in a binary, a couple of times.<p>New-ish gnu dd even adds a nice progress display option (standard is sending it sigusr1, since dd is made to be scripted where only the exit code matters).<p>&gt; Actually, using dd is almost never necessary, and due to its highly nonstandard syntax is usually just an easy way to mess things up.<p>Personally I never messed it up, nor was confused about it. This sentence also sets the tone of the whole article, a rather subjective tone that is.<p>edit: Some dd usage examples: <a href="http:&#x2F;&#x2F;www.linuxquestions.org&#x2F;questions&#x2F;linux-newbie-8&#x2F;learn-the-dd-command-362506&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.linuxquestions.org&#x2F;questions&#x2F;linux-newbie-8&#x2F;learn...</a>
评论 #13903391 未加载
electrum大约 8 年前
Don&#x27;t cat a file and pipe it into pv. Use &quot;pv file&quot; as a replacement for &quot;cat file&quot; and it will show you the progress as a percentage. When it&#x27;s in the middle of a pipeline, it doesn&#x27;t know the total size (unless you tell it with -s), so it can only show the throughput.
评论 #13899924 未加载
gunnihinn大约 8 年前
A counterpoint: dd survives not because it&#x27;s good or makes sense, but explicitly because it doesn&#x27;t.<p>You wanna format a usb key? Google this, copy&#x2F;paste these dd instructions, it works, move on with your life.<p>You wanna format a usb key using something related to cat you once saw and didn&#x27;t fully understand? Have fun.<p>Both approaches have their weak points, but in any OS the answer to &quot;How do I format a usb key&quot; should not start with &quot;Oh boy, let&#x27;s have a Socratic dialog over 10 years on how to do that.&quot;
评论 #13899187 未加载
评论 #13900696 未加载
knz42大约 8 年前
What about the `seek` argument which skips over some blocks at the beginning but still allocates them (unix &quot;holes&quot;)?<p>Also note that there are still unix systems out there which do not support byte-level granularity of access to block devices. On those devices you must actually use a buffer of exactly the size of the blocks on the device. Heck, linux was like this until at least v2.
评论 #13898786 未加载
chrisfosterelli大约 8 年前
I think dd is primarily so popular because it is used in mostly dangerous operations. Sure, using cat makes logicial sense, but if we are talking about writing directly to disk devices here I&#x27;ll trust the command I read from the manual and not explore commands I <i>think</i> would work.<p>dd&#x27;s &quot;highly nonstandard syntax&quot; comes from the JCL programming language, but it&#x27;s really just another tool to read and write files. At the end of the day it&#x27;s not more complex or incompatible than other unix tools. For example, you can also use tools like `pv` with dd no problem to get progress statements.
评论 #13901227 未加载
评论 #13900011 未加载
donaldihunter大约 8 年前
Cult of pv. It looks to have more command-line complexity than dd. <a href="https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;1&#x2F;pv" rel="nofollow">https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;1&#x2F;pv</a>
评论 #13899163 未加载
angry_octet大约 8 年前
This is a great example of why downvoting submissions should be a thing. Or at least showing the up&#x2F;down tuple. I would say every upvote represents someone misled and likely to further propagate this nonsense.
评论 #13900291 未加载
merlincorey大约 8 年前
The author doesn&#x27;t even give correct invocations of dd (on BSD, at least, for their last example with head).<p>I certainly agree the syntax of the arguments is strange, due to its age, but I don&#x27;t agree that learning it is difficult or a waste of time.<p>All I&#x27;ve learned is that the author doesn&#x27;t like dd well enough to learn it.
betaby大约 8 年前
Author is wrong bs IS useful, try to dd one hard drive to another without reasonable bs (1-8M) with and without and you will see a difference.
评论 #13899174 未加载
评论 #13900538 未加载
评论 #13899385 未加载
snickerbockers大约 8 年前
OP, your alternatives to DD are more complicated, not less complicated. I shouldn&#x27;t need to pipeline two commands together just to cut off the first 100MB of a file.
评论 #13906133 未加载
ocschwar大约 8 年前
Dude&#x27;s missing an important point:<p>If you mess up the syntax on a dd invocation, a nice thing happens: nothing.<p>Use a shell command and pipes, and your command better be perfect before you hit return.
评论 #13899957 未加载
评论 #13899569 未加载
sndean大约 8 年前
Somewhat related short story: Earlier this week my friend said that he dd&#x27;d away just over 50 bitcoins, back when they were worth ~$3 each.<p>&quot;One of the biggest regrets of my life.&quot;
评论 #13899096 未加载
AdamJacobMuller大约 8 年前
I&#x27;ll point out that dd also allows you to control lots of other filesystem and OS-related things that other tools do not. See: fsync&#x2F;fdatasync. I&#x27;m not aware of any shell tools that allow you to write data like that.
gravypod大约 8 年前
An even easier solution: don&#x27;t make people fall into the command line to format a USB reliably.<p>The command line should be reserved for times where you need the fine grain control to do something that DD is meant to do. A GUI should implement everything else in a reliable way that doesn&#x27;t break half the time or crash on unexprected input.
评论 #13899465 未加载
评论 #13899562 未加载
评论 #13899438 未加载
评论 #13899514 未加载
评论 #13899733 未加载
评论 #13993754 未加载
kev009大约 8 年前
Ignorance on the blocksize arg.<p>Also, I only need to remember one progress command for my entire operating system: control+t. I also get a kernel wait channel from that which is phenomenally pertinent to rapidly understanding and diagnosing what the heck a command is doing or why it is stuck.<p>I hate what Linux has done to systems software culture.
emmelaich大约 8 年前
Specifiying a large block size used to help a LOT with performance. From memory shell redirection used a tiny blocksize. On Solaris at least.<p>And <i>if</i> you use dd then you probably should specify a bigger block size than the default of 512 bytes.<p>But yeah, most usage is obsolete.
评论 #13899274 未加载
gabrielblack大约 8 年前
I think this article is full of &quot;alternative computer science&quot; and reminds me other article, published here as well, about the obsolescence of Unix. The only good thing is this discussion thread.
ori_b大约 8 年前
To be fair, dd was mostly a toungue in cheek reference to the overly baroque JCL command for IBM mainframes.
jsd1982大约 8 年前
Interesting assertion. Can you show me a shell invocation without using dd that cuts off the first 16 bytes of a binary file, for example? This is a common reason I use dd.
评论 #13898508 未加载
tardo99大约 8 年前
One of the charms of dd is its hilarious syntax. And, used properly, it&#x27;s a bit of a swiss army knife for a few different disk operations.
noir_lord大约 8 年前
not sure status=progress is that obscure a command, it was added relatively recently as well (in terms of dd).
评论 #13900510 未加载
kazinator大约 8 年前
dd precisely controls the sizes of read, write and lseek system calls. This doesn&#x27;t matter on buffered block devices; there is no &quot;reblocking&quot; benefit.<p>Some kinds of devices are structured such that each write produces a discrete block, with a maximum size (such that any bytes in excess are discarded) and each read reads only from one block, advancing to the next one (such that any unread bytes in the current block due to the buffer being too small are discarded). This is very reminiscent of datagram sockets in the IPC&#x2F;networking arena. dd was developed as an invaluable tool for &quot;reblocking&quot; data for these kinds of devices.<p>One point that the blog author doesn&#x27;t realize (or neglects to comment upon) is that &quot;head -c 100MB&quot; relies on an extension, whereas &quot;dd if=&#x2F;dev&#x2F;zero of=image.iso bs=4MB count=25&quot; is ... <i>almost</i> POSIX: there is no MB suffix documented by POSIX, only &quot;b&quot; and &quot;k&quot; (lower case). The operator &quot;x&quot; is in POSIX: bs=4x1024x1024.<p>Here is a non-useless use of dd to request exactly one byte of input from a TTY in raw mode:<p>file:&#x2F;&#x2F;&#x2F;usr&#x2F;share&#x2F;doc&#x2F;bash-doc&#x2F;examples&#x2F;scripts&#x2F;line-input.bash<p>Wrote that myself, back in 1996; was surprised years later to find it in the Bash distribution.
paulddraper大约 8 年前
My most common use of dd is warming up AWS EBS volumes. <a href="http:&#x2F;&#x2F;docs.aws.amazon.com&#x2F;AWSEC2&#x2F;latest&#x2F;UserGuide&#x2F;ebs-initialize.html" rel="nofollow">http:&#x2F;&#x2F;docs.aws.amazon.com&#x2F;AWSEC2&#x2F;latest&#x2F;UserGuide&#x2F;ebs-initi...</a><p>Though fio is better because it can work in parallel.
diegorbaquero大约 8 年前
Question: Will cat do a bit-to-bit copy between disks?
dom0大约 8 年前
dd is for handling blocked data, while cat, redirection and pipelines are completely useless for that, since they are not meant to manipulate blocks of data, but streams. They do not compare (apart from really simple cases where either will do, like copying a file into some other file); this blog posts mainly highlights that neither the author nor many tutorial writers now the difference.
nwah1大约 8 年前
Someone should write a wiki bot to crawl through the wikis for Arch, Debian, and so forth to help rewrite all these bad instructions.
评论 #13898630 未加载
评论 #13898482 未加载
rurban大约 8 年前
Instead of<p><pre><code> cat image.iso | pv &gt;&#x2F;dev&#x2F;sdb </code></pre> just do<p><pre><code> pv image.iso &gt;&#x2F;dev&#x2F;sdb</code></pre>
bigbugbag大约 8 年前
A self submitted opinion blog post pretty much entirely wrong ending up on HN front page. What gives ?
gbin大约 8 年前
Instead of `cat file | pv &gt; dev` why not `pv file &gt; dev` ?
jeffdavis大约 8 年前
What about writing a block into the middle of a file?
number6大约 8 年前
This is cat abuse
badatusernames大约 8 年前
TLDR This has nothing to do with dunkin donuts