| by Arround The Web | No comments

dd Command Examples on Linux

The dd command, or data duplicator, is a robust and versatile utility famous for its disk manipulation features. While its primary purpose is to create disk images, it also lets you clone data, convert file formats, take backups, and more. Whether you’re a Linux beginner or an experienced user, understanding the various applications of the dd command can be beneficial. In this short tutorial, we’ll explore multiple examples of the dd command in Linux you can learn with no hassles.

Creating a Bootable USB Drive

One of the most common uses of `dd` is to create a bootable USB drive from an ISO image.

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

– `if=` specifies the input file (the ISO image).
– `of=` specifies the output file (your USB drive, e.g., `/dev/sdX`). Ensure you replace `sdX` with your USB device identifier.
– `bs=4M` sets the block size to 4 megabytes for faster copying.
– `status=progress` displays progress during the operation.
**Warning:** Double-check the device identifier (`/dev/sdX`) to avoid overwriting important data.


Creating a Disk Image

Use `dd` to create a backup image of an entire disk.

sudo dd if=/dev/sdX of=/path/to/backup.img bs=1M status=progress

This copies the entire disk (`/dev/sdX`) to a single image file (`backup.img`).
To restore the image, reverse the `if` and `of` parameters:

sudo dd if=/path/to/backup.img of=/dev/sdX bs=1M status=progress

Cloning a Partition

You can clone a specific partition using `dd`:

sudo dd if=/dev/sdX1 of=/dev/sdY1 bs=1M status=progress

Replace `/dev/sdX1` with the source partition and `/dev/sdY1` with the target partition.


Wiping a Disk

To securely erase a disk, overwrite it with random data:

sudo dd if=/dev/urandom of=/dev/sdX bs=1M status=progress

`/dev/urandom` provides random data. This completely overwrites the disk, making data recovery nearly impossible. Alternatively, you can overwrite with zeros:

sudo dd if=/dev/zero of=/dev/sdX bs=1M status=progress

Benchmarking Disk Performance

Measure the write speed of a disk:

sudo dd if=/dev/zero of=/tmp/testfile bs=1G count=1 oflag=direct

– `count=1` writes one block of 1 GB.
– `oflag=direct` bypasses system caches for accurate measurement.


Splitting a Large File

Use `dd` to split a file into smaller chunks:

dd if=largefile of=chunk_ bs=100M count=1

– `bs=100M` specifies the chunk size (100 MB).
– `count=1` writes only one chunk. Repeat with adjusted `skip=` to create subsequent chunks:

dd if=largefile of=chunk_2 bs=100M skip=1 count=1

Converting File Formats

You can use `dd` to convert files to different formats, such as uppercase text:

dd if=input.txt of=output.txt conv=ucase

`conv=ucase` converts all text to uppercase.


Creating Swap Files

Create a swap file using `dd`:

sudo dd if=/dev/zero of=/swapfile bs=1G count=4

This creates a 4 GB swap file. Enable it with:

sudo mkswap /swapfile\nsudo swapon /swapfile

Backing Up the Master Boot Record (MBR)

The first 512 bytes of a disk contain the MBR, which can be backed up using:

sudo dd if=/dev/sdX of=/path/to/mbr_backup bs=512 count=1

`bs=512` specifies the block size. `count=1` ensures only the first 512 bytes are copied.
To restore the MBR:

sudo dd if=/path/to/mbr_backup of=/dev/sdX bs=512 count=1

Fixing Bad Sectors

Attempt to read and rewrite data to fix bad sectors:

sudo dd if=/dev/sdX of=/dev/sdX bs=512 conv=noerror,sync

– `conv=noerror` skips errors instead of stopping.
– `conv=sync` pads the output with zeros in case of read errors.

Conclusion

The dd command is a powerful utility for creating, modifying, and restoring disk images. You can master it to perform disk cloning and data conversion tasks with ease. Therefore, this tutorial demonstrates multiple examples of the dd command in Linux. You can experiment the above commands to make disk images, restore disk data from images, create bootable drives, clone a disk, and much more.

Share Button

Source: linuxhint.com

Leave a Reply