The dd command is a powerful utility used for low-level data copying and conversion on Linux systems. It stands for “data duplicator” and is often employed to create exact copies of files, partitions, or entire disks, making it useful for tasks like creating backups, cloning drives, or writing disk images to physical media. The command can copy and convert data between files or devices, with options to control block size, skip or seek data, and even apply filters like compression or byte swapping. Because of its ability to work at such a fundamental level, dd must be used with caution, as incorrect usage can result in data loss or corruption.
In this tutorial, we will discuss how the tool works using some easy-to-understand examples. But before we do that, it’s worth mentioning that all examples mentioned here have been tested on Ubuntu 24.04.
Linux dd command
In short, the dd command lets you copy and convert a file. The tool offers some operands that you can use to specify what kind of formatting you want. Here’s the generic syntax of the command as described on its man page:
dd [OPERAND]...
dd OPTION
Create a bootable USB drive from ISO file using dd command
A common use of the dd command in Linux is to create a bootable USB drive from an ISO file. This is often done when preparing a USB stick to install an operating system. The command would look something like this:
sudo dd if=/path/to/your.iso of=/dev/sdX bs=4M status=progress && sync
In this example:
- if=/path/to/your.iso specifies the input file, which is the ISO image.
- of=/dev/sdX specifies the output file, which is the USB drive (replace X with the appropriate letter for your USB device).
- bs=4M sets the block size to 4 megabytes, optimizing the copying process.
- status=progress provides real-time updates on the progress of copying.
- sync ensures that all data is written to the USB before the process is completed.
This command will create a bootable USB drive that can be used to install the operating system contained in the ISO file.
Frequently Asked Questions
The following Q&A-type examples should give you a good idea about how the dd command works.
Q1. How to change the case of input text using dd command?
Suppose you have some lines of text written in lower-case, and the requirement is to convert them to upper-case quickly. You can use the conv command line option (with ucase as its value).
Here’s an example:
The above screenshot shows the command as well as the input text. The key combination Ctrl+d was used to tell dd that we’re done entering the input, and as soon as that was done, the command produced the following output:
So you can see that the input text was converted from lower-case to upper-case. The last three lines are just some operation-related statistics.
Note: Similarly, you can use the value lcase to convert upper-case text into lower-case.
Q2. How to read from and write to files instead?
We entered the text through stdin (standard input) in the first example above. However, you can always use input and output files. To specify the names of input and output files, use the if and of command-line options.
For example, if you want to convert the case of text in file1 and then have it written in file2, then you can do this in the following way:
dd if=file1 of=file2 conv=ucase
Q3. How to skip text while reading input?
If you want, you can ask dd to skip some initial bytes while reading input text. This can be done using the skip option, which requires a numerical value. If, say, this value is ‘N’, then dd will skip N ibs-sized blocks at the start of input. This brings us to ‘ibs’, another command-line dd option whose value specifies the number of bytes the tool reads at a time (the default is 512).
So, suppose you want to skip the first 4 bytes of an input file. You should first set it to 4 and then use 1 as the skip value. This will ensure that the tool skips 1 block of 4 bytes while reading the input file.
So you can see that the text hey, was skipped while reading file1 – that’s why it didn’t appear in file2.
Q4. How to swap each pair of input bytes in output?
You can also use dd to swap every pair of input bytes if there’s such a requirement. This can be achieved by using swab as value for the conv command line option. The following screenshot will make things more clear:
Q5. How to make dd only work when the output file doesn’t already exist?
If the requirement is that the dd command should not do anything when the output file already exists, then the excl value for the conv option can enforce this condition.
The following screenshot shows the use-case in action:
Q6. How to make sure the output file is updated in append mode?
By default, dd just overwrites the output file (if that exists). But if you want it to append the text instead, use the value append for oflag FLAG and notrunc for conv option.
For example:
dd if=file1 of=file3 oflag=append conv=notrunc
Here’s the above command in action:
Q7. How to make dd not create output file?
If you want, you can also force the dd command to not create an output file if the file given in the command doesn’t already exist. This can be done by using the nocreat value of the conv command line option.
For example:
Q8. How to control the level of information printed on stderr?
If you want, you can also limit the amount of information the command prints on stderr. This can be done using the status command-line option. For example, if you want to suppress everything but error messages, you can pass the value none to this command-line option:
dd if=file1 of=file3 status=none
Other available values are noxfer and progress. Here’s what the man page says about the status option:
status=LEVEL
The LEVEL of information to print to stderr; 'none' suppresses
everything but error messages, 'noxfer' suppresses the final
transfer statistics, 'progress' shows periodic transfer statis?
tics
Conclusion
The examples discussed in this tutorial just scratch the surface when exploring what all the dd command can do. Just to give you an idea, you can use the tool to create virtual filesystems and backups of hard drives or system partitions. For more information on dd, head to its man page.