Find the difference between 02 files in Linux

To find the difference between 02 files in Linux, use the diff command. In nutshell, diff compares the contents of the two files from-file and to-file.Syntax: diff [options] from-file to-file2 The following options can be used with diff command:

-b Ignore changes in amount of white space.
-B Ignore changes that just insert or delete blank lines.
-i Ignore changes in case; consider upper- and lower-case letters equivalent.
–new-file In directory comparison, if a file is found in only one directory, treat it as present but empty in the other directory.
-r When comparing directories, recursively compare any subdirectories found.

Example:Suppose you have a file ‘file1.txt‘:
difference between 02 files in Linux

and another file ‘file2.txt‘:

difference between 02 files in Linux

Now, if you execute the diff command ‘diff file1.txt file2.txt‘, then it will display the output like this:

difference between 02 files in Linux
Lines “1d0” and “6a6” are the coordinates and types of the differences between the two compared files, while lines like “< aa” and “> kk” are the differences themselves. Diff change notation includes 2 numbers and a character between them. Characters tell you what kind of change was discovered:
a – line added

c – line changed

d – line deleted
Left number of the character defines the line number in the first file, and Right number of the character defines the line number in the second file.Output details:

1d0
< aa
It means that 1 line was deleted. < aa denotes that the aa line is present only in the first file.
3c2
< cc

> kk
It means that the line#3 has changed from “cc”[in first file] to “kk”[in second file].
6a6
> gg
It means that one new line added in the second file, it’s “gg” at line#7.

Leave a Reply

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