dd command in Linux with examples

Linux considers anything stored on a file system as files, even block devices. This means commands such as the dd command in Linux can be very handy in many situations, as it can be used to convert and copy files in the terminal, backup disks, or wipe data. The dd command is just as fundamental as it is useful, as it’s ready to use even on the most basic installations of Linux distros.

If you’re relatively new to Linux, the dd command might seem a little confusing as its syntax is a lot different from most other Linux commands. By default the dd command reads from the standard input and writes to the standard output. But we can invoke the if and of command line options to specify the input and output.

In this tutorial, we’ll show you with examples the best way to utilize the dd command and its various command line options.

In this tutorial you will learn:

  • How to use the dd command on Linux
dd command in Linux with examples
dd command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software dd
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

Be very careful when using dd, including in the examples below. It can easily erase an entire hard disk or partition. Don’t simply copy and paste the examples below into your Linux terminal, before realizing what they will do.

dd command in Linux Basic Examples

  1. One of the most common applications of the dd command is for backing up the hard disk. We can use the dd command with the if and of command line options to copy one hard disk to another. For example, if we had a hard disk /dev/sda and wanted to copy it to a hard disk in /dev/sdb, we would use the syntax below.
    $ dd if=/dev/sda of=/dev/sdb

    In the example above, the if option represents the “input file” and the of option represents the “output file”

  2. We can also use the dd command to back up a partition. A partition is a section where your hard drive can be divided into sections, each of these sections act as their own hard drive. For example, we can back MBR with the dd command and the if and of command line options. MBR or Master Boot Record is in the first sector of a Linux system. This means that we can use the syntax below to backup the Master Boot Record.
    $ sudo dd if=/dev/sda of=/backup/mbr.img bs=512 count=1

    In the screenshot above, we used the sudo command to obtain the required permissions to access /dev/sda. We then used the if option to specify the /dev/sda block device as input and the of option to specify the mbr.img as the output. We also used the bs and count command line options. The bs option allows us to tell dd how much data it should read at a given time. The data length of the first sector we mentioned above (where the Master Boot Record is) is 512 bytes. The count option is used to specify the number of blocks the dd command should read, which, in our example, was one.

  3. We can also use the dd command to manage conversions; we can convert all characters in a file from lower case to upper case. dd converts text to all caps by creating a new copy of the original text. For example, if we have a text file listing various commands, we can create a copy of the file and convert the data to all caps using the syntax below.
    $ dd if=commands.txt of=commands.caps conv=ucase 

    Using the dd command to convert a list of Linux commands to all caps
    Using the dd command to convert a list of Linux commands to all caps

    The syntax for converting characters in a text file to lowercase instead of uppercase is the exact same aside for the part where you have to use conv=lcase as opposed to conv=ucase, like we did above.

  4. We can also use the dd command to wipe data. A hard disk, for example, can be completely wiped with dd by overwriting the disk with null characters or zeros.
    $ sudo dd if=/dev/zero bs=5m of=/dev/sda

    The amount of time this command will take to finish will depend upon the size and type of the block device.

  5. Conversions in Linux with dd command can be used in situations where an input file does not have the same character format as your system. For example, if your Linux system uses the ASCII format for strings but the input file is set under a different format, you can use the syntax below to make the conversion.
    $ dd if=file.ebcdic of=file.ascii conv=ascii
You can always use the man command to read more about the dd command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Closing Thoughts

In this tutorial, we learned all about the dd command on Linux. The dd command is essential to master for users and administrators that frequently manage files and block devices on a Linux system.