Insight: Spot the Difference—An Ubuntu Newbie's Guide to the diff Command


Insight: Spot the Difference—
An Ubuntu Newbie's Guide to the diff Command







Welcome, Ubuntu explorers! Ever found yourself with two similar files and wondered what exactly changed between them? That's where the handy command-line tool diff comes to the rescue. Don't let the terminal intimidate you; diff is a straightforward and incredibly useful tool for anyone working with text files. Let's take a look!


What Does diff Do?

In its simplest form, diff compares two files line by line and highlights the differences it finds. It tells you which lines have been added, deleted, or changed. This can be invaluable for:
  • Tracking changes in configuration files: See exactly what you altered.
  • Comparing different versions of a document: Pinpoint the edits made.
  • Understanding software updates: Sometimes, patch notes refer to diff output.


Basic Usage

Open your Ubuntu terminal (usually by pressing Ctrl + Alt + T). 

The basic syntax for using diff is:


Bash
diff file1 file2


Let's create two simple text files to play with. You can use the nano text editor for this:

File 1 (file1.txt):

Bash
This is the first line. 
This line is common. 
Here is some unique text in file one. 
Another common line.


File 2 (file2.txt):

Bash
This is the first line. 
This line is common. 
This line is new in file two. 
Another common line. And yet another line.


Now, let's run the diff command:


Bash
diff file1.txt file2.txt


You'll likely see output that looks something like this:


Bash
3c3 
< Here is some unique text in file one. 
--- 
> This line is new in file two. 
5a6 
> And yet another line.


Let's break down this output:
  • 3c3: This indicates that the line at number 3 in file1.txt has been changed (c) to the line at number 3 in file2.txt.
  • < Here is some unique text in file one.: The < symbol indicates a line that is present in the first file (file1.txt) but not in the second file (in its changed form).
  • ---: This is a separator.
  • > This line is new in file two.: The > symbol indicates a line that is present in the second file (file2.txt) but not in the first file (in its changed form).
  • 5a6: This indicates that a line has been added (a) to the second file after line 5 of the first file (becoming line 6 in the second file).
  • > And yet another line.: This is the line that was added.


Common Options

diff has several useful options to customize its output. Here are a couple of the most common ones for beginners:


-u or --unified: 
This option provides a more readable "unified" output format. It shows a few lines of context around the changes, making it easier to understand where the modifications occurred. Let's try it:


Bash
diff -u file1.txt file2.txt


The output will look something like this:


Bash
--- file1.txt 2025-04-18 10:35:00.123456789 -0500
+++ file2.txt 2025-04-18 10:35:00.987654321 -0500
@@ -1,4 +1,5 @@
 This is the first line.
 This line is common.
-Here is some unique text in file one.
+This line is new in file two.
 Another common line.
+And yet another line.


Here's how to interpret this:
  • Lines starting with - are present only in the first file.
  • Lines starting with + are present only in the second file.
  • Lines without a prefix are common to both files (the context).
  • The @@ -1,4 +1,5 @@ line indicates that the changes occurred around lines 1-4 of the first file and lines 1-5 of the second file.


-y or --side-by-side: 
This option displays the two files side by side, highlighting the differences. This can be very visually intuitive.


Bash
diff -y file1.txt file2.txt


The output might look something like this (depending on your terminal width):


Bash
This is the first line.                 This is the first line.
This line is common.                    This line is common.
Here is some unique text in file one.   | This line is new in file two.
Another common line.                  Another common line.
                                     > And yet another line.


  • A | in the middle indicates a changed line.
  • A < on the left indicates a line only in the first file.
  • A > on the right indicates a line only in the second file.


Wrapping Up

The diff command is a fundamental tool in the Linux world. While this introduction only scratches the surface, you now have the basic knowledge to start comparing text files on your Ubuntu system. Experiment with the different options and see which output format works best for you. As you become more comfortable with the command line, you'll find diff to be an indispensable ally in your Ubuntu adventures! Keep exploring!


Need Ubuntu Expertise?

We'd love to help you with your Ubuntu projects.  Feel free to reach out to us at info@pacificw.com.


Written by Aaron Rose, software engineer and technology writer at Tech-Reader.blog.

Comments

Popular posts from this blog

The New ChatGPT Reason Feature: What It Is and Why You Should Use It

Raspberry Pi Connect vs. RealVNC: A Comprehensive Comparison

The Reasoning Chain in DeepSeek R1: A Glimpse into AI’s Thought Process