Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


This. The alternative:

    sudo sh -c 'cat some.img > /dev/sdb'
or even more baroque:

    cat some.img | sudo tee /dev/sdb > /dev/null
is a pain by comparison, and the `sudo sh -c` variant has env implications when spawning a sub-shell.

I have an ARM/linux installer script that writes the u-boot image to a specific offset before the first partition:

    dd if=${UBOOT_DIR}/MLO of=$LO_DEVICE count=1 seek=1 bs=128k
    dd if=${UBOOT_DIR}/u-boot.img of=$LO_DEVICE count=2 seek=1 bs=384k
This is admittedly somewhat esoteric, but it seems like a stretch to say `dd` does not have some place, especially when transferring binary data in very specific ways.


Since we're sharing shell tricks: The "sudo tee > /dev/null" may be baroque, but I find it useful whenever I start editing stuff in /etc in vim, only to find that I cannot write my changes because I'm not root. In that case,

  :w !sudo tee %
does the trick. (What "w!" does is send the buffer into the given shell command as stdin.)


Even simpler:

    sudo cp image.iso /dev/sdb


Would that seriously work?


Yep! See this: http://askubuntu.com/questions/751193/what-is-the-difference...

Basically, you can use cp wherever you use dd, as long as you're not changing any low-level parameters (e.g. starting 500 bytes into the file or something).


Yes. Yes it does. Reminds me of a Sunday evening in the late 90ies when I stopped working as root all the time:

    cp backup.tar.bz /dev/sda 
Nowadays I would know enough to at least get the contents of the backup.tar.bz back. Back then, this was the end of both my / partition (or any other partition) and the backup of my music collection.

Still, that didn't end my love affair with Unix. It did make me a whole lot more careful though.


> cp backup.tar.bz /dev/sda

Ouch. That just hurts seeing that line.


I think the trick is to do sudo sh, or some such.


There is a trivial alternative. Just use a subshell.

    sudo (cat ... > target)


I don't know if this works but I believe it doesn't.


It doesn’t. In fact, I would be very careful using subshellsm in general, because it can lead to bugs like this one: ​http://danwalsh.livejournal.com/74642.html​ .


That link 404s due to some url-encoded garbage at the end. This should work: http://danwalsh.livejournal.com/74642.html


Well that's strictly a bug caused by mistaken use, as strings are not expanded lazily and heredocs are just another string syntax. How can one use the unix shell without string interpolation? Also, a similar programme would give the wrong result in, say, Ruby or Perl too.


    sudo bash


but a (boring) su and working as root?




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: