by dredmorbius · 4y ago · 0 comments
There remain some useful applications of dd. These may of course be achieved by other mechanisms, but typically less conveniently.
1. Read a specific number of blocks or bytes from a source:
dd if=/dev/hda of=/root/mbr bs=512 count=1
This will make a copy of, say, your master boot record (first 512 bytes of your first disk drive) and stash it in your /root directory.2. Read from specific bytes of a file
dd if=mydata skip=1k bs=32 count=1
Reads 32 bytes after the first 1024 (1k) bytes of "mydata".3. Write to specific bytes of a file
dd if=source of=target seek=10k bs=512 count=1 conv=notrunc
That should write 512 bytes from "source" beginning 10k into "target". (I've not tested this, you should verify.)4. Create a sparse file. Sparse files appear to have a nonzero size, but take up no space on disk, until data is actually written to them. These are often used as "inflating" dynamic filesystem images for virtual machines.
dd if=/dev/zero of=sparsefile bs=1 count=0 seek=20000M # Create 20 GB sparse file
5. Case conversions. Sure, you could use tr(1), but where's the sport? dd if=MixEdCaSE of=lcase conv=lcase # Convert to lower case
dd if=MixEdCaSE of=ucase conv=ucase # Convert to upper case
6. ASCII / EBCDIC conversions dd if=ebcdic of=ascii conv=ascii # ebcdic -> ascii
dd if=ascii of=ebcdic conv=ebcdic # ascii -> ebcdic
When reading to or from IBM data tapes, you might find blocking / unblocking conversions useful. I've done this, but it's so long ago that I don't trust my memory on that any more. Odds are good you'll not have to worry about this.There are other useful applications as well, though these are not typically encountered very often. Do feel free to explore and attempt these on safe media.
The specific recipies should be vetted. The stated goals can be achieved with proper invocation.
Especially handy when you've fed in a huge amount of JSON (sometimes all on one line because, y'know, why not) into jq and you get the inscrutable output:
parse error: Invalid numeric literal at line 1, column 236162512 tail --bytes=+236162512(edit: I do have to concede that the `tail|head` version is much faster than the `dd` - ~11s vs ~65s in my quick test with that ^skip)
Large blocks -> efficient I/O. Within reason.
I'll keep that in mind, json-parsing being among my current hobbies...
What does the (1) mean beside dd? I see this with man pages. Is it a version identifier?
Edit: thank you both for taking the time to share. I appreciate the quick response.
man 1 <name> # executable: <name>(1)
man 3 <name> # function: <name>(3)
Without the distinction argument, man will throw its arms up in the air and give up.However, if there’s no ambiguity (such as with ‘dd’ where only the executable exists), you can drop the section number parameter when running man. man will then search all the sections, see that ‘dd’ only exists in section 1, and go from there.
This is all ancient knowledge and well in place back in the mid 1980's long before Linux or any of that stuff. The `man` page sections used to be different 3-ring binders all printed out sitting on a table in the computer lab. The 'sections' were just different binders of documentation. Get off my lawn!!!
I also use the notation as a convention to indicate that I'm referring to a Unix command (or library function, etc.). E.g., "cat" might refer to a feline, but "cat(1)" should more clearly refer to the Unix / Linux command.
(Where I've got proper markup, I'll typically set commands or references in monospace using backtick notation: `dd`, `cat`, etc.
You can do that here by putting them on a separate line (two newlines, separate paragraph) and indenting by a couple characters.
At least in man-db and FreeBSD implementations, it actually shows you the first page it found by default.
I just tested, and `man man` opened man(1) despite man(7) existing.
man exe <name>
man lib <name> $ man -k ''
And if you just want to see the ones for high-level documentation (i.e. section 7), then $ man -s 7 -k ''
does the trick. Sections 7 and 5, in particular, are full of hidden gems.edit: `man -k ''` might be a GNUism too. Just tried on a BSD derived OS and got back nothing.
the unix manual was commonly printed out, and these section numbers were a necessity for looking things up. Collation was section number, and then alphabetical
when you were first introduced to unix, you sat down with the manual and read it.
head -b 512
Will also copy the first 512 bytes in case you want to avoid dd for clarity. I have actually used that for moving mbrs around.Though in this case I think previous posters are mistaken. My GNU implementation of head supports -c and not -b.
Perhaps it's the fact that the longopt is --bytes that caused the confusion.
What's keeping other implementations from adding these features?
I simply miss-rememered.
Note that this can also be done using truncate(1).
The thing is that dd issues a read() for each block, but is doesn't actually care how many bytes it gets back in response (unless you turn on fullblock mode).
This isn't really a problem when you're reading from a block device, because it's pretty uncommon to get back less data than you requested. But when you're reading from a pipe, it can/does happen sometimes. So you might ask for five 32-byte chunks, and get [32, 32, 30, 32, 32]-sized chunks instead. This has the effect of messing up the contents of file you're writing, with possibly destructive effects.
To avoid it, use `tee` or something else. Or use iflag=fullblock to ensure that you get every byte you request (up to EOF or count==N).
dd bs=440 count=1 if=/usr/lib/syslinux/bios/mbr.bin of=/dev/sda
Even in the age of EFI/GPT, that still gets used often (usually VM providers that only offer MBR boot). head -b 440 /usr/lib/syslinux/bios/mbr.bin > /dev/sdaFun fact, the -c usage comes from ksh, where head is a shell builtin.