How to use diff command in Linux.

How To Use Diff Command In Linux.

The diff command in Linux is used to compare two text files or directories and display the differences between them. It’s a very useful tool for identifying changes made to files or directories, and it’s commonly used in software development, system administration, and other tasks involving text-based files.

diff Syntax

The syntax for using the diff command is:

diff [option] file1 file2

Output Syntax

When working with diff, it is crucial to know how to interpret the output, which consists of:

  • Output starting with < refers to the content in the first file.
  • Output starting with > refers to the content in the second file.
  • Line numbers corresponding to the first file.
  • A special symbol. Special symbols indicate how the first file needs to be edited to match the second file. The output may display:
    • a (add)
    • c (change)
    • d (delete)
  • Line numbers corresponding to the second file.

diff Example

To show how the diff command works, we created two sample files and compared their content.

. First, using the terminal, create a Linux file named music. We use the Nano text editor, but you can use a text editor of your choice.

sudo nano music

Once the text editor creates and opens the file, add the following lines to it:

1. Crazy Train – Ozzy Osbourne
2. Holy Wars The Punishment Due – Megadeth
3. Iron Man – Black Sabbath
4. Master Of Puppets – Metallica
5. Back In Black – AC/DC
6. Breaking The Law – Judas Priest

. Save and exit the file – hold Ctrl + X and confirm by pressing Y.

Next, create an

example2.txt file by running:

sudo nano my.music

Add the following content to the file:

1 Bohemian Rhapsody - Queen.
2 Stairway to Heaven - Led Zeppelin.
3 Imagine - John Lennon.
4 Smells Like Teen Spirit - Nirvana.
5 One - Metallica.
6 Hotel California - Eagles.

Save the changes and exit.

diff Options

Without additional options, diff displays the output in the default format. There are ways to modify this output to make it more understandable or applicable for your use case. Read on to learn more about diff command options.

-c Option

The context format is a diff command-line utility option that outputs several lines of context around the lines that differ.

To display the difference between the files in context form, use the command:

diff -c music my.music

Take a look at the output for the sample files in the context form in the image below.

Lines displaying information about the first file begin with ***, while lines indicating the second file start with.

The first two lines display the name and timestamp of both files:

*** music       2023-07-03 16:53:02.582928469 +0300
--- my.music    2023-08-07 15:32:29.523245500 +0300

**************** – is used just as a separator.

Before listing the lines from each file, the output starts with the line range of the files:

***  1,8 ****
---  1,6 ----

The rest of the lines list the content of the files. The beginning of each line instructs how to modify music to make it the same as my.music If the line starts with:

(minus) – it needs to be deleted from the first file.
+ (plus) – it needs to be added to the first file.
! (exclamation mark) – it needs to be changed to the corresponding line from the second file.

If there is no symbol, the line remains the same.

-u Option

The unified format is an option you can add to display output without any redundant context lines. To do so, use the command:

diff -u file1 file2

Now, let’s examine the output for the sample files in the unified format:

Lines displaying information about the first file begin with , while lines indicating the second file start with +++.

The first two lines display the name and timestamp of both files:

--- music       2023-07-03 16:53:02.582928469 +0300
+++ my.music    2023-08-07 15:32:29.523245500 +0300

@@ -1,8 +1,6 @@ – shows the line range for both files.

The lines below display the content of the files and how to modify music to make it identical to my.music. When the line starts with:

(minus) – it needs to be deleted from the first file.
+ (plus) – it needs to be added to the first file.

If there is no symbol, the line remains the same.

-i Option

By default, diff is case sensitive. If you want it to ignore case, add the -i option to the command:

diff -i music my.music

For example, if we create one file with the following lines:

< 1. Crazy Train – Ozzy Osbourne
< 2. Holy Wars The Punishment Due – Megadeth
< 3. Iron Man – Black Sabbath
< 4. Master Of Puppets – Metallica
< 5. Back In Black – AC/DC
< 6. Breaking The Law – Judas Priest

And another file with the content:

> 1 Bohemian Rhapsody - Queen.
> 2 Stairway to Heaven - Led Zeppelin.
> 3 Imagine - John Lennon.
> 4 Smells Like Teen Spirit - Nirvana.
> 5 One - Metallica.
> 6 Hotel California - Eagles.

The output with no additional options shows there are differences between the files and gives instructions how to modify them.

However, if you add the -i option, there is no output as the command doesn’t detect any differences.

–version Option

To check the version of diff running on your system, run the command:

diff --version

–help Option

To output a summary of diff usage run:

diff --help

Other diff Options

Other options that diff supports include:

-a / –textView files as text and compare them line-by-line.
-b / –ignore-space-changeIgnore white spaces when comparing files.
-B / –ignore-blank-lines<code>Ignore blank lines when comparing files.
–binaryCompare and write data in binary mode.
-d–minimalModify the algorithm (for example, to find a smaller set of changes).
-e / –edMake output a valid ed script.
-E / –ignore-tab-expansionIgnore tab extension when comparing files.
-l / –paginateRun the output through pr to paginate it.
-N / –new-fileTreat a missing file as present but empty.
-q / –briefOutput whether files differ without specifying details.
-s / –report-identical-filesOutput when the files are identical.
-w / –ignore-all-spaceIgnore white space when comparing files.

Conclusion:

The diff command is a potent tool in the Linux command-line arsenal, enabling users to compare and analyze textual differences in files and directories. Whether you’re a developer tracking changes in your codebase, a system administrator ensuring configuration consistency, or simply a user seeking to understand modifications, the diff command provides a clear and concise means of visualizing changes. By mastering its usage, you empower yourself with a valuable skill that can streamline your workflows and enhance your efficiency in the Linux environment.