Linux dd Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the powerful dd command in Linux to create backup images of storage devices, such as USB drives, and restore them. The lab covers the syntax and options of the dd command, as well as practical examples of creating and restoring backup images. This lab is part of the Disk and File System Utilities skill set, which is essential for system administrators and power users who need to perform low-level data manipulation tasks.

The lab starts by explaining the syntax and options of the dd command, including how to specify the input and output files or devices, set the block size, and control the status updates during the copy process. Then, it demonstrates how to create a backup image of a USB drive and restore that image to another USB drive. The lab assumes that you have the necessary permissions to access the storage devices on your system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/cp("`File Copying`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") subgraph Lab Skills linux/cp -.-> lab-422630{{"`Linux dd Command with Practical Examples`"}} linux/sudo -.-> lab-422630{{"`Linux dd Command with Practical Examples`"}} linux/dd -.-> lab-422630{{"`Linux dd Command with Practical Examples`"}} end

Understand the dd Command Syntax and Options

In this step, we will learn about the syntax and options of the dd command in Linux. The dd command is a powerful tool used for low-level data manipulation, including creating backup images of storage devices.

First, let's understand the basic syntax of the dd command:

dd if=<input_file> of=<output_file> [options]

Here's what each part of the command means:

  • if=<input_file>: Specifies the input file or device.
  • of=<output_file>: Specifies the output file or device.
  • [options]: Various options that can be used to customize the behavior of the dd command.

Some common options for the dd command include:

  • bs=<bytes>: Sets the block size in bytes. The default is 512 bytes.
  • count=<blocks>: Specifies the number of input blocks to copy.
  • conv=<conversion>: Performs various conversions on the input data, such as conv=notrunc to not truncate the output file.
  • status=<type>: Controls the status updates displayed during the copy process. The status=progress option shows the progress.

Example usage:

sudo dd if=/dev/sdb of=/tmp/usb_backup.img bs=4M status=progress

This command creates a backup image of the /dev/sdb device (typically a USB drive) and stores it in the /tmp/usb_backup.img file. The bs=4M option sets the block size to 4 MB, and status=progress displays the progress of the copy operation.

Example output:

1073741824 bytes (1.1 GB, 1.0 GiB) copied, 60.0926 s, 17.9 MB/s

The output shows that 1 GB of data was copied from the input device to the output file, and the copy operation took 60 seconds with a transfer rate of 17.9 MB/s.

Create a Backup Image of a USB Drive

In this step, we will create a backup image of a USB drive using the dd command.

First, let's identify the USB drive device. You can use the lsblk command to list all block devices connected to your system:

sudo lsblk

Look for the device that corresponds to your USB drive, typically named /dev/sdb or similar.

Now, let's create a backup image of the USB drive:

sudo dd if=/dev/sdb of=~/project/usb_backup.img bs=4M status=progress

This command creates a backup image file named usb_backup.img in the ~/project directory. The bs=4M option sets the block size to 4 MB, and status=progress displays the progress of the copy operation.

Example output:

4096000000 bytes (4.1 GB, 3.8 GiB) copied, 180.104 s, 22.7 MB/s

The output shows that 4.1 GB of data was copied from the USB drive to the backup image file, and the copy operation took 180 seconds with a transfer rate of 22.7 MB/s.

Restore a Backup Image to a USB Drive

In this step, we will restore the backup image of the USB drive that we created in the previous step.

First, let's identify the USB drive device again using the lsblk command:

sudo lsblk

Look for the device that corresponds to your USB drive, typically named /dev/sdb or similar.

Now, let's restore the backup image to the USB drive:

sudo dd if=~/project/usb_backup.img of=/dev/sdb bs=4M status=progress

This command restores the usb_backup.img file to the USB drive device /dev/sdb. The bs=4M option sets the block size to 4 MB, and status=progress displays the progress of the restore operation.

Example output:

4096000000 bytes (4.1 GB, 3.8 GiB) copied, 180.104 s, 22.7 MB/s

The output shows that 4.1 GB of data was copied from the backup image file to the USB drive, and the restore operation took 180 seconds with a transfer rate of 22.7 MB/s.

After the restore operation is complete, you can verify the contents of the USB drive to ensure the data has been restored correctly.

Summary

In this lab, you learned the syntax and options of the dd command in Linux, which is a powerful tool for low-level data manipulation and creating backup images of storage devices. You explored the basic command structure, including the if (input file), of (output file), and various options like bs (block size), count (number of blocks), conv (data conversions), and status (progress display). You then learned how to create a backup image of a USB drive by identifying the device and using the dd command to copy the data to an image file. Finally, you discovered how to restore the backup image to a USB drive, ensuring that the data is replicated accurately.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like