dd Command Examples in Linux

The dd command copies and converts files to enable them to be transferred from one type of media to another. The dd command has various operands, or actions, to perform.

Operand Used To
if={file name} Specify the file from which data will be read.
of={file name} Specify the file to which data will be written.
bs={bytes} Specify the total block size to read and write, in bytes. Bytes can also be formatted in a more human-friendly way, such as 50M to specify 50 megabytes and 10G to specify 10 gigabytes.
count={blocks} Specify the number of blocks to be written to the output file from the input file.
status={level} Specify the level of information to print to standard error – none to suppress everything except error messages, noxfer to suppress total transfer statistics, progress to display transfer statistics periodically.

Syntax

The syntax of the dd command is:

# dd [options] [operands]

Using dd for backups

You can use dd to perform a full backup of a storage partition. The following example copies data from /dev/sda1 to /dev/sdb2:

# dd if=/dev/sda of=/dev/sdb

Using dd, you can also create an image of a drive and then clone a second drive with it:

# dd if=/dev/sda of=drive_image.iso 
# dd if=drive_image.iso of=/dev/sdb

dd Command Examples

1. To copy a file:

# dd if=old.txt of=old.txt 

2. To read and write specified bytes of a file at a time:

# dd bs=1024 if=old.txt of=old.txt 

3. To convert a file to a specified format:

# dd bs=1024 if=old.txt of=new.txt conv=ascii (from EBCDIC to ASCII)
# dd bs=1024 if=old.txt of=new.txt conv=ebcdic (from ASCII to EBCDIC)
# dd bs=1024 if=old.txt of=new.txt conv=ibm  (from ASCII to alternate EBCDIC)
# dd bs=1024 if=old.txt of=new.txt conv=block    (pad oldline-terminated records with spaces to cbs-size)
# dd bs=1024 if=old.txt of=new.txt conv=unblock (replace trailing spaces in cbs-size records with oldline)
# dd bs=1024 if=old.txt of=new.txt conv=lcase  (change upper case to lower case)
# dd bs=1024 if=old.txt of=new.txt conv=excl  (fail if the output old already exists)
# dd bs=1024 if=old.txt of=new.txt conv=notrunc   (do not truncate the output old)
# dd bs=1024 if=old.txt of=new.txt conv=ucase    (change lower case to upper case)
# dd bs=1024 if=old.txt of=new.txt conv=swab     (swap every pair of input bytes)
# dd bs=1024 if=old.txt of=new.txt conv=noerror  (continue after read errors)
# dd bs=1024 if=old.txt of=new.txt conv=sync    (pad  every  input  block  with  NULs to ibs-size)
# dd bs=1024 if=old.txt of=new.txt conv=fdatasync  (physically write output old data before finishing)
# dd bs=1024 if=old.txt of=new.txt conv=fsync  (likewise, but also write metadata) 

4. To copy only specified number of blocks:

# dd count=1024 if=old.txt of=new.txt 

5. To copy a file with specifying a flag:

# dd if=old.txt of=new.txt oflag=append
# dd if=old.txt iflag=directory of=new.txt oflag=directory
# dd if=old.txt iflag=noatime of=new.txt oflag=append 

6. To convert specified bytes at a time:

# dd cbs=1024 if=old.txt of=new.txt  

7. To read specified number of bytes at a time:

# dd ibs=1024 if=old.txt of=new.txt  

8. To write specified number of bytes at a time:

# dd obs=1024 if=old.txt of=new.txt

9. To skip specified number of blocks at start of o/p:

# dd seek=1024 if=old.txt of=new.txt 

10. To skip specified number of blocks at start of i/p:

# dd skip=1024 if=old.txt of=new.txt 

11. To suppress the transfer statistics:

# dd status=noxfer if=old.txt of=new.txt 

12. To erase the partition table for a device:

# dd if=/dev/zero of=/dev/sda4 bs=512 count=1 
Related Post