dd Command in Linux



The dd command in Linux copies and converts raw data between files, devices, partitions, and storage devices. Raw data signifies the data collected directly from its source in an unprocessed form.

The dd command is primarily used for copying, converting, erasing, backing, and restoring data. Moreover, it is also used to create bootable USB drives.

Table of Contents

Note − The dd command is a powerful tool, but it should be used with caution. It can overwrite files without warning, and this action is irreversible. Therefore, it is advisable to exercise care when using it.

Syntax for dd Command

The basic syntax of the Linux dd command is as follows −

dd if=[INPUT_FILE] of=[OUTPUT_FILE] [OPTIONS]

Here, if and of indicate the input and output files respectively. These options take filenames or file paths on which the operation will be performed. While [OPTIONS] are replaced with dd command options such as bs is used to set block size, conv converts the file, and count sets the number of blocks to copy. These options are briefly discussed in the following section.

Options for dd Command

The Linux dd command comes with various parameters that take a value. Commonly used options are listed below −

Options Description
bs Sets the block size for reading from the input file and writing to the output file
count Sets the number of blocks to copy
conv Sets the conversion options for input or output file*
skip Skips the specified number of blocks while reading the input file
seek Skips the specified number of block while copying to the output file
status Displays the level of information to the standard input; such as progress, or error messages
ibs Sets the input block size
obs Sets the output block size
iflag Modifies how input file is read^
oflag Modifies how output file is written^

The conv parameter options are listed in the following table −

Options Description
ascii Converts EBCDIC to ASCII
ebcdic Converts ASCII to EBCDIC
lcase Converts uppercase to lower
ucase Converts lowercase to upper
swab Swap every pair of input byte
sync Pads the input block to the specified block size
excl Fails to copy with output that already exists
nocreate Does not create an output file
notrunc Does not truncate the output file
noerror Continue after reading the errors

The values of iflag and oflag parameters are given in the following table −

Options Description
append To append data from the input file to the output
direct To directly refer to the I/O for data rather than relying on OS
nocache Directly reads and writes data without using the OS cache
noatime To prevent the operating system from creating access timestamps
nofollow To instruct command not to follow the symbolic links

Using dd Command in Linux

This section lists some useful examples of dd command usage in Linux.

Copying Content of Input to Output Files

Copying the content of one file to another is one of the basic functions of the dd command.

dd if=file.txt of=newfile.txt
dd Command Linux 1

The files should be in the current working directory.

It is important to note that the dd command overwrites the output file if it already exists.

Avoid Overwriting the Output File

By default, the dd command overwrites the content of the output file if it already exists. To avoid this action the conv option with the excl value is used.

dd if=file.txt of=newfile.txt conv=excl
dd Command Linux 2

The excl value warns and prevents the output file from being overwritten.

Appending Data to a File

To append the content of the input file to the output, the oflag option is used with the append as value. The oflag defines how the output file will be written. In our case, the data from the input file will be appended. While the notrunc value with the conv option prevents the truncation of the output file.

dd if=file.txt of=newfile.txt oflag=append conv=notrunc
dd Command Linux 3

Changing Text Case

To change the case of the input file and copy it to the output file the conv option is used. The ucase and lcase values are used for uppercase and lowercase respectively.

The following command converts the lowercase content in the input file to uppercase and copies it to the output file.

dd if=file.txt of=newfile.txt conv=ucase
dd Command Linux 4

Skipping Bytes from the Input File

To skip bytes from the input file first set the input block size. Then use the skip option to specify the number of blocks to skip.

dd if=file.txt of=newfile.txt ibs=4 skip=2

In the above command, the block size is 4 bytes, and 2 blocks will be skipped, which means 8 bytes. In the text file, each character is equal to one byte. So, 8 characters will be skipped.

dd Command Linux 5

Note − In the following examples, I will be using /dev/sdX instead of explicitly mentioning the /dev/sda or /dev/sdb to avoid misinterpretation. Where X can be a, b, or c.

Creating Backups of Linux Drive or Partition

The dd command is a handy tool to create a backup of a partition, or entire hard drive.

In Linux, the /dev directory contains the device files. The device files represent block devices, such as hard drives, SSDs, USB drives, or CD-ROMs. The /dev/sda represents the primary hard drive, while /dev/sda1, or /dev/sda2 represent partitions.

To list the block devices, use the lsblk command.

lsblk
dd Command Linux 6

Note − If you are using a virtual machine running on a Linux-based hypervisor, the disk associated with the virtual machine is typically represented by the device name /dev/vda.

Backing up the live drive is not a straightforward process. You cannot create a backup of a live drive. For that, first, create a bootable USB drive, insert it into the system, and boot the operating system. Then list the block devices and unmount the primary drive of the system that you want to create a backup of.

sudo umount /dev/sdX

Use the following command to create the backup of the entire hard drive −

sudo dd if=/dev/sdX of=drive_backup.img

Note that to access the block devices you need sudo permissions.

To create the backup of the partition, use −

sudo dd if=/dev/sdX1 of=part_backup.img
dd Command Linux 7

You can also set the block size while creating the backup. The smaller block size improves the storage efficiency, while the larger block size improves the transfer rate.

sudo dd if=/dev/sdX of=drive_backup.img bs=1G
sudo dd if=/dev/sdX1 of=part_backup.img bs=1M

Restoring Backups of Linux Drive and Partition

To restore the backup, change the input file with the backup file and output with the target disk.

To avoid data corruption, unmount the drive, if it is active.

sudo umount /dev/sdX

To restore the backup of the entire drive, use −

sudo dd if=drive_backup.img of=/dev/sdX

Similarly, to restore the backup of a partition, use −

sudo umount /dev/sdX1
sudo dd if=part_backup.img of=/dev/sdX1
dd Command Linux 8

Creating & Restoring a Backup of the Master Boot Record (MBR)

The Master Boot Record also known as MBR is 512 bytes sector in the hard drive and contains information about the partition table and operating system.

To back up the MBR on Linux, use the following command.

sudo dd if=/dev/sdX of=mbr_backup.img bs=512 count=1
dd Command Linux 9

Here, the block size is set to 512 and count to 1. The first 512 bytes of the drive will be backed up.

Similarly, to restore the MBR, execute the following command −

sudo dd if=mbr_backup.img of=/dev/sdX bs=512 count=1

To restore the MBR, you do not need to unmount the drive, because the MBR is stored outside the active partition.

Copying Data from CD/DVD Drives

The dd command can be used to copy data from a CD/DVD drive.

sudo dd if=/dev/cdrom of=disk.iso

In Linux, the CD or DVD drives are represented by /dev/cdrom.

Erasing a Drive

The dd command can be employed to completely wipe the drive. It essentially writes zeros to all the blocks of the drive.

Before erasing the drive, ensure it is not mounted.

sudo dd if=/dev/zeros of=/dev/sdX bs=2M

The above command writes null characters to the target device. It is recommended to fill the drive with random data to make it unrecoverable.

sudo dd if=/dev/urandom of=/dev/sdX bs=2M

Creating a Bootable Drive

The bootable drive is used to boot and install an operating system ISO. The dd command easily creates a bootable drive without any third-party tool.

To create a bootable USB drive, first, connect it to the system and identify its name using lsblk utility.

Assume, the USB drive name is /dev/sdX, where X is the letter assigned to the drive.

sudo dd if=ubuntu.iso of=/dev/sdX bs=8M

To monitor the length process, it is a good practice to enable the progress bar.

Enabling Progress Bar

Creating a bootable drive or erasing it takes a lot of time. To keep track of the progress, it is advisable to enable the progress bar. The status option of the dd command is used to set the progress bar of the current task.

sudo dd if=ubuntu.iso of=/dev/sdX bs=8M status=progress

In the above command, the status is set to progress.

Troubleshooting the Common Issues of dd Command

The dd command is not a perfect tool, it does come with shortcomings. Common issues and their solutions are mentioned below −

Avoiding Data Corruption

The dd command can potentially corrupt data during copying if the specified blocks are misaligned. To prevent data corruption, it's important to synchronize partial blocks or small reads/writes with the specified block size.

The conv=sync option is used to achieve this synchronization. When sync is used, dd fills any smaller blocks with zeros, aligning them properly with the assigned block size. This ensures that data is correctly aligned and prevents corruption during copying operations.

sudo dd if=/dev/sdX of=drive_backup.img bs=4M conv=sync

Managing Slow Performance

The performance of the dd command can be painfully slow. It largely depends upon the block size and disk speed. Reducing block sizes can lead to slow performance. To improve, it is recommended to keep the larger block size.

Conclusion

The dd command in Linux is essentially copying the content of one file to another or converting it. It is a useful tool for creating the backup of the drives and partitions and restoring it. To completely wipe a hard drive and to make it unrecoverable, the dd command can be employed. Moreover, you can also create a bootable USB drive from your terminal using it.

This guide covers the syntax, various options, and examples of the Linux dd command.

Advertisements