DEV Community

Hosein Esmailpour
Hosein Esmailpour

Posted on

Comparing text files with diff command

How would you check the difference between two files with same code? open both files at the same time in editor and look at the each line of code and looking for differences! Maybe you can spot the differences but its not super obvious. its complex and super-prone.
Here is a command called diff which compare the contents of files and write to the standard output the list of changes necessary to convert one file into the other. diff stands for difference and is a Unix base operating system command.
To compare two files, use the following syntax:
diff [option] file1 file2
Now lets take a look at 2 Python files using cat command:
Image description
Can you find the difference? Lets use diff command to find difference:
diff file1.py file2.py
Image description
The less than and greater than symbol tells about removed and added to file.
‘c’ means changed. if you saw ‘a’ it means added and ‘d’ stands for delete.
You can use ‘-u’ flag to show the differences in unified format:
Image description
using the minus sign to mark lines that were removed and the plus sign to mark lines that were added.
There are many tools to compare files, diff is the most popular one. you may also use wdiff, meld KDiff3 or vimdiff.

Top comments (0)