How to Use the diff Command to Compare Files in Ubuntu

If you have two slightly different versions of a configuration file, a script, or a long text document, finding the exact changes between them manually is incredibly tedious. You could open both files side-by-side and read them line-by-line, but you are almost guaranteed to miss a subtle change like a deleted comma or an altered IP address.

In Ubuntu (and all Linux distributions), you can instantly compare two files using the built-in diff command. This powerful terminal utility analyses both files simultaneously and prints exactly which lines have been added, deleted, or modified to make the first file match the second file.

How to Run a Basic Comparison

To use the tool, you simply need the paths to the two files you want to compare.

  1. Open your Ubuntu terminal.
  2. Type the diff command followed by the name of the original file, and then the name of the new/edited file. For example:
    diff old_config.txt new_config.txt
  3. Press Enter.

The terminal will output the results instantly. However, the basic output of diff can be confusing for beginners because it uses a highly technical formatting system.

How to Read Standard diff Output

If you run a basic comparison, the output will look something like this:

3c3
< ServerIP=192.168.1.50
---
> ServerIP=192.168.1.100

Here is how to decode this output:

  • The alphanumeric code (3c3): This tells you exactly what happened. The first number is the line in the first file. The letter is the action (a for added, d for deleted, c for changed). The second number is the line in the second file. So, “3c3” means line 3 was changed.
  • Lines starting with <: These represent text from the first file you specified (e.g., old_config.txt).
  • Lines starting with >: These represent text from the second file you specified (e.g., new_config.txt).

In our example, diff is telling us that line 3 changed from an IP of .50 to an IP of .100.

How to Use Unified Format (Easier to Read)

The standard output is powerful but visually messy. Most developers prefer to use the Unified Format, which looks identical to the standard formatting used by Git and GitHub.

To use Unified Format, simply add the -u flag to your command:

diff -u old_config.txt new_config.txt

The output will now display a clean block of context lines, with deleted text marked by a clear minus sign (-) and new text marked by a plus sign (+). This makes it significantly easier to read.

How to Compare Entire Directories

The diff command is not limited to single files. If you have an entire folder of code and you want to know what changed in a new version of the folder, you can compare the directories directly.

diff -rq old_project_folder/ new_project_folder/
  • The -r (recursive) flag tells diff to look inside every sub-folder.
  • The -q (brief) flag tells diff to stop printing the exact code changes and simply output a clean list of which files are different (e.g., “Files old/index.html and new/index.html differ”).

Leave a Reply

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

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.