Categories: TutorialsUbuntu

diff Command in Linux

Introduction

diff is an acronym for the difference. It is a useful command used to compare the differences between files based on the lines of the file.

This command just tells us which line to change in the file to make the 2 files the same. To be more precise, it creates a list of changes in the first file to match the second file.

This tutorial will guide you on how to compare two text files by the diff command in Linux as you go through below.

The syntax of diff command

$ diff [options] file1 file2

You need to remember some special symbols of diff:

Sponsored

a: add

c: change

d: delete

For example, I have 2 files a.txt and b.txt

Now we will try to use the diff command with these 2 files:

$ diff a.txt b.txt

Output:

This command will print out:

  • Corresponding line number.
  • Special symbols.

Definition:

0a1,2 means from line 0 you need to add Malaysia and Campuchia to lines 1, 2 of the first file.

3,5c5 means from line 3 to line 5 of the first file you need to change it to match line 5 of the second file.

— to separate line 1 from line 2.

Options

1. -c (context): Provides context to easily spot the differences

We will apply with files a.txt and b.txt:

$ diff -c a.txt b.txt

Output:

*** to specify the first file

Sponsored

— to specify the second file

+ to specify lines that need to add to the first file

! to specify lines that changed

2. -u (unified): To check differences in unified mode

$ diff -u a.txt b.txt

Output:

— to specify the first file

+++ to specify the second file

@@ mark the beginning of changes and which line of each file

3. -i: To case sensitive

For example, I have 2 files a.txt and b.txt

Now we will try to use the diff command with -i option with these 2 files:

$ diff -i a.txt b.txt

Output:

4. –version: Check your version

$ diff --version

Output:

Conclusion

In this article, we guided you on how to compare two text files by the diff command in Linux.

Thanks for reading!

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.

Ubuntu Server Admin

Recent Posts

How to Install Google Cloud BigQuery Python client library on Linux

In this article, we will see how to Install Google Cloud BigQuery Python client library…

2 days ago

Wallpaper Contest for Xfce 4.20 open for voting

Nov 15,2024 Wallpaper Contest for Xfce 4.20 open for voting The submission phase for the…

2 days ago

Canonical announces the first MicroCloud LTS release

MicroCloud 2.1.0 LTS is now available, expanding the number of Canonical infrastructure solutions with a…

3 days ago

Join Canonical in Paris at Dell Technologies Forum

Canonical is thrilled to be joining forces with Dell Technologies at the upcoming Dell Technologies…

4 days ago

Bringing automation to open source 5G software at Ubuntu Summit 2024

In today’s massive private mobile network (PMN) market, one of the most common approaches to…

5 days ago

Ubuntu Weekly Newsletter Issue 865

Welcome to the Ubuntu Weekly Newsletter, Issue 865 for the week of November 3 –…

7 days ago