dd Command in Linux

Learn via video courses
Topics Covered

Overview

The dd command in Linux is a versatile and powerful utility used for copying and converting files. It is especially useful for tasks such as creating bootable USB drives, backing up data, and converting file formats.

Linux dd Command Syntax

The syntax for the dd command is as follows:

Where:

  • if: Specifies the input file to be used.
  • of: Specifies the output file to be created or overwritten.

dd Command Options:

  • bs: Sets the block size for input and output in bytes (default is 512).
  • count: Copies only a specific number of input blocks.
  • seek: Skips a specified number of output blocks before writing.
  • skip: Skips a specified number of input blocks before reading.

Example Usages

  • Create a bootable USB drive from an ISO file.:

    Output:

    Explanation: The command takes input.iso as the input file, writes it to /dev/sdb (the USB drive), with a block size of 4M and displays progress.

  • Backup an entire disk to an image file.:

    Output:

    Explanation: The command takes /dev/sda as the input file (the entire disk), writes it to backup.img, with a block size of 4M and displays progress.

Tips

  • Always double-check the input and output files to avoid data loss.

  • Use the 'status=progress' option to display progress during long operations.

Advanced Use Cases of dd Command in Linux

  • Convert a file from lowercase to uppercase.:

    Output:

    Explanation: The command takes input.txt as the input file, writes it to output.txt, and converts all lowercase characters to uppercase using the conv=ucase option.

  • Create a 1GB file filled with zeros.:

    Output:

    Explanation: The command reads from /dev/zero (a special file that produces null characters), writes it to 1GBfile.img, with a block size of 1M and a count of 1024 (1GB).

  • Securely erase a disk.:

    Output:

    Explanation: The command reads from /dev/urandom (a special file that produces random data), writes it to /dev/sda (the disk), with a block size of 4M, effectively overwriting the entire disk with random data.

Conclusion

  • dd is a powerful and versatile utility for copying and converting files.

  • It can be used for creating bootable USB drives, backing up data, and converting file formats.

  • Always double-check input and output files to avoid data loss.

  • Use the 'status=progress' option to display progress during long operations.