The Wayback Machine - https://web.archive.org/web/20240606190643/https://www.geeksforgeeks.org/dd-command-linux/
Open In App

‘dd’ command in Linux

Last Updated : 10 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

dd is a command-line utility for Unix and Unix-like operating systems whose primary purpose is to convert and copy files.

  • On Unix, device drivers for hardware (such as hard disk drives) and special device files (such as /dev/zero and /dev/random) appear in the file system just like normal files.
  • dd can also read and/or write from/to these files, provided that function is implemented in their respective drivers
  • As a result, dd can be used for tasks such as backing up the boot sector of a hard drive, and obtaining a fixed amount of random data.
  • The dd program can also perform conversions on the data as it is copied, including byte order swapping and conversion to and from the ASCII and EBCDIC text encodings.

Usage : The command line syntax of dd differs from many other Unix programs, in that it uses the syntax option=value for its command line options, rather than the more-standard -option value or –option=value formats. By default, dd reads from stdin and writes to stdout, but these can be changed by using the if (input file) and of (output file) options.

Some practical examples on dd command :

  1. To backup the entire harddisk : To backup an entire copy of a hard disk to another hard disk connected to the same system, execute the dd command as shown. In this dd command example, the UNIX device name of the source hard disk is /dev/hda, and device name of the target hard disk is /dev/hdb.
    # dd if=/dev/sda of=/dev/sdb
    
    • “if” represents inputfile, and “of” represents output file. So the exact copy of /dev/sda will be available in /dev/sdb.
    • If there are any errors, the above command will fail. If you give the parameter “conv=noerror” then it will continue to copy if there are read errors.
    • Input file and output file should be mentioned very carefully. Just in case, you mention source device in the target and vice versa, you might loss all your data.
    • To copy, hard drive to hard drive using dd command given below, sync option allows you to copy everything using synchronized I/O.
      # dd if=/dev/sda of=/dev/sdb conv=noerror, sync
      
  2. To backup a Partition : You can use the device name of a partition in the input file, and in the output either you can specify your target path or image file as shown in the dd command.
    # dd if=/dev/hda1 of=~/partition.img
    
  3. To create an image of a Hard Disk : Instead of taking a backup of the hard disk, you can create an image file of the hard disk and save it in other storage devices. There are many advantages of backing up your data to a disk image, one being the ease of use. This method is typically faster than other types of backups, enabling you to quickly restore data following an unexpected catastrophe.It creates the image of a hard disk /dev/hda.
    # dd if=/dev/hda of=~/hdadisk.img
    
  4. To restore using the Hard Disk Image : To restore a hard disk with the image file of an another hard disk, the following dd command can be used
    # dd if=hdadisk.img of=/dev/hdb
    

    The image file hdadisk.img file, is the image of a /dev/hda, so the above command will restore the image of /dev/hda to /dev/hdb.

  5. To create CDROM Backup : dd command allows you to create an iso file from a source file. So we can insert the CD and enter dd command to create an iso file of a CD content.
    # dd if=/dev/cdrom of=tgsservice.iso bs=2048
    

    dd command reads one block of input and process it and writes it into an output file. You can specify the block size for input and output file. In the above dd command example, the parameter “bs” specifies the block size for the both the input and output file. So dd uses 2048bytes as a block size in the above command.

References :


Previous Article
Next Article

Similar Reads

uniq command in Linux with examples
The linux uniq command is basically used to remove all repeated lines in a file. This command is used when a line is repeated multiple times and replace these multiple lines with one line. This command is designed to work on sorted files. Purpose : Remove repetitious lines from the sorted 'input-file' and send unique lines to the 'output-file'. If
2 min read
SAR command in Linux to monitor system performance
sar (System Activity Report) It can be used to monitor Linux system's resources like CPU usage, Memory utilization, I/O devices consumption, Network monitoring, Disk usage, process and thread allocation, battery performance, Plug and play devices, Processor performance, file system and more. Linux system Monitoring and analyzing aids in understandi
3 min read
pr command in Linux
In Linux/Unix pr command is used to prepare a file for printing by adding suitable footers, headers, and the formatted text. pr command actually adds 5 lines of margin both at the top and bottom of the page. The header part shows the date and time of the last modification of the file with the file name and the page number. Syntax: pr [options][file
2 min read
Tree command in Linux with examples
In UNIX/LINUX systems, as well as MS-DOS and Microsoft Windows, the tree is a recursive directory listing program that produces a depth-indented listing of files. With no arguments, the tree lists the files in the current directory. When directory arguments are given, the tree lists all the files or directories found in the given directories each i
5 min read
How to List Current Logged-In Users in Linux | who Command
Linux, being a powerful and versatile operating system, provides several command-line tools to manage and monitor user activity. One common administrative task is to identify and list the currently logged-in users on a Linux system. In this article, we will delve into multiple ways to achieve this, exploring commands such as who, user, w, last, fin
5 min read
How to View the Content of File in Linux | cat Command
The cat command in Linux is more than just a simple tool; it's a versatile companion for various file-related operations, allowing users to view, concatenate, create, copy, merge, and manipulate file contents. Let's delve into the details of some frequently used cat commands, understanding each example along the way. Table of Content Basic Syntax o
6 min read
'IPCS' command in Linux with examples
ipcs shows information on the inter-process communication facilities for which the calling process has read access. By default, it shows information about all three resources: shared memory segments, message queues, and semaphore arrays. Without options, the information shall be written in short format for message queues, shared memory segments, an
3 min read
Sed Command in Linux/Unix with examples
SED command in UNIX stands for stream editor and it can perform lots of functions on file like searching, find and replace, insertion or deletion. Though most common use of SED command in UNIX is for substitution or for find and replace. By using SED you can edit files even without opening them, which is much quicker way to find and replace somethi
6 min read
Linux Virtualization : Linux Containers (lxc)
Operating-system-level virtualization is a server virtualization method in which an operating system's kernel allows multiple isolated user-space instances, instead of just one. Such instances, which are sometimes called containers, software containers, virtualization engines (VEs), or jails (FreeBSD jail or chroot jail), may look and feel like a r
6 min read
getopt() function in C to parse command line arguments
The getopt() function is a builtin function in C and is used to parse command line arguments. Syntax: getopt(int argc, char *const argv[], const char *optstring) optstring is simply a list of characters, each representing a single character option. Return Value: The getopt() function returns different values: If the option takes a value, that value
2 min read
Article Tags :
Practice Tags :