Linux diff Command

XnConvert: Best Batch Image Converter for Linux Windows & Mac

XnConvert is a cross platform batch image processing software with which we can not only change the format of multiple images in one click but also perform watermarking, special effects, border, image adjustment etc. in a one click. Available for Windows, Mac and Linux.

How to do voice typing in Google Docs and WordPress

Gone are the days when typing long documents either required the help of a typist or learning typing itself. Because now it is possible to type by speaking due to the development of artificial intelligence. In this post we will learn about various web based tools for Voice Typing.

How to save the command output in a file in a Linux terminal

In this post are going to discuss about how you can save the output of any Linux command in a file.
Linux diff Command 4
Ankur Guptahttps://antarjaal.in
Web developer by profession, Over 10 years experience of building websites and web applications. Currently working on Epaper CMS Cloud (Software As Service based product). Special interest in computer and technology. After writing on the blogger platform for years, started his own tech portal Antarjaal.In.

On the Linux operating systems, the diff command analyzes two files and prints the lines that are different. In short, it outputs a set of instructions to make one file the same as another file.

The diff software does not actually change the files it compares. However, it can optionally produce a script (if the -e option is specified) for the program ed or ex that can be used to implement the changes.

For example, let’s discuss about two files, file1.txt and file2.txt

file1.txt has the following four lines:

I need to buy apples.
I need to run the laundry.
I need to wash the dog.
I need to get the car detailed

… and file2.txt has these four lines:

I need to buy apples.
I need to do the laundry.
I need to wash the car.
I need to get the dog detailed.

Then we can use the diff command which will show us which lines different between the two files.

diff file1.txt file2.txt

… and the result will be:

2,4c2,4
< I need to run the laundry.
< I need to wash the dog.
< I need to get the car detailed.
---
> I need to do the laundry.
> I need to wash the car.
> I need to get the dog detailed.

When diff shows these differences to you, it is telling you how to change the first file to match the second file. It is shown as follows.

READ  5 Best Open Source Software You Should Try
READ  Get Linux Commands Cheat sheet using TLDR

The following line will appear in the first line:

  • Line number corresponding to the first file,
  • A letter (a for adding, c for change or d for deleting), and
  • Line number corresponding to the second file.

In our output above, “2,4c2,4” means: replace the second to fourth line of file1.txt with the second to fourth line of file2.txt

The lines that are preceded by < are those of the first file, and the lines that are preceded by > are from the second file.

(—) These three dashes separate the rows of both files.

Let us take another example of diff command

file1.txt has the following lines:

Gujarat
Uttar Pradesh
Kolkata
Bihar
Jammu and Kashmir

file2.txt has the following lines:

Tamil Nadu
Gujarat
Andhra Pradesh
Bihar
Uttar pradesh

Now if you give diff command then output will be:

$ diff file1.txt file2.txt
0a1
> Tamil Nadu
2,3c3
< Uttar Pradesh
 Andhra Pradesh
5c5
 Uttar pradesh
  • 0a1 is telling that the first line of file2.txt has to be “added” to file1.txt at the very beginning (denoted by zero). Here ‌a means to add.
  • 2,3c3 is telling us to replace the second and third lines of file1.txt with the third line of file2.txt. Here c means change.
  • 5c5 means to replace the fifth line of file1.txt with the fifth line of file2.txt.
READ  4 Commands to Get Detailed Linux OS Info

By the way diff command output is not for humans. It is for reading by machines. The results shown above are the default output of the diff command.

Read More: Check Internet Connection Speed using Linux Command Line

If you want to see its output in column, then -y option can be used.

Linux diff command

It is very easy to understand this output. Here > means add, | Means to change and < means delete.

READ  Top 5 Great Features of Pop OS Linux

In normal state, this command is “case sensitive”. If you want to make it “case insensitive”, you can use the -i option.

Use the –version option to find out which version of ‌ diff is installed on your computer.

[email protected]Ubuntu-PC:~/Documents$ diff --version
diff (GNU diffutils) 3.7
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later https://gnu.org/licenses/gpl.html.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Paul Eggert, Mike Haertel, David Hayes,
Richard Stallman, and Len Tower.

To know about all the options of diff command, you can read its man page by giving “man diff” command.

READ  5 things to consider before the upgrading RAM

Read More: Run Visual Studio Code in Cloud using Code Server

1 COMMENT

Comments are closed.

XnConvert: Best Batch Image Converter for Linux Windows & Mac

XnConvert is a cross platform batch image processing software with which we can not only change the format of multiple images in one click but also perform watermarking, special effects, border, image adjustment etc. in a one click. Available for Windows, Mac and Linux.

How to do voice typing in Google Docs and WordPress

Gone are the days when typing long documents either required the help of a typist or learning typing itself. Because now it is possible to type by speaking due to the development of artificial intelligence. In this post we will learn about various web based tools for Voice Typing.

How to save the command output in a file in a Linux terminal

In this post are going to discuss about how you can save the output of any Linux command in a file.

How to install Webmin on Fedora 33

Webmin is web based system administration software for Unix and Linux. Using webmin you can control and manage your server through web browser. You can also call it the GUI of Linux server. In this post we will learn how to install webmin in Fedora 33.

Get Linux Commands Cheat sheet using TLDR

Today we will discuss about TLDR. TLDR is a collection of "help pages" related to Linux commands maintained by communities that can be accessed by any client. This can become an alternative to traditional MAN pages. Because it is very easy to understand.

More Articles Like This

READ  5 Best Open Source Software You Should Try