Skip to main content

Commands

dd command Examples

Here are various practical examples of using the dd command on Ubuntu.

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.

Sounds interesting?

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

⚠️
Make sure to practice under a testing environment as it has the potential to damage your data.

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 ext4:

sudo mkfs.ext4 /path/to/drive
flash drive in specific filesystem in Linux

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 sdb1 drive:

sudo dd if=ubuntu-server.iso of=/dev/sdb1
burn ISO over USB drive in Ubuntu terminal using the dd command

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 backup.img:

sudo dd if=/dev/vda2 of=backup.img
Create a copy of disk drive using dd command in ubuntu

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 /dev/sdb to /dev/sda:

sudo dd if=/dev/sdb of=/dev/sda
use dd command to create clone of disk in Ubuntu

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

Here, the bs=1M sets one block size equals to 1 megabyte.

For your reference, here, I erased my external drive sdb1:

sudo dd if=/dev/urandom of=/dev/sdb1 bs=1M
securely wipe out disk in Ubuntu using the dd command

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
create a file of specific filesize in Linux using the dd command

That's it!

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:

What is /dev/null in Linux?
/dev/null is the blackhole equivalent of Linux systems. What is it and why it used?

I hope you will find this guide helpful.