diff Command in Linux VPS

diff Command in Linux VPS

The diff command in Linux VPS is used to tell whether two files are different or not, and to reveal the differences between the files line by line.

It is best used to detect the differences between two similar (text) files and it is not advised to compare two different files with this command.

The syntax of the diff command is:

diff [option] file1 file2

Prerequisites to use diff command in Linux VPS

  • A Linux VPS or a machine with a popular Linux distribution
  • diff package already installed
  • Root user or a user with sudo privileges

Understanding the diff command in Linux VPS

The diff command in Linux VPS is a powerful tool used to compare files line by line. It identifies the differences between two files, providing a detailed output that shows what changes need to be made to make the files identical.

When using the diff command, understanding the output is essential:

-Content from the first file is marked by lines starting with “<”

-Content from the second file is marked by lines starting with “>”

-Line numbers indicate the corresponding lines in the first file

-Special symbols show how the first file needs to be altered to match the second one. These symbols include:

  • “A” for adding lines
  • “C” for changing lines
  • “D” for deleting lines

How does the diff command in Linux VPS work?

In this section, we will go through an example use case of the diff command in Linux VPS.

Step one: Generate the first .txt file

To start, we need to have two text files on our Linux machine. Using the nano text editor, we generate the “example1.txt” by running this command:

nano example1.txt

Then, we add the following lines to the “example1.txt” file:

Apple
Orange
Banana
Watermelon
Chery

generating first example text file

Now, save the changes and exit the file.

Step two: Generate the second .txt file

We need to generate the second file using the nano editor, which will be named “example2.txt”:

nano example2.txt

Let’s add something different to this .txt file:

Orange
Peach
Apple
Banana
Melon
Cherry

generating second example text file

Again, save the changes and exit the nano text editor. Now that we have generated the files, we can compare them.

Step three: Compare two files

It is time to use the diff  command to see the differences between the “example1.txt” and “example2.txt” files. To do so, run:

diff example1.txt example2.txt

diff command output for two text files

The output of the command above indexes some instructions on how to revise the first file to contain the same content as in the “example2.txt” file.

The following explanation breaks down the output of the diff command for two sample files:

  • 1d0: This means the first line (1) from the first file should be deleted (d). If not deleted, it would appear before the first line (line 0) in the second file.
  • < Apple: This indicates the content from the first file that needs to be deleted as specified by the “1d0” phrase.
  • 2a2,3: This indicates that after line 2 in the first file, you need to add (a) lines 2 and 3 (2,3) from the second file.
  • > Peach, > Apple: These show the contents from the second file that need to be added as specified by “2a2,3”.
  • 4c5: This means the fourth line (4) in the first file should be changed (c) to match the fifth line (5) in the second file.
  • < Watermelon: This shows the content from the first file that needs to be changed.
  • > Melon: This shows what the content should be changed to, as specified by the “4c5” phrase.

This output helps understand the changes needed to make the files identical, specifying deletions, additions, and modifications with clear indicators for each action.

What is the diff option in Linux?

Here are the detailed components of the various and important diff command options:

1. Basic Comparison Options:

  • -i or –ignore-case: Ignores case differences in file contents.
  • -b or –ignore-space-change: Ignores changes in the amount of whitespace
  • -w or –ignore-all-space: Ignores all whitespace
  • -B or –ignore-blank-lines: Ignores changes where lines are all blank
  • -I or –ignore-matching-lines=RE: Ignores changes where all lines match a regular expression

2. Output Format Options:

  • -c or –context: Produces a context format diff, showing several lines of context
  • -u or –unified: Produces a unified format diff, a more compact form of context format
  • -y or –side-by-side: Displays the differences side by side in columns
  • –normal: Produces a normal diff (default format)
  • -q or –brief: Reports only when files differ without showing details
  • -s or –report-identical-files: Reports when two files are the same

3. Recursive and Directory Comparison:

  • -r or –recursive: Recursively compares any subdirectories found
  • -N or –new-file: Treats absent files as empty

4. Additional Options:

  • -a or –text: Treats all files as text
  • –strip-trailing-cr: Strips trailing carriage return on input
  • –speed-large-files: Optimizes performance for large files with many small changes
  • –color: Adds color to the output for better readability
  • -v or –version: Displays the version information of the diff utility

Using these options, you can customize the output and behavior of the diff command to suit your specific needs when comparing files and directories in Linux.

How do I install diff command in Linux VPS?

The diff command comes preinstalled on most Linux-based systems. To check whether it is installed on your machine or not, run this command:

diff --version

If it is not installed on your Linux VPS, run the command below to install it:

sudo apt install diffutils

How to display diff command in Linux VPS side-by-side?

The sdiff  command is similar to the diff  command, but it merges the differences between two files into a single output. To use the sdiff  command, enter the command followed by the names of the two files you want to compare. For example:

sdiff example1.txt example2.txt

This command will display the differences between “example1.txt” and “example2.txt” side-by-side, allowing you to easily see and merge changes from both files into a unified view.

The output for these files would be like:

Apple                                       Orange
Orange                                      Peach
Banana                                      Apple
Watermelon                                  Banana
Chery                                       Melon
                                            Cherry

Conclusion

As you learned so far, the diff  command in Linux VPS is a powerful and essential tool for comparing files line by line, identifying differences, and understanding how files need to be modified to match each other.

It provides clear indicators for additions, changes, and deletions, making it invaluable for developers and system administrators who need to manage and update files efficiently.

With various options to customize its output and behavior, the diff command can be tailored to fit specific needs, ensuring precise and effective file comparison.

Leave a Reply

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