Funny Unix commands: dd
Well, guys, we are at the summer issue, and I take a little ‘Holidays for me as well, as you can see by the brevity of this article. Okay, would make more sense that I had written more, because on the one hand you have more time for reading, and on the other hand, the lines should be less loaded and leave this issue of BETA download faster. Whatever. I’m lazy, the end is near, and I do not feel right to put on the stain to the keyboard between the heat and mosquitoes.
On the other hand, I think the topic of this issue is quite interesting: in fact, deal with dd, the date duplicator, a relatively unknown program, but which I find very useful for many things that no one would suspect at first sight. Before submitting the command in all aspects, I want to tell a legend that explains why its syntax is so funny – but find!. Unfortunately I can not remember the source of what I will say, but nothing takes away the breath of antiquity that pervades the story, and that makes us feel so small …
Legend has it that in the distant past, when UNIX was born first, its creators were in need at some point a low-level command to copy data from one device to another. However, since were in a hurry, you began to study the syntax of the command, and copied the weight used in IBM mainframe-360, so there would be time to define a command interface consistent with the other. Time passed, and everyone was so used to the idiosyncrasies of dd, it occurred to anyone to rewrite a similar command. Is it true? I do not know, but I repeat that it would still be well devised. But we see in detail how it works.
Options
Actually, dd is not quite like any other Unix commands: it is also a filter, which by default that reads from standard input and writes to standard output. In short, if you write simply “dd” and press return, the terminal remains quiet. You can write what you want, and end with ctl-D, at this point will be rewritten as typed, and then dd will add his signature, such as “ 0 +10 records in 0 +1 records out ,” so to have some fun.
Call to chatter: The complete syntax is as follows.
dd [if=file] [of=file] [ibs=bytes] [obs=bytes]
[bs=bytes] [cbs=bytes] [skip=blocks] [seek=blocks]
[count=blocks] [conv={ascii,ebcdic,ibm,block,
unblock,lcase,ucase,swab,noerror,notrunc,sync}]
In short, all options are of the form variable=value. Warning: you can not put any spaces before or after the equal sign. This broke a little ‘boxes at one time, since the shell does not expand the file names in this situation and needed everything, fortunately bash Linux is smart enough to notice the context and the expansion to be made to work. In practice, nicc problem!
Another important thing to remember is that all the values that accept bytes may be followed by a multiplier. The Linux version of dd recognizes b lock (512), k for kilobytes (1024), w for word (2), and thirteenth to multiply by a number m, this becomes the value of block.
Below is given a more complete explanation of the various options.
if=filein and of=fileout
tell dd respectively, read from and write to filein fileout. In the Linux version, in which case the output file is truncated to the value given to seek, or 0 if the keyword is not present. But be careful notrunc option!
ibs=nn and obs=nn
specify how many bytes must be read or written per record, that is, to write. I would say that the default is 512 bytes, ie a block, so it happens in ordinary files. Of course it is possible that the rest is thrown away: when the output of dd showed above that said “0 +10 records”, I had written ten lines, all less than 512 characters, and therefore were treated as “remnants”. These parameters are very important when using special devices as input or output: the network should have read IBS for example set to 20k, and has as a block floppino “natural” 18k. Do not set these values can be not only more time to execute the command, but also in time-out errors, if not worse. User warned …
bs=nn
is an abbreviation to indicate how many bytes are simultaneously read and written at a time.
cbs=nn
please set the buffer conversion nnbyte. According to the manual, this is used when translating from ASCII to EBCDIC, or a device does not block in a block: Used together with the conversion block and unblock. I do not know. I never had the misfortune of having to use that option, and still live happy ![]()
skip=nbl and seek=nbl
tell the program to skip nblblocchi respectively at the beginning of the input and the output. Obviously the latter makes sense only if you give the conversion notrunc: see below. The block size is given by the value of ibs (obs). A useful observation: in fact it can safely write 1b skip = 512 to indicate “something”. But if you set ibs 1, the “something” is a block in practice, this means 512 * 512 bytes, or 256K. It was what you wanted?
count=nbl
it copied only the input nbl blocks, each the size indicated by ibs. This option, together with the preceding, it is nice when you have a corrupted file and you want to recover as much as possible, it skips the offending party and you get the rest.
conv=conversion [, conversion ...]
converts the file as specified by its argument. Possible conversions, at least in the Linux version, are ascii, which converts from EBCDIC to ASCII ebcdic and ibm, which are both a reverse conversion; block, stretching all the records terminated by a newline at length CBS, replacing the newline spaces; unblock, which does the opposite (trailing spaces removed, and adds a newline); lcase and ucase, to convert text to lowercase or uppercase; swab, which exchanges between them two by two bytes of input (For example, if you have written a 2-byte integer file on a machine based on 680×0 and must read on a PC); noerror, to continue the conversion in the case of input errors, sync, stretching blocks Input the size of ibs adding NUL.
Examples
The canonical example of use of dd is one in which there will certainly be encountered when you created your first floppy disk Linux: how to write a floppino without putting up the MS-DOS filesystem. The solution is simple:
% dd if=disk.img of=/dev/fd0 obs=18k count=80
I decided not to use IBS because I do not know what the best value for a hard disk, I could still use BS instead of obs and everything would be fine. Please note that for safety I also indicated the number of sectors to write (80), and I have written directly on behalf of low-level floppy.
Another useful application of dd occurs in the case of network backup. Suppose you are on the machine alpha, beta, and that on the machine there is a tape /dev/rst0 with a tar file that interests us, is not large enough to be able to be saved and then copied on beta. In this case, we can write
% rsh beta 'dd if=/dev/rst0 ibs=8k obs=20k' | tar xvBf
to do the operation in a single step. In this case, we used rsh to read from the tape: size of input and output are made to the default for these operations, ie, 8KB and 20KB read from tape to write on the ethernet. From the point of view of our local tar, the bytes arriving are exactly those that would have seen the tape, apart from the somewhat ‘strange with which they come, which is why I added the B to the [6].
Finally, a quick command that can be useful when you get a Word file from a Mac, and it turns out that despite all this remains unreadable because controconversioni written in MacBinary: just remove the first 128 bytes.
% dd if=file.bad of=file.doc ibs=128 skip=1
In short, it’s always handy to have on hand our data doubler.














