Skip to content

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:
-bIgnore changes in amount of white space.
-BIgnore changes that just insert or delete blank lines.
-iIgnore changes in case; consider upper- and lower-case letters equivalent.
–new-fileIn directory comparison, if a file is found in only one directory, treat it as present but empty in the other directory.
-rWhen comparing directories, recursively compare any subdirectories found.
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:
< aa
It means that 1 line was deleted. < aa denotes that the aa line is present only in the first file.
< cc

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


You may want to see
How to sort contents of a file

The sort command is used to sort the contents of a file. You can sort the data in a text Read more

How to use sed command in Linux

sed, short for "stream editor", allows you to filter and transform text. A stream editor is used to perform basic Read more

How to use crontab in Linux

Cron is the system process that will automatically perform tasks for you according to a set schedule. The schedule is Read more

How to extract archive file in Linux

To extract archive file in Linux, you can use the tar command, used for extracting content from the archive files. "tar" Read more

Leave a Reply

Your email address will not be published.