Enhancing Skills

diff: Compare Files Line by Line

Description:
The diff command compares two files line by line and outputs the differences between them. It is useful for identifying changes between files, often used in version control systems.

Command:

diff file1.txt file2.txt

Sample Input

file1.txt:

apple
banana
cherry

file2.txt:

banana
cherry
date

Sample Output:

1d0
< apple
3a3
> date

Explanation:

  • 1d0: Line 1 is present in file1.txt but not in file2.txt.
  • < apple: The line apple is in file1.txt but not in file2.txt.
  • 3a3: Line 3 of file2.txt is added after line 3 of file1.txt.
  • > date: The line date is in file2.txt but not in file1.txt.

Options:

  • -u: Unified format, showing a few lines of context around the differences.
  • -c: Context format, similar to unified but with more context lines.
  • -i: Ignore case differences.

Example with -u Option:

To show the differences in unified format:

diff -u file1.txt file2.txt

Sample Output with -u:

--- file1.txt
+++ file2.txt
@@ -1,3 +1,3 @@
 apple
 banana
-cherry
+date

This command is helpful for patch creation, code reviews, and file comparison tasks.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.