Linux dd Command Examples
The "dd" command can be used to copy and convert a file, make copies of partitions (back up a hard drive) and create image files. The dd command can only be run by the root user or a user with sudo privileges. When using the dd command, remember to check that you have sufficient space available at your target location before running the command.
Note: Great care must be taken when using the dd command as you can easily wipe a partition/drive.
Usage: dd [OPERAND]...
Copy a file, converting and formatting according to the operands.
bs=BYTES read and write up to BYTES bytes at a time 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 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=WHICH WHICH info to suppress outputting to stderr; 'noxfer' suppresses transfer stats, 'none' suppresses all
N and BYTES may be followed by the following multiplicative suffixes:
and so on for T (Terabytes), P (petabytes), E (exabytes), Z (zettabytes), and Y (yottabytes).
Each CONV symbol may be:
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 the output for NUL input 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
Each FLAG symbol may be:
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 synchronised 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 discard cached data 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)
Other Options are:
--help display this help and exit --version output version information and exit
Examples of the dd Command
Below are some commonly used examples for the dd command.
Create an iso file from a cdrom
In this example the dd command allows you to create an iso file from a source file.
dd if=/dev/cdrom of=image.iso bs=2k
Create an Image of a Hard Drive
This is a very useful use of the dd command. Here you are going to take an image of an existing hard drive and save it to another storage location.
dd if=/dev/sda of=~/sda_disk.img
Restore an Image file to a Hard Drive
To restore an image file that you have saved from a hard drive you can issue a command similar to:
dd if=sda_disk.img of=/dev/sdb
The above command restores the image file taken from /dev/sda and restores it to the location of /dev/sdb.
Make a Backup of a Hard Drive
The following dd command will make a backup of the specified drive to another drive attached to the same system.
dd if=/dev/sda of=/dev/sdb conv=noerror,sync
The above copies /dev/sda to /dev/sdb. The options "conv=noerror,sync" is used to specify that we do not stop processing when an error occurs. The sync parameter specifies that any missing input is replaced with null bytes and processed normally.
Make a Backup of a specified Partition
The following dd command will allow you to make a copy of a specified partition.
dd if=/dev/sda1 of=~/partition_sda1.img
Backup your MBR - Master Boot Record with dd
The following dd command will make a backup of the master boot record of the specified disk. The MBR is a 512 byte boot sector that is the first sector of a partitioned disk.
dd if=/dev/sda of=~/partition_sda1.mbr bs=512 count=1
Restore your MBR - Master Boot Record with dd
The following dd command will restore a previously saved copy of your MBR to the specified drive.
dd if=~/partition_sda1.mbr of=/dev/sda count=1 bs=512
Create a dummy test file with dd
The following dd command will create a dummy test file with the size specified by the block size and count.
dd if=/dev/zero of=~/test1.file bs=1024 count=1
The above will create a dummy test file of 1024 bytes in size.
dd if=/dev/zero of=~/test2.file bs=1024 count=1024
The above will create a dummy test file with a size of 1MB.
dd if=/dev/zero of=~/test3.file bs=1M count=10
The above dd command creates a dummy test file with a size of 10MB.
Output from above Commands:
dd if=/dev/zero of=~/test1.file bs=1024 count=1 1+0 records in 1+0 records out 1024 bytes (1.0 kB) copied, 0.000406424 s, 2.5 MB/s $ dd if=/dev/zero of=~/test2.file bs=1024 count=1024 1024+0 records in 1024+0 records out 1048576 bytes (1.0 MB) copied, 0.00375862 s, 279 MB/s $ dd if=/dev/zero of=~/test3.file bs=1M count=10 10+0 records in 10+0 records out 10485760 bytes (10 MB) copied, 0.0186268 s, 563 MB/s $ ls -rtlh test* -rw-rw-r-- 1 john john 1.0K Sep 2 11:37 test1.file -rw-rw-r-- 1 john john 1.0M Sep 2 11:38 test2.file -rw-rw-r-- 1 john john 10M Sep 2 11:38 test3.file
Note: If you are interested in creating files for testing, you may like to consider the "fallocate command. Follow the link fallocate command.