Linux dd Command to Copy Files

Linux dd Command to Copy Files

The dd command in Linux is a powerful tool for low-level disk operations, often used for tasks like cloning disks, creating images, recovering data, and copying files.

Linux dd command is particularly useful when you need precise control over the data transfer process, such as:

  • Specifying block sizes.
  • Limiting the number of blocks to be copied.
  • Applying data conversions.

Whether you’re a system administrator, developer, or simply a Linux enthusiast, mastering the Linux dd command can be a valuable asset in your toolkit.

  • Linux dd Command Basic Syntax:
dd if=input_file of=output_file [options]
  • if=input_file: Specifies the input file (source).
  • of=output_file: Specifies the output file (destination).

Prerequisites to Use dd Command in Linux Ubuntu, CentOS, etc

Provide the options below to let this tutorial work correctly and move on.

  • A Linux VPS running Ubuntu, CentOS, and etc.
  • A non-root user with sudo privileges.
  • Access to Command line/Terminal.

15 Practical Examples to Understand Linux dd Command

Linux dd command examples would be helpful for both beginners and professional Linux users since incorrect usage of the Linux dd command can lead to data loss or corruption.

Once you purchased your Linux VPS, you are ready to go through this tutorial and learn How to Use dd Command in Linux.

1. Backing Up a Partition Using dd Command

When creating a backup of a partition using dd command in Linux, you’re essentially creating a bit-for-bit copy of the partition’s data.

This can be useful for system recovery, data preservation, or migration purposes.

  • Syntax:
dd if=/dev/sdX of=partition_backup.img
  • if=/dev/sdX: Replaces sdX with the actual device name of your partition (e.g., /dev/sda1 for the first partition on the first disk).
  • of=partition_backup.img: Specifies the name of the output file (the backup image).
  • Example:

To create a backup of the first partition on the first disk using the Linux dd command, run:

dd if=/dev/sda1 of=sda1_backup.img

2. Managing Conversions Using dd Command

The dd command can be used to perform various data manipulation tasks, including conversions. By leveraging its conv option, you can apply different transformations to the data being copied.

  • Syntax:
dd if=input_file of=output_file conv=option1,option2,...
  • if=input_file: Specifies the input file.
  • of=output_file: Specifies the output file.
  • conv=option1,option2,...: Specifies one or more conversion options.
  • Common Conversion Options:
  • ucase: Converts input to uppercase.
  • lcase: Converts input to lowercase.
  • swab: Swaps bytes within each word.
  • notrunc: Prevents truncation of the output file.
  • sync: Ensures synchronous writes.
  • sparse: Creates sparse files (with allocated space only for non-zero data).
  • fdatasync: Flushes file data but not metadata.
  • Examples:

To convert a File to Uppercasem, run:

dd if=input_file of=output_file conv=ucase

This command will copy the contents of input_file to output_file, converting all characters to uppercase.

To convert a file to lowercase, run:

dd if=input_file of=output_file conv=lcase

To swap bytes within each word, run:

dd if=input_file of=output_file conv=swab

To create a sparse file, run:

dd if=input_file of=output_file conv=sparse

3. Wiping Data Using Linux dd Command

To securely wipe data from a disk or partitions, you can use the Linux dd command to overwrite the data with random or specific patterns. This can help prevent data recovery attempts.

  • Example

To wipe the first partition on the first disk with random data, run:

dd if=/dev/urandom of=/dev/sdX bs=1M count=1000
  • /dev/urandom: Provides a source of random data.
  • /dev/sdX: Replaces sdX with the actual device name of the disk or partition you want to wipe.
  • bs=1M: Sets the block size to 1 megabyte.
  • count=1000: Overwrites 1000 megabytes (1 gigabyte) of data.

4. Handling Input Files with Different Character Formats Using dd Command

When working with input files that have a different character format than your system, the dd command in Linux can be used to perform conversions or handle the data appropriately.

  • Syntax:
dd if=input_file of=output_file conv=option1,option2,...
  • if=input_file: Specifies the input file.
  • of=output_file: Specifies the output file.
  • conv=option1,option2,...: Specifies one or more conversion options.
  • Conversion Options for Character Formats:
  • ucase: Converts input to uppercase.
  • lcase: Converts input to lowercase.
  • swab: Swaps bytes within each word (useful for endianness conversions).
  • fconv=CODE: Converts the file to a specific character encoding (e.g., fconv=ucs2 for UTF-16).
  • Example:

To convert a UTF-16 File to UTF-8, run:

dd if=input_file.utf16 of=output_file.utf8 conv=fconv=utf8

5. Creating a Virtual Filesystem Using dd Command

Virtual Filesystems are useful for creating temporary storage spaces or testing new filesystem features.

  • To create a raw disk image, run:
dd if=/dev/zero of=vfs_image.img bs=1M count=100

This creates a 100MB raw disk image.

  • To attach the image to a loop device, run:
losetup -f vfs_image.img

This assigns a loop device (e.g., /dev/loop0) to the image.

  • To create a filesystem on the loop device, run:
mkfs.ext4 /dev/loop0

This creates an Ext4 filesystem on the loop device.

  • Mounting the VFS:

Once the filesystem is created, you can mount it to a directory:

mkdir /mnt/vfs
mount /dev/loop0 /mnt/vfs

Now, you can access the virtual filesystem through the /mnt/vfs directory.

  • Unmounting the VFS:

When you’re finished using the VFS, unmount it:

umount /mnt/vfs
  • Detaching the Loop Device:

To detach the loop device from the image, run:

losetup -d /dev/loop0

6. Creating an ISO Image from a CD/DVD Using dd Command

Creating an ISO image allows you to back up or distribute the contents of a physical disc.

Before using dd to create an ISO Image, consider:

  • Insert the CD/DVD into your drive: Ensure it is properly mounted.
  • Identify the device name: Use the df command to find the device name (e.g., /dev/sr0).
  • To create the ISO image, run:
dd if=/dev/sr0 of=cd_image.iso

It creates an ISO image of the CD/DVD inserted in the first drive.

7. Creating a Bootable USB Drive Using dd Command

A bootable USB drive is often used for installing operating systems or performing system recovery tasks.

Before using dd to create a Bootable USB Drive, consider:

  • Obtain a bootable ISO image: Download the ISO image of the operating system or distribution you want to install.
  • Insert the USB drive: Ensure it’s properly mounted and formatted. If you need more guidance, refer to the Linux Mount USB Drive tutorial.
  • Identify the device name: Use lsblk or df to find the device name (e.g., /dev/sdb).
  • To create the bootable USB, run:
sudo dd if=operating_system.iso of=/dev/sdb bs=4M

It creates a bootable USB drive from a downloaded Ubuntu ISO image.

8. Creating a Disk Image Using Linux dd Command

A disk image is a file that contains an exact copy of the contents of a disk or partition. It can be used for various purposes, including backups, system migration, and data recovery.

You can use the Linux dd command to create disk images:

  • Syntax:
dd if=/dev/sdX of=disk_image.img
  • if=/dev/sdX: Specifies the input device (e.g., /dev/sda1 for the first partition on the first disk).
  • of=disk_image.img: Specifies the name of the output file (the disk image).
  • Example:

To create a disk image of the first partition on the first disk, run:

dd if=/dev/sda1 of=disk_image.img

9. Copying a File Image Using dd Command

A file image is a complete copy of a file, including its metadata and contents. It can be used for backup, archiving, or analysis purposes.

dd command typically preserves the metadata of the file image, including permissions, ownership, and timestamps.

Linux dd command can be used to Copy a File Image.

  • Syntax:
dd if=input_file.img of=output_file.img
  • Examples:

To create a copy of a file image named file.img, run:

dd if=file.img of=file_copy.img

To copy only the first 100 megabytes of a file image, run:

dd if=large_file.img of=small_file.img bs=1M count=100

10. Restoring a Disk Image Using dd Command

To restore a disk image created using the Linux dd command, you’ll essentially be reversing the process of creating the image.

This involves writing the contents of the image back to the target disk.

  • Syntax:
dd if=disk_image.img of=/dev/sdX
  • if=disk_image.img: Specifies the path to the disk image file.
  • of=/dev/sdX: Specifies the target device (e.g., /dev/sda1 for the first partition on the first disk).
  • Example:

To restore a disk image named disk_image.img to the first partition on the first disk, run:

dd if=disk_image.img of=/dev/sda1

Note: Restoring a disk image will overwrite the existing data on the target device. Make sure you have a backup of any important data before proceeding.

11. Compressing Disk Images Using dd Command

While dd itself doesn’t provide direct compression capabilities, you can combine it with compression tools like gzip to create compressed disk images.

This can significantly reduce the size of your backups.

  • To compress the image using gzip, run:
dd if=/dev/sda1 | gzip > disk_image.gz

12. Restoring a Compressed Disk Image Using dd Command

To restore a compressed disk image created using dd and gzip, you’ll need to decompress the image first and then use dd to write the contents back to the target device.

  • To Decompress the image, run:
gzip -d disk_image.gz.gz

This will create a decompressed file named disk_image.img.

  • To Restore the image, run:
dd if=disk_image.img of=/dev/sdX

Keep in mind to replace /dev/sdX with the actual device name of the target partition.

  • Also, you can combine these steps into a single command using a pipe:
gzip -d disk_image.gz | dd of=/dev/sdX

13. Recovering Data Using dd Command

If a partition has been damaged, dd can sometimes be used to extract recoverable data.

Also, in some cases, deleted files might still be recoverable if they haven’t been overwritten by new data. Linux dd command can be used to directly copy data from the affected sector.

  • Recovering Damaged Partitions:

For example, to skip the first 10,000 blocks and attempt to recover data from the rest of the partition /dev/sda1, run:

dd if=/dev/sda1 of=recovered_data.img skip=10000
  • Recovering Deleted Files:

To recover the first 10,000 blocks (512 bytes each) from /dev/sda1, which might contain deleted files, run:

dd if=/dev/sda1 of=recovered_files.img bs=512 count=10000

14. Cloning Disk Using dd Command

Disk cloning creates an exact copy of a disk, including its partition table, file system, and data.

This is useful for tasks like system migration, disaster recovery, or creating backups.

  • Examples:

To clone the entire first disk to a file named disk_clone.img:

dd if=/dev/sda of=disk_clone.img

To clone only the first 100 megabytes of the disk /dev/sda, run:

dd if=/dev/sda of=disk_clone.img bs=1M count=100

15. Testing Performance Using dd Command

Performance testing involves measuring the speed and efficiency of various system components, including disk I/O.

The dd command in Linux can be used to generate controlled workloads and assess disk performance.

  • Examples:

To use dd for Performance Testing, run the command below which writes 1GB of data to /dev/null and report the elapsed time:

dd if=/dev/zero of=/dev/null bs=1M count=1000
  • /dev/zero: Provides a source of zero bytes.
  • /dev/null: Discards the output data.
  • bs=1M: Sets the block size to 1 megabyte.
  • count=1000: Specifies the number of blocks to transfer.

For measuring random read performance:

dd if=/dev/zero of=testfile bs=4k count=10000 seek=random

This command reads 10,000 4KB blocks from /dev/zero and writes them to testfile, simulating random reads.

That’s it! By understanding Linux dd command usage, syntax, options, and capabilities, you can effectively leverage dd to perform various tasks efficiently.

Linux dd Command Options

OptionDescriptionExample
if=fileSpecifies the input file.dd if=input.txt
of=fileSpecifies the output file.dd of=output.txt
bs=bytesSets the block size.dd bs=1024
count=blocksLimits the number of blocks to be copied.dd count=100
skip=blocksSkips the specified number of blocks from the input file.dd skip=100
seek=blocksSkips the specified number of blocks in the output file.dd seek=100
conv=optionApplies conversion options.dd conv=ucase (converts to uppercase)
iflag=flagSets input flags (e.g., fullblock for full-block reads).dd iflag=fullblock
oflag=flagSets output flags (e.g., sync for synchronous writes).dd oflag=sync
status=optionControls progress reporting (e.g., progress for progress bar).dd status=progress

 

How to Combine dd Command with other tools in Linux Ubuntu, CentOS, …?

The Linux dd command can be effectively combined with other Linux tools to create more complex and tailored workflows.

Combine dd Command with other tools in Linux

Here are some common examples:

Filtering

Combining dd with grep command:

dd if=input_file.txt | grep "pattern"

This command extracts data from input_file.txt using dd and then filters the output for lines containing the specified pattern using grep.

Extracting Data

Combining dd with head and tail command:

dd if=large_file.txt | head -n 10
dd if=large_file.txt | tail -n 10

These commands extract the first 10 lines or the last 10 lines of large_file.txt, respectively.

Compression

Combining dd with gzip or bzip2:

dd if=/dev/sda1 | gzip > disk_image.gz

This command creates a disk image of /dev/sda1 and compresses it using gzip.

Verification

Combining dd with md5sum:

dd if=input_file.txt | md5sum > checksum.txt

This command calculates the MD5 checksum of input_file.txt and saves it to checksum.txt.

Splitting Output

Combining dd with tee command:

dd if=input_file.txt | tee output1.txt output2.txt

This command splits the output of dd into two files: output1.txt and output2.txt.

Progress Monitoring

Combining dd with pv:

dd if=/dev/sda1 of=disk_image.img | pv -s $(stat -f %z /dev/sda1)

This command creates a disk image and displays progress information using the pv tool.

Data Manipulation

Combining dd with awk command:

dd if=input_file.txt | awk '{ print $1, $3 }'

This command extracts the first and third fields from each line of input_file.txt using awk.

When to use dd command and when not to use it in Linux?

It is important to understand the Linux dd command’s strengths and limitations to use it effectively.

When to use dd Command in Linux:

Cloning disks: Creating an exact copy of a disk is a common use case for dd. This is useful for system migration, backup, or disaster recovery.

Creating disk images: Linux dd command can be used to create disk images, which are essentially snapshots of a disk or partition. These images can be used for backup, transportation, or analysis.

Recovering data: In some cases, dd command can be used to recover data from damaged or corrupted disks, although it’s not always guaranteed to be successful.

Performing data conversions: The Linux dd command can be used to convert data between different formats or encodings.

Testing disk performance: dd command in Linux can be used to generate controlled I/O workloads to measure disk performance.

When Not to use dd Command in Linux:

Simple file copying: For most file copying tasks, the cp command is more efficient and user-friendly.

High-level file operations: The Linux dd command is not designed for complex file operations like editing, renaming, or deleting files.

Data manipulation: For complex data manipulation tasks, consider using tools like sed, awk, or scripting languages.

Why is my dd command taking so long?

To troubleshoot this situation, consider the below conditions:

  • Large Files: Copying large files takes time.
  • Slow Disk: A slow disk will affect the transfer speed.
  • Block Size: Experiment with different block sizes to optimize performance.

What are common errors encountered with dd and How to troubleshoot them?

  1. Device not found: Check the device name.
  2. Permission denied: Ensure you have appropriate permissions.
  3. I/O error: Verify the disk is healthy and accessible.
  4. Conversion errors: Double-check the conversion options.

Conclusion

The dd command in Linux is a versatile tool with a wide range of applications, from disk cloning and data recovery to file conversions and performance testing.

The examples provided in this guide demonstrate the diverse use cases of dd, including backing up partitions, managing conversions, wiping data, handling different character formats, creating virtual filesystems, and more.

By combining Linux dd command with other tools and techniques, you can create customized workflows to suit your specific needs.

Leave a Reply

Your email address will not be published. Required fields are marked.