dd Command Examples on Linux
Creating a Bootable USB Drive
One of the most common uses of `dd` is to create a bootable USB drive from an ISO image.
– `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.
This copies the entire disk (`/dev/sdX`) to a single image file (`backup.img`).
To restore the image, reverse the `if` and `of` parameters:
Cloning a Partition
You can clone a specific partition using `dd`:
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:
`/dev/urandom` provides random data. This completely overwrites the disk, making data recovery nearly impossible. Alternatively, you can overwrite with zeros:
Benchmarking Disk Performance
Measure the write speed of a disk:
– `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:
– `bs=100M` specifies the chunk size (100 MB).
– `count=1` writes only one chunk. Repeat with adjusted `skip=` to create subsequent chunks:
Converting File Formats
You can use `dd` to convert files to different formats, such as uppercase text:
`conv=ucase` converts all text to uppercase.
Creating Swap Files
Create a swap file using `dd`:
This creates a 4 GB swap file. Enable it with:
Backing Up the Master Boot Record (MBR)
The first 512 bytes of a disk contain the MBR, which can be backed up using:
`bs=512` specifies the block size. `count=1` ensures only the first 512 bytes are copied.
To restore the MBR:
Fixing Bad Sectors
Attempt to read and rewrite data to fix bad sectors:
– `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.
Source: linuxhint.com