5 Useful Unix DD Command Examples
dd is a common Unix program whose primary purpose is the low-level copying and conversion of raw data. You can backup whole hard drives, create a large file filled with only zeros, create and modify image files at specific points, and even do conversions to upper case.
Alright, lets get to the juicy stuff.
1. Make an ISO of a your favourite CD just for backing up purposes with dd:
dd if=/dev/cdrom of=/home/erik/myCD.iso bs=2048 conv=sync
Breaking down the commands:
- if is “input file”, so in this case our cdrom drive at /dev/cdrom
- of is “output file”, in this case myCD.iso
- bs is “block size”, in this case 2048 bytes per block
- conv is for conversion, in this case we are using “sync” which tells DD to execute synchronized input and output, this is needed for the CD-ROM as we want to read a whole block to ensure no data loss occurs.
2. Duplicate one hard disk partition to another hard disk with dd:
dd if=/dev/sda1 of=/dev/sdb1 bs=4096 conv=noerror
In this case everything is the same as example 1 but our conversion methods states that noerror should be executed, this tells DD to continue after read errors.
3. Fill a file with 1MB of random bytes with dd:
erik@debian:~$dd if=/dev/urandom bs=1024 count=1000 of=fun.bin
1000+0 records in
1000+0 records out
1024000 bytes (1.0 MB) copied, 0.198349 s, 5.2 MB/s
This time I stated that our block size is 1024 bytes, and we are going to make 1000 of them sequentially. I also used the built-in kernel device urandom which provides random bytes.
4. Skip first 128K of input file then write remaining with dd:
dd if=/home/erik/fun.bin skip=128k bs=1 of=/home/erik/fun2.bin
The skip command tells DD to move passed the (in this case) 128k of data infun.bin then write the rest to fun2.bin. This can be handy if you have a large file that needs to be written across more than one partition. For instance, if you had 3 partitions each 128k. You wouldn’t want to write the same 128k to each partition, you would want to write the first 128k to partition 1, then from 128k-256k of the file to partition 2 and so on.
5. Using dd to convert a file to uppercase:
dd if=erik.txt of=erik_up.txt conv=ucase
Finally, we use conv again to do a conversion. In this case we convert with the specifier of ucase.
What is your favourite use of dd?
Erik is an avid Linux user and has experience with a wide variety of Linux/Unix based systems. In his spare time he likes writing software (C/C++/Perl/PHP/BASH/Python) and experimenting with different Linux distributions. He is starting to build a wide variety of articles Unix driven (what a surprise) that hope to inform those just getting into Linux on his website.