How to Use dd in Linux

The dd Linux command is use for file conversion and copying. The name dd stands for ‘Data Duplicator’ which reflects its primary function – to copy and convert raw data.

It’s known for its ability to handle tasks that other similar commands can’t perform as effectively, such as creating bootable USB drives, copying data between hard drives, or even rescuing data from damaged drives.

However, it’s important to use dd with caution. Because it operates on a low level, you can easily overwrite important data if you’re not careful.

Here are some ways to use the dd command:

1. Copy a File

The basic syntax for copying a file is dd if=inputfile of=outputfile.

dd if=input.txt of=output.txt
2. Create an ISO Image from a CD/DVD

You can use dd to create an ISO image from a CD or DVD.

dd if=/dev/cdrom of=/home/user/cdrom_image.iso
3. Create a Bootable USB Drive

If you have an ISO image of a Linux distribution, you can write it to a USB drive to create a bootable USB stick.

Replace /dev/sdb with the path to your USB drive.

dd if=/path/to/linux.iso of=/dev/sdb bs=4M
4. Backup an Entire Drive

You can use dd to create a backup of an entire drive.

dd if=/dev/sda of=/path/to/backup.img
5. Restore a Drive from a Backup Image

You can restore a drive from a backup image created with dd.

dd if=/path/to/backup.img of=/dev/sda
6. Copy a Partition

You can use dd to copy a partition to another partition.

dd if=/dev/sda1 of=/dev/sdb1
7. Create a File of a Certain Size

You can use dd to create a file of a certain size filled with zero bytes.

dd if=/dev/zero of=file.txt bs=1M count=100

This command creates a file named file.txt that is 100MB in size.

8. Securely Erase a Disk

You can use dd to securely erase a disk by overwriting it with random bytes.

dd if=/dev/urandom of=/dev/sda bs=4M

dd vs. cp in Linux

The dd and cp commands in Linux are both used for copying data, but they serve different purposes and have different capabilities.

The cp command, short for copy, is used for copying files and directories. It is typically used for routine tasks such as creating a duplicate of a file or copying files from one directory to another. It operates on the file level, reading and writing data in such a way that the file’s contents are copied.

On the other hand, the dd command operates on a lower level, directly reading and writing raw data from and to devices or files. This command is usually used for tasks such as creating and writing disk images, backing up and restoring entire partitions, or even creating bootable USB drives.

Unlike cp, dd can be used to copy data between devices of different filesystems and can copy non-regular files like directories or device files.

So, in summary, cp is a more general-purpose command for duplicating files and directories, while dd is a more specialized tool used for low-level operations involving raw data.

More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open