Friday, September 2, 2016

compare two files in Linux\Unix

 

We can use the diff cmd to compare files on Linux\Unix machines.



atoorpu@Linux01:[~/tmp] $ cat t1.txt
abcd
efgh
ijkl
mnop

atoorpu@Linux01:[~/tmp] $ cat t2.txt
abcd2
efgh
ijkl12
mnop


atoorpu@Linux01:[~/tmp] $ diff -y t1.txt t2.txt
abcd                                                          | abcd2
efgh                                                            efgh
ijkl                                                          | ijkl12
mnop                                                            mnop


[option] -y     :    Use the side by side output format.


you can also set width to print columns using the -W, --width=NUM option:

atoorpu@Linux01:[~/tmp] $ diff -y -W 20 t1.txt t2.txt
abcd  | abcd2
efgh    efgh
ijkl  | ijkl1
mnop    mnop

To ignore the case sensitivity between two files use -i :

atoorpu@Linux01:[~/tmp] $ diff -y -i t1.txt t2.txt
ABCD                                                          | abcd2
EFGH                                                            efgh
ijkl                                                          |
                                                              > ijkl12
mnop                                                            mnop

To ignore white spaces between two files use --ignore-all-space :

ADDING SOME SPACE TO T1 IT LOOKS LIKE THIS:
atoorpu@Linux01:[~/tmp] $ pr -m -t t1.txt t2.txt
AB CD                               abcd2
EF GH                               efgh
ij kl
mnop                                ijkl12
                                    mnop



To compare two directories you can use :

atoorpu@Linux01:[~/tmp] $ diff -y /home/atoorpu/tmp/A /home/atoorpu/tmp/B
diff -y /home/atoorpu/tmp/A/t1.txt /home/atoorpu/tmp/B/t1.txt
AB CD                                                           AB CD
EF GH                                                           EF GH
ij kl                                                           ij kl
mnop                                                            mnop
Only in /home/atoorpu/tmp/A: t2.txt


reference for diff cmd     :  http://linux.about.com/library/cmd/blcmdl1_diff.htm

No comments:

Post a Comment