Linux dd command explained for beginners (8 examples)
On this page
- Linux dd command
- Q1. How to change case of input text using dd command?
- Q2. How to read from and write to files instead?
- Q3. How to skip text while reading input?
- Q4. How to swap each pair of input bytes in output?
- Q5. How to make dd only work when output file doesn't already exists?
- Q6. How to make sure output file is updated in append mode?
- Q7. How to make dd not create output file?
- Q8. How to control level of information printed on stderr?
Sometimes, while working on the command line in Linux, you may need to perform a copy operation in way that the data/text gets formatted before it's written at the destination. A simple example could be to copy text from a file and write the case-changed version (lower to upper, or upper to lower) to the destination file.
If you've been looking for way to do this, you'll be glad to know there exists a command line utility - dubbed dd - that's built for this purpose. 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 16.04LTS.
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:
The following Q&A-type examples should give you a good idea about how the dd command works.
Q1. How to change case of input text using dd command?
Suppose you have some lines of text that are written in lower-case, and the requirement is to quickly convert them to upper-case. You can do that using 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?
In the first example above, we entered the text through stdin (standard input). However, you can always use input and output files. To specify 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 start of input. This brings us to 'ibs', which also another command line dd option whose value specifies the number of bytes the tool reads at a time (default is 512).
So suppose, you want to skip first 4 bytes of input file, then you should first set ibs to 4 and then use 1 as the value for skip. This will ensure that the tool will skip 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?
If there's such a requirement, you can also use dd to swap every pair of input bytes. 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 output file doesn't already exists?
If the requirement is that the dd command shouldn't do anything when the output file already exists, then this condition can be enforced using the excl value for the conv option.
The following screenshot shows the use-case in action:
Q6. How to make sure 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.
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.
Q8. How to control 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:
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?
The examples discussed in this tutorial just scratch the surface when it comes to exploring what all the dd command can do. Just to give you an idea, you can use the tool to create virtual filesystems as well as backups of hard drives or system partitions. For more information on dd, head to its man page.