Home > Package & System Management > dd

dd: File Conversion and Copying

The dd command is a powerful utility used for converting and copying files. It is primarily used for low-level data manipulation such as creating disk images, backing up and restoring partitions, creating bootable USB drives, and zeroing out file contents. Extreme caution is advised as incorrect usage can lead to data loss.

Overview

dd is known as an abbreviation for 'disk dump' or 'data duplicator' and operates by reading and writing data in block units. It reads data from an input file (if) and copies it to an output file (of), allowing for various conversions (conv) and block size (bs) options to be applied during the process.

Key Features

  • Disk/partition image creation and restoration
  • Creation of bootable media
  • File resizing and content initialization
  • Data conversion (e.g., case conversion)

Key Options

The dd command allows for sophisticated data manipulation through various options. Specifying the input (if) and output (of) files is particularly important, and the block size (bs) and number of blocks to copy (count) control the scope of the operation.

Input/Output Specification

Block Size and Count

Conversions and Status

Generated command:

Try combining the commands.

Description:

`dd` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

The dd command is powerful, but incorrect usage can lead to severe data loss. The examples below demonstrate common use cases, but when applying them to actual system disks, always perform backups and proceed with caution.

Create a 100MB Dummy File

dd if=/dev/zero of=dummy.img bs=1M count=100

Reads 100 blocks of 1MB from /dev/zero to create a file named 'dummy.img'.

Backup Disk Partition (Requires Caution)

dd if=/dev/sda1 of=sda1_backup.img bs=4M status=progress

Backs up the entire content of the /dev/sda1 partition to a file named 'sda1_backup.img'. 'status=progress' displays the progress. **Verify the target device path very carefully.**

Overwrite File Content with Zeros (Secure Deletion)

dd if=/dev/zero of=myfile.txt bs=1M count=1 conv=notrunc

Overwrites the content of the existing 'myfile.txt' file with zeros. 'conv=notrunc' preserves the file size.

Create a Bootable USB Drive from an ISO Image (Extreme Caution)

dd if=ubuntu.iso of=/dev/sdb bs=4M status=progress conv=fsync

Copies the 'ubuntu.iso' file to the '/dev/sdb' USB drive to make it bootable. **Ensure the device path specified in 'of' is correct. Incorrect specification can damage system disks.** 'conv=fsync' ensures all data is physically written to disk.

Copy a Specific Part of a File

dd if=input.txt of=output.txt bs=1 count=100 skip=50 seek=0

Reads 100 bytes starting from the 51st byte of 'input.txt' and copies them to the beginning of 'output.txt'. (Since bs=1, skip/seek are byte-wise)

Tips & Precautions

The dd command is very powerful, so extreme caution is necessary when using it. Specifying incorrect devices or files for the `if` and `of` options can lead to system data corruption.

Key Precautions

  • **Verify Destinations**: Double-check the paths specified for `if` and `of`. Device files like `/dev/sda` can be particularly critical.
  • **Monitor Progress**: Use the `status=progress` option to monitor the operation's progress in real-time. This is useful for large operations.
  • **Block Size**: The `bs` option significantly affects performance. Generally, 1M, 4M, or 8M are efficient.
  • **Risk of Data Loss**: `dd` can overwrite data without warning. Always back up important data before using it on disks or partitions containing critical information.

Using with pv Command

The `dd` command itself has limited progress display. Using it with the `pv` (Pipe Viewer) command allows for more detailed progress, transfer speed, and estimated completion time. If `pv` is not installed, you need to install it first. (e.g., `sudo apt install pv` or `sudo yum install pv`) **Usage Example:** `dd if=/dev/sda | pv -pteb | dd of=/dev/sdb`


Same category commands