The Wayback Machine - https://web.archive.org/web/20250114213322/https://www.geeksforgeeks.org/dd-command-linux/
Open In App

‘dd’ command in Linux

Last Updated : 22 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

dd is a command-line utility for Unix and Unix-like operating systems whose primary purpose is to convert and copy files.

  • On Unix, device drivers for hardware (such as hard disk drives) and special device files (such as /dev/zero and /dev/random) appear in the file system just like normal files.
  • dd can also read and/or write from/to these files, provided that function is implemented in their respective drivers
  • As a result, dd can be used for tasks such as backing up the boot sector of a hard drive, and obtaining a fixed amount of random data.
  • The dd program can also perform conversions on the data as it is copied, including byte order swapping and conversion to and from the ASCII and EBCDIC text encodings.

How the dd Command Works

The command line syntax of dd differs from many other Unix programs, in that it uses the syntax option=value for its command line options, rather than the more standard -option value or –option=value formats. By default, dd reads from stdin and writes to stdout, but these can be changed by using the if (input file) and of (output file) options.

Practical Use Cases for the dd command

1. To backup the entire harddisk

To backup an entire copy of a hard disk to another hard disk connected to the same system, execute the dd command as shown. In this dd command example, the UNIX device name of the source hard disk is /dev/hda, and device name of the target hard disk is /dev/hdb.

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

Explanation:

  • if stands for “input file,” and of stands for “output file.” In this example, it copies all data from /dev/sda to /dev/sdb.
  • The command creates an exact copy of /dev/sda on /dev/sdb.

Handling Errors:

If there are any errors, the above command will fail. If you give the parameter “conv=noerror” then it will continue to copy if there are read errors.

Input file and output file should be mentioned very carefully. Just in case, you mention source device in the target and vice versa, you might loss all your data.

To copy, hard drive to hard drive using dd command given below, sync option allows you to copy everything using synchronized I/O.

# dd if=/dev/sda of=/dev/sdb conv=noerror, sync

2. To backup a Partition

You can use the device name of a partition in the input file, and in the output either you can specify your target path or image file as shown in the dd command.

# dd if=/dev/hda1 of=~/partition.img

3. To create an image of a Hard Disk

Instead of taking a backup of the hard disk, you can create an image file of the hard disk and save it in other storage devices. There are many advantages of backing up your data to a disk image, one being the ease of use.

This method is typically faster than other types of backups, enabling you to quickly restore data following an unexpected catastrophe.It creates the image of a hard disk /dev/hda.

# dd if=/dev/hda of=~/hdadisk.img

4. To restore using the Hard Disk Image

To restore a hard disk with the image file of an another hard disk, the following dd command can be used

# dd if=hdadisk.img of=/dev/hdb

The image file hdadisk.img file, is the image of a /dev/hda, so the above command will restore the image of /dev/hda to /dev/hdb.

5. To create CDROM Backup

dd command allows you to create an iso file from a source file. So we can insert the CD and enter dd command to create an iso file of a CD content.

# dd if=/dev/cdrom of=tgsservice.iso bs=2048

dd command reads one block of input and process it and writes it into an output file. You can specify the block size for input and output file. In the above dd command example, the parameter “bs” specifies the block size for the both the input and output file. So dd uses 2048bytes as a block size in the above command.

Conclusion

The dd command is a versatile tool in Linux and Unix systems, capable of handling a variety of tasks, from copying and backing up disks to performing complex data conversions. Its ability to interact with device files makes it particularly powerful for system administration tasks, including disk imaging, partition backups, and creating bootable media. Although dd is a simple command, it must be used carefully to avoid data loss, especially when dealing with low-level system components like hard drives and partitions.

dd command in Linux – FAQs

What does the bs option do in the dd command?

The bs option specifies the block size for input and output operations. A larger block size (e.g., bs=4M) can speed up the process, especially for large files or disk copying.

How can I use dd to create a bootable USB drive?

You can write an ISO file to a USB drive using dd by specifying the ISO file as the input and the USB drive as the output:

sudo dd if=/path/to/iso of=/dev/sdX bs=4M status=progress

Is dd suitable for creating backups?

Yes, dd is commonly used to create full backups of disks or partitions. However, for more advanced backup strategies, other tools like rsync or tar may be more efficient.

What is the difference between conv=noerror and sync in dd?

conv=noerror instructs dd to continue copying even if errors are encountered, while sync ensures that blocks are padded with null bytes if there are partial reads, preserving the data structure.

    Master DevOps and also get 90% Course fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

    The most sought-after Three 90 challenge has started and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!


    Next Article
    Article Tags :

    Similar Reads

    three90RightbarBannerImg