Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Diff Command in Linux - DesignLinux

Sep 18 2020
designlinux 0 Comments
Diff Command in Linux

The diff command is used to compare two files line by line and contents of the directories. In this guide we will cover the diff command in Linux.

How to Use the diff Command#

Following is the basic syntax of the diff command:

diff [OPTION]... FILES

You can get the output of diff command in multiple formats like normal, context and unified formats. If the files match, it will not show any output.

You can save the output of command to a file using the redirection operator:

diff filename1 filename2 > patch

Here are two files which we will use in this guide to explain working of diff command.

Ubuntu
Arch Linux
Debian
CentOS
Fedora
Kubuntu
Ubuntu
Debian
Arch Linux
Centos
Fedora

Normal Format#

By default, when run the diff command for two files, it will show output in the normal format:

diff file1 file2

It should show output something like below:

0a1
> Kubuntu
2d2
< Arch Linux
4c4,5
< CentOS
---
> Arch Linux
> Centos

The output of the normal format contains one or multiple sections which shows the differences.

change-command
< from-file-line...
---
> to-file-line...

In the output 0a1, 2d2 and 4c4,5 are change commands. Each change command contains the following, from left to right:

  • The line number or range of lines in the first file.
  • A special change character.
  • The line number or range of lines in the second file.

Following are the other change characters:

  • a – Add the lines.
  • c – Change the lines.
  • d – Delete the lines.

The change command is followed by the complete lines that are removed (<) and added to the file (>).

Following is the explaination of the output:

  • 0a1 – It means add line 1 of the second file at the starting of the file1.
    • > Kubuntu – The line from the second line that is added to the first file as described above.
  • 2d2 – Meaning of this is to delete line 2 in the first file. The 2 after the d symbol means that if the line is not deleted it would appear on line 2 in the second file.
    • < Arch Linux – the deleted line.
  • 4c4,5 – Change line 5 in the first file with lines 4-5 from the second file.
    • < CentOS – The line in the first file to be replace.
    • --- – Separator.
    • > Arch Linux and > CentOS – Lines from the second file replacing the line in the first file.

Context Format#

To show output in context format, use -c option with diff command. In the context format output, the diff command shows few lines of context around the lines that displays differences.

diff -c file1 file2
*** file1.txt   2020-09-17 14:03:48.890297526 +0000
--- file2.txt   2020-09-17 14:04:08.330295458 +0000
***************
*** 1,6 ****
  Ubuntu
- Arch Linux
  Debian
! CentOS
  Fedora

--- 1,7 ----
+ Kubuntu
  Ubuntu
  Debian
! Arch Linux
! Centos
  Fedora

In above output, you can see it starts with file names and the timestamps, after that it shows the differences. It should display each section as below:

from-file-line-numbers and to-file-line-numbers – The line numbers or comma-separated range of lines in the first and second file, respectively.

from-file-line and to-file-line – Shows the line which have differences:

  • It shows line starting with two spaces which are same in both files.
  • When line starting with - symbol, that means that line is not in second file.
  • Line beginning with + symbol means that line is not in first file.
  • Lines starting with the exclamation mark (!) are the lines that are changed between two files.

Below is the explanation of the important parts of the output:

Here only one section have the difference.

  • *** 1,6 **** and --- 1,7 ---- shows the range of the lines from the first and second files that are included in this section.
  • The lines are started with double space are same in both files, those are Ubuntu, Debian, Fedora, and the last empty line.
  • The line with value Arch Linux of the first file shows that same value exists in second file but the positions are different.
  • Line + Kubuntu from the second file corresponds to nothing in the first file.
  • Line ! CentOS from the first file and lines ! Arch Linux and ! CentOS from the second file are changed between the files.

The context lines defaults to three, if you would like to change you can use the -C (--contexts) option:

diff -C 1 file1 file2
*** file1.txt   2020-09-17 14:03:48.890297526 +0000
--- file2.txt   2020-09-17 14:04:08.330295458 +0000
***************
*** 1,5 ****
  Ubuntu
- Arch Linux
  Debian
! CentOS
  Fedora
--- 1,6 ----
+ Kubuntu
  Ubuntu
  Debian
! Arch Linux
! Centos
  Fedora

Unified Format#

The unified output format contains the smaller output and it’s advance version of context format.

You should use -u option with diff command to output in unified format:

diff -u 1 file1 file2
--- file1.txt   2020-09-17 14:03:48.890297526 +0000
+++ file2.txt   2020-09-17 14:04:08.330295458 +0000
@@ -1,6 +1,7 @@
+Kubuntu
 Ubuntu
-Arch Linux
 Debian
-CentOS
+Arch Linux
+Centos
 Fedora

Conclusion#

The diff command is most useful command to compare text files for differences in Linux systems. For more information, type man diff in your terminal.

If you have any questions, please leave a comment below.

Related

Tags: diff, terminal

How to Install Windows Subsystem for Linux

Prev Post

How to Install Moodle Learning Platform with Nginx in CentOS 8

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 2
  • 1,477
  • 567,746

DesignLinux.com © All rights reserved

Go to mobile version