# Cheatsheet & Examples: dd

The `dd` command is a powerful utility for copying files, converting data, and performing low-level disk operations. It is often used for cloning disks, creating backups, and transferring data between devices.

## Copy a Disk to an Image File  
Example Usage:  
`dd if=/dev/sdX of=image.img`  

What it does:  
Creates a disk image of the source device (`/dev/sdX`) and saves it to `image.img`.  

Command-line Arguments Explained:  
- `if`: Specifies the input file or device (e.g., `/dev/sdX` is the source disk).  
- `of`: Specifies the output file or device (e.g., `image.img` is the destination file).  

## Create a Bootable USB Drive  
Example Usage:  
`dd if=image.iso of=/dev/sdY bs=4M status=progress`  

What it does:  
Writes a bootable ISO file (`image.iso`) to a USB drive (`/dev/sdY`) for creating a bootable device.  

Command-line Arguments Explained:  
- `if`: Input file (e.g., the ISO image to write to the USB).  
- `of`: Output device (e.g., the USB drive, typically `/dev/sdY`).  
- `bs`: Block size (e.g., `4M` improves performance by reading/writing 4MB chunks).  
- `status=progress`: Displays real-time progress of the copy operation.  

## Copy Files with Custom Block Size  
Example Usage:  
`dd if=source.txt of=destination.txt bs=1M`  

What it does:  
Copies `source.txt` to `destination.txt` using a block size of 1MB for faster transfers.  

Command-line Arguments Explained:  
- `if`: Source file (e.g., `source.txt`).  
- `of`: Destination file (e.g., `destination.txt`).  
- `bs`: Block size (e.g., `1M` sets 1MB blocks for efficiency).  

## Monitor Copy Progress  
Example Usage:  
`dd if=largefile of=backupfile bs=1G status=progress`  

What it does:  
Copies a large file (`largefile`) to `backupfile` and shows real-time progress updates.  

Command-line Arguments Explained:  
- `if`: Input file (e.g., `largefile`).  
- `of`: Output file (e.g., `backupfile`).  
- `bs`: Block size (e.g., `1G` for 1GB chunks).  
- `status=progress`: Displays progress (bytes copied, time elapsed, speed).  

## Skip or Seek Specific Blocks  
Example Usage:  
`dd if=image.img of=part.img skip=512 count=100 bs=512`  

What it does:  
Extracts a portion of a disk image (`image.img`) starting from block 512, copying 100 blocks to `part.img`.  

Command-line Arguments Explained:  
- `if`: Input file (e.g., `image.img`).  
- `of`: Output file (e.g., `part.img`).  
- `skip`: Number of input blocks to skip before starting (e.g., 512 blocks).  
- `count`: Number of blocks to copy (e.g., 100 blocks).  
- `bs`: Block size (e.g., `512` for sector-sized blocks).  

## Convert File Case or Process Data  
Example Usage:  
`dd if=input.txt of=output.txt conv=ucase`  

What it does:  
Converts all lowercase letters in `input.txt` to uppercase and saves the result to `output.txt`.  

Command-line Arguments Explained:  
- `if`: Input file (e.g., `input.txt`).  
- `of`: Output file (e.g., `output.txt`).  
- `conv=ucase`: Converts input text to uppercase (other options like `lcases` exist).  

## Transfer Data Over a Network  
Example Usage:  
`dd if=/dev/sda | ssh user@remote 'dd of=/backup/sda.img'`  

What it does:  
Sends data from `/dev/sda` to a remote server's `/backup/sda.img` via SSH.  

Command-line Arguments Explained:  
- `if`: Input device (e.g., `/dev/sda`).  
- `of`: Output path on the remote system (e.g., `/backup/sda.img`).  
- `ssh`: Executes the `dd` command on the remote machine.  

## Create a Zero-Filled File  
Example Usage:  
`dd if=/dev/zero of=file.img bs=1M count=1024`  

What it does:  
Generates a 1GB file (`file.img`) filled with zeros, useful for testing or allocating space.  

Command-line Arguments Explained:  
- `if`: Input source (e.g., `/dev/zero` provides null bytes).  
- `of`: Output file (e.g., `file.img`).  
- `bs`: Block size (e.g., `1M` for 1MB blocks).  
- `count`: Number of blocks to copy (e.g., 1024 × 1MB = 1GB).  

## Clone One Disk to Another  
Example Usage:  
`dd if=/dev/sda of=/dev/sdb`  

What it does:  
Clones the entire contents of `/dev/sda` to `/dev/sdb`, creating an exact copy of the disk.  

Command-line Arguments Explained:  
- `if`: Source disk (`/dev/sda`).  
- `of`: Destination disk (`/dev/sdb`).  

## Verify Data Integrity  
Example Usage:  
`dd if=/dev/sdX of=/dev/null bs=1M conv=noerror,sync`  

What it does:  
Reads data from `/dev/sdX` and discards it (`/dev/null`), skipping errors and padding blocks to ensure data integrity checks.  

Command-line Arguments Explained:  
- `if`: Input source (`/dev/sdX`).  
- `of`: Output destination (`/dev/null` to discard data).  
- `bs`: Block size (e.g., `1M` for efficiency).  
- `conv=noerror,sync`: `noerror` skips read errors, `sync` pads blocks with nulls to handle bad sectors.  

## Compress Data on the Fly  
Example Usage:  
`dd if=image.img | gzip > image.gz`  

What it does:  
Streams a disk image (`image.img`) through `gzip` for compression, saving the result as `image.gz`.  

Command-line Arguments Explained:  
- `if`: Input file (`image.img`).  
- `| gzip`: Pipes the output to `gzip` for compression (not a `dd` argument but part of the command chain).  

## Benchmark Disk Performance  
Example Usage:  
`dd if=/dev/zero of=tempfile bs=1G count=1 oflag=direct`  

What it does:  
Tests write performance by writing 1GB of zeros to `tempfile` without using system cache (`oflag=direct`).  

Command-line Arguments Explained:  
- `if`: Input source (`/dev/zero` for zeros).  
- `of`: Output file (`tempfile`).  
- `bs`: Block size (`1G` for large chunks).  
- `count`: Number of blocks to copy (`1` block of 1GB).  
- `oflag=direct`: Bypasses the system cache for raw I/O.  

## Copy File Segments  
Example Usage:  
`dd if=bigfile of=partfile skip=100 seek=500 bs=1M`  

What it does:  
Copies data from `bigfile` and writes it to `partfile`, skipping 100 blocks and seeking 500 blocks.  

Command-line Arguments Explained:  
- `if`: Input file (`bigfile`).  
- `of`: Output file (`partfile`).  
- `skip`: Skips the first 100 input blocks.  
- `seek`: Starts writing at the 500th output block.  
- `bs`: Block size (e.g., `1M` for 1MB blocks).
