The primary function of the dd command is to copy and convert files in Linux.
But for that purpose, didn't we already have a cp command?
Yes, the cp command is still there but the dd command works at a low level and gives you various advantages over the cp command.
Disk cloning, customizing a disk block, and creating disk images is one of the few use cases of the dd command.
Let's have a look at how to use the dd command in Ubuntu with practical examples.
How to use the dd command in Ubuntu
To use the dd command, you'd have to follow a simple syntax:
dd if=Input_File of=Output_File [options]
Looks too complex? Let me break it down for you.
if=Input_File: Specifies the input file or device.
of=Output_File: Here, you specify the output file or device.
[options]: Using options, you can modify the default behavior of the dd command.
Now, let's have a look at some examples.
1. Create a bootable drive using the dd command
By far, this is the most famous use case of the dd command.
The reason is simple. You don't have to use other tools like WoeUSB or Ventoy to create a bootable drive.
First, unmount the drive (if mounted):
sudo unmount /path/to/drive
Now, it's time to flash the drive in desired filesystem type.
If used without any options, it will flash the drive in ext2 I assume that you want to create a bootable drive for Linux so use
sudo mkfs.ext4 /path/to/drive
And finally, use the dd command to burn the ISO file over the disk:
sudo dd if=File.iso of=/path/to/drive
For example, here, I burned the Ubuntu server on the
sudo dd if=ubuntu-server.iso of=/dev/sdb1
There you have it!
2. Create a disk image using the dd command
There are times when you want to create a backup for a specific disk drive and guess what? dd is known for such ability.
To create a disk image, all you have to do is specify the path to the disk and target as shown here:
sudo dd if=/path/to/device of=backup.img
Yes, you need superuser privileges to create a copy of a disk drive.
For example, here, I created a copy of
/dev/vda2 and named it
sudo dd if=/dev/vda2 of=backup.img
3. Create a clone of a disk drive using the dd command
If you want to clone one disk to another, the dd command can make things super easy for you.
Just make sure the drive which is about to store the clone drive has enough space.
Here's the syntax to clone the disk drive using the dd command:
sudo dd if=/target-device of=/where/to/clone
For example, here, I cloned the
sudo dd if=/dev/sdb of=/dev/sda
4. Erase data securely using the dd command
Before passing your drive to your friend, you must make sure the data you removed can not be traced back.
And for that purpose, you're advised to write random data, multiple times on that drive so even if your friend tries to recover your data, the only thing he gets is trash!
To do so, you can use the urandom to generate random data in Linux:
dd if=/dev/urandom of=/path/to/drive bs=1M
bs=1M sets one block size equals to 1 megabyte.
For your reference, here, I erased my external drive
sudo dd if=/dev/urandom of=/dev/sdb1 bs=1M
You may ignore the error saying "No space left on this device".
5. Create a file with a specific file size using the dd command
Generally, when you create empty files, you don't get any options to create an empty file with a specific size.
But using the dd command, you can do that too.
For that purpose, I will be using the/dev/zero to create a file with a specific file size in the following manner:
dd if=/dev/zero of=large_file.txt bs=Block_Size count=sets_of_blocksize
So let's say you want to create a file worth 169MB, then you can use 1MB of block size 169 times.
Sounds too complex? Here's how to do it:
dd if=/dev/zero of=Large.txt bs=1MB count=169
Next: What is /dev/null in Linux?
Did you know that there's a black hole in the Linux filesystem?
Yes, I'm talking about /dev/null here. Whatever is sent there can not be traced back!
Sounds interesting? Here's the detailed guide:
I hope you will find this guide helpful.