Linux dd command explained for beginners (8 examples)

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:

dd [OPERAND]...
dd OPTION

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:

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:

How to change case of input text using dd command

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

How to read from and write to files instead

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.

How to skip text while reading input

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:

How to swap each pair of input bytes in output

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:

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?

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:

How to make sure output file is updated in append mode

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:

How to make dd not create output file

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:

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 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.

Share this page:

Suggested articles

3 Comment(s)

Add comment

Comments

From: Pete at: 2017-07-10 15:33:03

 I have **Never** used dd in the ways shown above.  Not once in 25 yrs as a Unix admin.

I use dd a few times a month, usually to make a bootable Linux flash drive for some Distro.

$ sudo dd if=/path/to/ubuntu.iso of=/dev/sdZ bs=1M

That will make a flash drive with a single distro ready to be booted with which ever type of boot the distro supports (EFI/Legacy BIOS / Both). Ubuntu supports both, BTW.

The real power in dd, is how stupid it is. It moved bytes from A ---> B.  Need to mirror a partition? Use dd at the partition level.  Need to mirror a whole disk, including all the partition tables and boot records?  Use dd at the whole disk level.

Assume sda is your boot disk with multiple partitions on it and sdZ is the target, disk that is the same or larger size of sda.

$ sudo dd if=/dev/sda of=/dev/sdZ bs=1M

That command copies, bit for bit, everything from sector 0 to the end of sda onto sdZ. You'll get any boot sector, partition tabels, and all data in all partitions.  If using GPT, the GPT partition table at the end of the disk will be in the wrong place unless it is exactly the same size. Use gdisk to fix it. The 1st GPT table will be used and the partitions and all data on them will be perfectly fine.

If you ever find yourself with dd, but without simple commands like ls, you can use dd to read a directory.  Just copy the directory file to the terminal.  That's what the ls command does too.

And don't forget that if the source disk is having issues, using ddrescue or gddrescue will continue and try to get all the input data it can, whereas dd will fail and stop at the first error.  ddrescue has slightly different command options, but it is basically the same thing. It copies bytes from A --> B too.

From: Sid at: 2017-07-11 14:54:22

eMMC and SD cards suffer damage from constant use and often need to be restored or replaced.

Yesterday I used it to backup a ODROID-C2 eMMC across the LAN from a x86_64 PC using the following command. I have used similar command to backup my other 4 ARM SBC's.

ssh -X 192.168.10.230 "dd if=/dev/mmcblk0 bs=64K | gzip -c --fast"|dd of=odroid64_eMMC.img.gz bs=64K status=progress

From: nic0teen at: 2017-07-11 21:30:05

I really like what you did there Sid