How to Convert and copy a File with dd command in Linux
In Linux, if we want to convert and copy a file, we use the dd (data d)
command.
The dd
(data duplicator) called dd because cc
is already in use by the C compiler. The dd
is a command-line utility for Linux/Unix operating systems. This command is mainly used for copying and converting data so it is also called data duplicator.
On Linux/Unix, device drivers for hardware and special device files appear in the file system just like normal files, dd
can read write these files. This tool is used for:
- If we want to backup and restore the entire hard disk or portion.
- Conversions of the data as it is copied, including byte order swapping and
dd
command is also used to convert from ASCII to EBCDIC.
The general syntax of dd command :
dd if=<source file name> of=<target file name> [Options]
if
= <source>
- 'if ' stands for input file and source from where we want to copy data.
of
= <source>
- 'of ' stands for the output file and destination where we want to write or paste data.
[option]
- This option defines what is the format of the file and its transferring speed.
dd command manual / description
Options |
Description |
bs=BYTES |
read and write BYTES bytes at a time |
cbs=BYTES |
convert BYTES bytes at a time |
conv=CONVS |
convert the file as per the comma-separated symbol list. |
count=N |
copy only N input blocks |
ibs=BYTES |
read up to BYTES bytes at a time (default:512) |
if=FILE |
read from FILE instead of stdin |
ifflag=FLAGS |
read as per the comma-separated symbol list |
obs=BYTES |
write BYTES bytes at a time (default: 512) |
of=FILE |
write to FILE instead of stdout |
oflag=FLAGS |
write as per the comma-separated symbol list |
seek=BLOCKS |
skip BLOCKS obs-sized blocks at the start of the input |
skip=BLOCKS |
skip BLOCKS ibs-sized blocks at the start of the input |
status=noxfer |
suppress transfer statistics |
Example: Backing up a file and transfer data into another file.
In this example, in the directory studytonight, there is a file shadow.txt after using dd if=shadow.txt of=~snow.txt,
a backup of shadow.txt file ~snow.txt created in studytonight directory.
Example: Backing up the MBR record from /dev/sda directory to /Desktop/mbr.img .
if "dd" command in Linux operating system used for backing up similarly, "dd" command is also used for restoring as per requirement.
Example: Clone one hard disk to another hard disk.
# dd if=/dev/sda of=/dev/sdb
Press enter to execute this command. Completion time will depend on the size of the disk or partition. Make sure that sdb is large enough to store it. This is useful when we are building many machines with the same configuration.
Example: Create a backup-file in a disk file.
We can also create a backup of CDROM using the command.
# dd if = /dev/cdrom of = backup.iso bs = 2048
This command is working after inserting a CD and the block size will be 2048 bytes.
Example: Convert all letters in a file lower-upper mixed case into upper case.
Conclusion
In this tutorial, we covered how to convert and copy a file using the dd
command with available options and suitable examples. It is also called a data duplicator.
warning - Entering wrong values or improper usage of the dd
command can destroy, wipe, or overwrite the data so be careful while using the dd
command either it will be data destroyer.