How do I Compare two files under Linux or UNIX?
You need to use diff command to display line-by-line difference between two files. The general syntax of diff command as follows:
diff FILE1 FILE2
Where,
FILE1 FILE2: Diff command will examine both file1 and file2 and tells you what changes need to be made for file1 and file2 to match. Please note that diff command point to which lines need be:
- Added (a)
- Deleted (d)
- Changed (c)
- Further lines in file1 identified with a less than () symbol and lines in file2 with a grater than (>) symbol.
Examples
diff file1.txt file2.txt
Output:
8c8,9 URL: www.nixcraft.in > Email: support@nixcraft.in
The contents of both files:
$ cat file1.txt
Output:
Welcome to nixCraft! If undeliverd return to nixCraft #404, DC bay area, 2nd phase, Pune. Ph: 555-11112223 URL: www.nixcraft.com
$ cat file2.txt
Output:
Welcome to nixCraft! If undeliverd return to nixCraft #404, DC bay area, 2nd phase, Pune. Ph: 555-11112223 URL: www.nixcraft.in Email: support@nixcraft.in
Side-by-side merge of file differences
You can get a clear-cut visual difference between two text files using the command sdiff:
$ sdiff file1.txt file2.txt
Output:
Welcome to nixCraft! Welcome to nixCraft!
If undeliverd return to nixCraft If undeliverd return to nixCraft
#404, DC bay area, 2nd phase, #404, DC bay area, 2nd phase,
Pune. Pune.
Ph: 555-11112223 Ph: 555-11112223
URL: www.nixcraft.com | URL: www.nixcraft.in
> Email: support@nixcraft.in
Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
Related Other Helpful FAQs:
- Linux: How do I find out causes for memory faults?
- Search all the Linux man pages for a particular command or text
- Solaris add a new swap file for database
- Linux / UNIX Display Lines Common in Two Files
- Do I need antivirus software for linux?
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!


Recent Comments
Today ~ 1 Comment
Today ~ 3 Comments
Today ~ 57 Comments
Yesterday ~ 9 Comments
Yesterday ~ 7 Comments