Cheat Sheet #day59 - diff

Cloud TunedCloud Tuned
2 min read

diff Command Cheatsheet

The diff command in Unix-like systems is used to compare two files line by line and display the differences between them. Here’s a quick reference guide:

Basic Syntax

diff [OPTION]... FILE1 FILE2

Common Options

  • -u, --unified: Show differences in a unified format. This is the most commonly used format.

      diff -u file1.txt file2.txt
    
  • -c, --context: Show differences in a context format, which includes a few lines of context before and after each change.

      diff -c file1.txt file2.txt
    
  • -i, --ignore-case: Ignore case differences.

      diff -i file1.txt file2.txt
    
  • -w, --ignore-all-space: Ignore all white space.

      diff -w file1.txt file2.txt
    
  • -B, --ignore-blank-lines: Ignore changes whose lines are all blank.

      diff -B file1.txt file2.txt
    
  • -y: Display differences side by side.

      diff -y file1.txt file2.txt
    
  • -Z, --ignore-trailing-space: Ignore whitespace at the end of lines.

      diff -Z file1.txt file2.txt
    

Examples

  1. Basic difference between two files:

     diff file1.txt file2.txt
    
  2. Unified format comparison:

     diff -u file1.txt file2.txt
    
  3. Context format comparison:

     diff -c file1.txt file2.txt
    
  4. Ignore case differences:

     diff -i file1.txt file2.txt
    
  5. Ignore all white space:

     diff -w file1.txt file2.txt
    
  6. Ignore blank lines:

     diff -B file1.txt file2.txt
    
  7. Side-by-side comparison:

     diff -y file1.txt file2.txt
    
  8. Ignore trailing spaces:

     diff -Z file1.txt file2.txt
    

Displaying Differences with Line Numbers

  • Show line numbers in the output:

      diff -n file1.txt file2.txt
    

Additional Information

  • Help option:

      diff --help
    
  • View diff manual page:

      man diff
    

This cheatsheet covers the essential options and usage scenarios for the diff command. For more advanced features and details, refer to the man page or use diff --help.

0
Subscribe to my newsletter

Read articles from Cloud Tuned directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Cloud Tuned
Cloud Tuned