Search notes:

Shell command: dd

dd is used to read from an input file stream and to write to an output file stream. It can perform encoding conversions.
dd [OPERAND]…
dd OPTION

Operands

bs=BYTES Read and write up to BYTES bytes at a time (default: 512); overrides ibs and obs
cbs=BYTES Convert BYTES bytes at a time
conv=CONVS Convert the file as per the comma separated symbol list
count=N Copy only N input blocks
ibs=BYTES Read up to BYTES bytes at a time (default: 512)
if=FILE Read from FILE instead of stdin, stdout and stderr
iflag=FLAGS Read as per the comma separated symbol list
obs=BYTES Write BYTES bytes at a time (default: 512)
of=FILE Write to FILE instead of stdout
oflag=FLAGS Write as per the comma separated symbol list
seek=N Skip N obs-sized blocks at start of output
skip=N Skip N ibs-sized blocks at start of input
status=LEVEL LEVEL detemrmines the amount of information printed to stderr; none suppresses everything but error messages (similar to quiet mode which is turned on with -q in other shell commands), noxfer suppresses the final transfer statistics, progress shows periodic transfer statistics (Compare with pv)

Multiplicatative suffixes

N and BYTES may be followed by multiplicative suffixes:
c 1,
w 2,
b 512,
kB 1000,
K, KiB 1024,
MB 1000*1000,
M, MiB 1024*1024,
xM M,
GB 1000*1000*1000,
G, MiB 1024*1024*1024
and so on for T, P, E, Z, Y.

CONV symbols

ascii From EBCDIC to ASCII
ebcdic From ASCII to EBCDIC
ibm From ASCII to alternate EBCDIC
block Pad newline-terminated records with spaces to cbs-size
unblock Replace trailing spaces in cbs-size records with newline
lcase Change upper case to lower case
ucase Change lower case to upper case
sparse Try to seek rather than write all-NUL output blocks
swab Swap every pair of input bytes
sync Pad every input block with NULs to ibs-size; when used with block or unblock, pad with spaces rather than NULs
excl Fail if the output file already exists
nocreat Do not create the output file
notrunc Do not truncate the output file
noerror Continue after read errors
fdatasync Physically write output file data before finishing
fsync Likewise, but also write metadata

FLAG symbols

append Append mode (makes sense only for output; conv=notrunc suggested)
direct Use direct I/O for data
directory fail unless a directory
dsync Use synchronized I/O for data
sync Likewise, but also for metadata
fullblock accumulate full blocks of input (iflag only)
nonblock Use non-blocking I/O
noatime Do not update access time
nocache Request to drop cache. See also oflag=sync
noctty Do not assign controlling terminal from file
nofollow Do not follow symlinks
count_bytes Treat count=N as a byte count (iflag only)
skip_bytes Treat skip=N as a byte count (iflag only)
seek_bytes Treat seek=N as a byte count (oflag only)

Options

dd has two options only:
--help Display this help and exit
--version Output version information and exit

Create a 1GB file with random data

The following command creates a 1GB file with random data:
$ dd of=1GB if=/dev/random bs=GiB count=1

Handling of trailing bytes

If the input file is not exactly a multiple of the specified bs, the trailing bytes will, as expected, be copied.
Here's a file with exactly 516 (= 2*256 + 4) bytes:
123456789 12345
 2 * 16 =  32 |
 3 * 16 =  48 |
 4 * 16 =  64 |
 5 * 16 =  80 |
 6 * 16 =  96 |
 7 * 16 = 112 |
 8 * 16 = 128 |
 9 * 16 = 144 |
10 * 16 = 160 |
11 * 16 = 176 |
12 * 16 = 192 |
13 * 16 = 208 |
14 * 16 = 224 |
15 * 16 = 240 |
16 * 16 = 256 |
17 * 16 = 272 |
18 * 16 = 288 |
19 * 16 = 304 |
20 * 16 = 320 |
21 * 16 = 336 |
22 * 16 = 352 |
23 * 16 = 368 |
24 * 16 = 384 |
25 * 16 = 400 |
26 * 16 = 416 |
27 * 16 = 432 |
28 * 16 = 448 |
29 * 16 = 464 |
30 * 16 = 480 |
31 * 16 = 496 |
32 * 16 = 512 |
abc
Github repository shell-commands, path: /dd/trailing-bytes/516.bytes
This file is copied with dd:
dd if=516.bytes of=out.file bs=256
Github repository shell-commands, path: /dd/trailing-bytes/copy-file
The command will print 2+1 records in. The 2 indiciates that 2 blocks of 256 bytes where transferred, the +1 refers to the incomplete (less than 256 byte) final block (of 4 bytes).
If the input file had been exactly 512 bytes, dd would have reported 2+0 records in.

Using dd to make backups of partitions

Creating a backup of the harddisk (/dev/sda) of a laptop.
Use a live CD to start Linux on the laptop.
Make sure that no partition of the harddisk is mounted
mount -l | grep sda
Mounting the backup destination drive
mount -t ntfs /dev/sdb1 /mnt
Creating the destination folders
mkdir /mnt/Backups/T420
Writing the backup
dd if=/dev/sda bs=1M | gzip > /mnt/Backups/T420/backup.sda.gz
Restoring the backup
gunzip -c /mnt/Backups/T420/backup.sda.gz | dd of=/dev/sda bs=1M  

Misc

Sending a USR1 signal to a running dd process makes it print I/O statistics to standard error and then resume copying.

See also

Shell commands such as truncate

Index