dd
LINUXLPIC1-101


tl;dr
Before using dd, always unmount your FS. (Optional) Before plugging in your USB device, you can check the kernel messages with: sudo dmesg -wH (Human readable with colors)
dd if=/dev/sdX of=/dev/null status=progress # test read speed of a disk
dd if=/dev/sdX of=backup_mbr.bin bs=512 count=1 status=progress # backup of your MBR
dd if=/dev/sdX of=/dev/sdZ status=progress # cloning a disk
dd if=/dev/zero of=/dev/sdX status=progress # wiping a disk with zeros
dd if=/dev/urandom of=/dev/sdX status=progress # wiping a disk with random noise
dd if=/dev/zero of=/swapfile bs=1M count=1024 status=progress # creates a swap file of 1GB
dd if=/path/to/iso/image of=/dev/sdX status=progress # Create an ISO image
---------------------------------------------------------------------------
The dd command (often jokingly called "Disk Destroyer" because a small typo can wipe a drive) is a low-level utility used for copying and converting raw data. It works block-by-block, ignoring filesystems and focusing on the underlying bits. (Optional) Before plugging in your USB device, you can check the kernel messages with: sudo dmesg -wH (Human readable with colors). Before using dd, always unmount your FS.
1. The Command: dd if=/dev/sdX of=/dev/null
This specific command is essentially a read-speed test for your hard drive.
if=/dev/sdX: Input File. It reads the raw data from your first physical hard drive (sdX).
of=/dev/null: Output File. It sends all that data to the "bit bucket"—a virtual device that discards everything sent to it.
The result: You aren't saving any data, but when the command finishes, dd will report how much data was processed and the average transfer speed. It tells you how fast your disk can read data.
2. Backing up the MBR (Master Boot Record)
The MBR sits in the first 512 bytes of a disk. If it gets corrupted, the system won't boot.
dd if=/dev/sdX of=mbr_backup.bin bs=512 count=1
bs=512: Set the Block Size to 512 bytes.
count=1: Only copy one block.
3. Cloning a Whole Drive*
You can mirror an entire drive to another identical drive.
dd if=/dev/sdX of=/dev/sdZ
Warning: This will overwrite everything on /dev/sdZ! The author is not responsible for any damages on the system.
4. Wiping a Disk (Security)
If you are throwing away a computer, you can fill the drive with zeros or random data so the old files can't be recovered.
dd if=/dev/zero of=/dev/sdX # Fills with zeros
dd if=/dev/urandom of=/dev/sdX # Fills with random noise
5. Creating a Swap File
If you run out of RAM and need more swap space quickly, you can use dd to create a large empty file.
dd if=/dev/zero of=/swapfile bs=1M count=1024 # Creates a 1GB file
Flags to Memorize
Flag Meaning
if= Input source (file or device).
of= Output destination.
bs= Block Size (e.g., 512, 1M, 4k). Crucial for speed performance.
count= Number of blocks to copy.
status=progress Shows a live progress bar (very helpful for large disks).
Notes:
* Cloning a whole drive: after cloning an operating system, you need to consider a few things to make it work so your settings don't collide on the network. Take a look at this checklist.
Practical examples
1. Wiping a USB drive
Important: determine what is the drive you want to wipe. dd has no undo options!
lsblk, df and mount commands can help you with this.
Then, unmount the partition:
umount /dev/sdXY
Wipe the USB drive:
sudo dd if=/dev/zero of=/dev/sdX bs=4M status=progress
Can you use other values for bs?
Absolutely. Common choices are bs=1M, bs=4M, bs=8M. The sweet spot depends on the hardware, but 4M is a widely used practical default that works well for USB drives and disk imaging.
In my case I got a "No space left on device" message from the "dd" command. I tried Ctrl+c but it didn't work. Ctrl+z seemed to work but it took a minute. Then I had to kill the "dd" process with a "kill -9", since "kill" on itself didn't work.
ps aux | grep -w dd
Now back at the USB drive, create a new partition table with fdisk:
sudo fdisk /dev/sdX
Options: o (to create a MBR/FAT partition table), n (new), p (primary), 1 (partition number), Enter (default values for sector), Enter (default values for sector again), w (write an exit).
Verify the partition has been created:
lsblk
Format the partition:
sudo mkfs.vfat /dev/sdXY
sudo fatlabel /dev/sdXY "MY LABEL"
Unplug the USB drive and plug it again so it is auto-mounted.
2. Creating an ISO image
dd if=/path/to/iso/image of=/dev/sdX status=progress
3. Creating a disk for Timeshift backups
sudo dd if=/dev/zero of=/dev/sdX status=progress # Fill the drive with zeros
sudo fdisk /dev/sdX # Create the partition
(GPT format "g", press "n" for new partition, press Enter for default values, "w" to write and exit)
mkfs.ext4 /dev/sdXY # Format the partition with a Linux FS
tune2fs -L "TIMESHIFT BACKUP" /dev/sdXY # Create a new label for the device
(unplug and plug it again so the system mounts it automatically)
Contact
hello@unixtips.eu
© 2025. All rights reserved.
NEW! Join the community at https://www.skool.com/linux-and-unix-command-line-6976/about