# Cheatsheet & Examples: diff

diff is a command-line utility used to compare the contents of two files and display the differences between them. It is commonly used to analyze changes in code, text, or configuration files.

## Compare Two Files  
Example Usage:  
`diff file1 file2`  

What it does:  
Compares the contents of `file1` and `file2` and outputs the lines that differ, indicating which lines are added, deleted, or changed.  

Command-line Arguments Explained:  
- `file1`: The first file to compare.  
- `file2`: The second file to compare.  

---

## Generate a Unified Diff  
Example Usage:  
`diff -u file1 file2`  

What it does:  
Produces a "unified" diff format that shows the context around changed lines, making it easier to apply patches or understand the changes in code.  

Command-line Arguments Explained:  
- `-u`: Specifies the unified diff format, which includes a few lines of context before and after each change.  

---

## Generate a Context Diff  
Example Usage:  
`diff -c file1 file2`  

What it does:  
Creates a "context diff" that includes more surrounding lines than the default format, useful for detailed analysis of changes in files.  

Command-line Arguments Explained:  
- `-c`: Produces a context diff, including lines before and after each difference.  

---

## Compare Two Directories Recursively  
Example Usage:  
`diff -r dir1 dir2`  

What it does:  
Compares all files in `dir1` and `dir2`, including subdirectories, and reports on differences in file contents and structure.  

Command-line Arguments Explained:  
- `-r`: Recursively compares the contents of directories.  

---

## Ignore Whitespace Differences  
Example Usage:  
`diff -w file1 file2`  

What it does:  
Compares files while ignoring changes in whitespace (spaces, tabs, line breaks), useful for focusing on actual content changes.  

Command-line Arguments Explained:  
- `-w`: Ignores all whitespace differences during the comparison.  

---

## Compare Files Side by Side  
Example Usage:  
`diff -y file1 file2`  

What it does:  
Displays the contents of `file1` and `file2` side by side for easier visual comparison, especially for small files.  

Command-line Arguments Explained:  
- `-y`: Outputs the comparison in a side-by-side format.  

---

## Ignore Case Sensitivity  
Example Usage:  
`diff -i file1 file2`  

What it does:  
Compares files in a case-insensitive manner, treating uppercase and lowercase letters as equivalent.  

Command-line Arguments Explained:  
- `-i`: Ignores case differences during the comparison.  

---

## Show Only the Filenames with Differences  
Example Usage:  
`diff -q file1 file2`  

What it does:  
Prints a brief summary of whether the files differ, without showing the actual lines of difference.  

Command-line Arguments Explained:  
- `-q`: Quiet mode, only reports if files are different.  

---

## Compare with a Specific Number of Context Lines  
Example Usage:  
`diff -C 3 file1 file2`  

What it does:  
Displays three lines of context around each difference, providing a balanced view of changes.  

Command-line Arguments Explained:  
- `-C 3`: Specifies three context lines to show for each difference.  

---

## Compare Binary Files  
Example Usage:  
`diff -a file1 file2`  

What it does:  
Treats all files as text (even if they are binary), allowing comparison of their contents line by line.  

Command-line Arguments Explained:  
- `-a`: Treats each file as a text file, ensuring line-by-line comparison.  

---

## Show Line Numbers for Differences  
Example Usage:  
`diff -n file1 file2`  

What it does:  
Outputs the line numbers where differences occur, aiding in locating changes.  

Command-line Arguments Explained:  
- `-n`: Displays line numbers for each change.  

---

## Ignore Blank Lines in Comparison  
Example Usage:  
`diff -B file1 file2`  

What it does:  
Skips differences that involve only blank lines, making the output cleaner for files with formatting changes.  

Command-line Arguments Explained:  
- `-B`: Ignores blank lines when comparing files.  

---

## Compare with a Custom Header  
Example Usage:  
`diff -L "Header1" -L "Header2" file1 file2`  

What it does:  
Adds custom headers to the output for clarity, especially when diffing files with different names or purposes.  

Command-line Arguments Explained:  
- `-L "Header1"`: Sets a custom header for `file1`.  
- `-L "Header2"`: Sets a custom header for `file2`.  

---

## Compare Without Showing Exact Lines  
Example Usage:  
`diff -s file1 file2`  

What it does:  
Shows no output if the files are identical, and only reports if they differ.  

Command-line Arguments Explained:  
- `-s`: Suppresses output for identical files, making differences easier to spot.  

---

## Compare with a Specific Line Separator  
Example Usage:  
`diff -t file1 file2`  

What it does:  
Replaces tabs with spaces or other characters for consistent formatting in the output.  

Command-line Arguments Explained:  
- `-t`: Expands tabs to spaces in the output for clarity.  

---

## Compare with a Custom Output Format  
Example Usage:  
`diff -e file1 file2`  

What it does:  
Outputs the differences in ed command format, often used for creating patch files.  

Command-line Arguments Explained:  
- `-e`: Produces an ed script that can be used to transform `file1` into `file2`.  

---

## Compare with Minimal Output  
Example Usage:  
`diff --brief file1 file2`  

What it does:  
Similar to `-q`, but provides a more concise output about file identity.  

Command-line Arguments Explained:  
- `--brief`: Outputs only whether the files are identical or different.  

---

## Compare Files with a Specific Algorithm  
Example Usage:  
`diff -d file1 file2`  

What it does:  
Uses a different algorithm (e.g., for large or binary files) to find differences.  

Command-line Arguments Explained:  
- `-d`: Uses a different diff algorithm, which can be more efficient for certain file types.
