Sunday, 1 March 2015

Find difference between 02 files in linux

To compare two text 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
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': 
and another file 'file2.txt':
Now, if you execute the diff command 'diff file1.txt file2.txt', then it will display the output like this:
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.


No comments:

Post a Comment