[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Useful use of dd [was: Useless use of "dd"]



On Fri, Jul 02, 2021 at 08:40:39AM +0300, Teemu Likonen wrote:

[...]

>     dd if=/dev/sdX | gzip >image.gz
> 
> is slower but functionally the same as either of these:
> 
>     gzip </dev/sdX >image.gz
>     gzip --to-stdout /dev/sdX >image.gz

Quite right -- akin to "useless use of cat".

FWIW, I've found something which could be deemed to be an "useful use
of dd" which somehow bears a hidden symmetry. As a replacement for
`cat' whenever you need to put a name on the output file.

I'll try to explain: sometimes, you use `cat' to populate a file from
stdin -- say from a script like so:

  #!/bin/sh
  cat <<__EOF >> /etc/hosts
  127.0.0.1 www.google-analytics.com
  __EOF

Now assume you don't want your whole script running under root. A
`sudo cat' (with a suitable setup) or some moral equivalent would
suffice. But, alas! the output redirect `>>' is the one needing the
higher privileges, and that's done by the shell around cat!

The customary pattern here is `tee': it passes stdin through to stdout,
and makes a "side copy" into a named file. So you'll see things like
this

   blah blah | sudo tee myfile > /dev/null

i.e. use `tee's "side copy" and throw away the straight stream.

Now OCD [1] seems to be a professional risk with some computer folks.
This "splitting the channel to throw away one copy" somehow kept tickling
me (although the folks at Combinatory Logic [2] don't seem to have
problems with that kind of wastefulness).

Well, that's where dd rescued me:

  sudo dd of=/etc/hosts oflags=append

(note that if=- is the default). Season to taste :-)

From the "semi-useful ramblings". Perhaps it amuses someone, then it was
downright useful.

FWIW [2], I still use `dd' where I could use `cp' or `cat'. Sometimes it
does align better with my hands. But I agree that it's better left out
when not needed, as is `cat'.

Now to another pet peeve of mine: useless use of grep:

  grep xxxx <myfile> | sed -e 's/bla/foo/' ...

Cheers

[1] Obsessive Compulsive Disorder
[2] https://en.wikipedia.org/wiki/Combinatory_logic#Examples_of_combinators

-- t

Attachment: signature.asc
Description: Digital signature


Reply to: