Click here for "Safe Air Conditioner Repair and Proper Freon Recovery".

File comparison

File comparison

●Compare two files

To see if there is a difference between the two files, use the "cmp" command.
Specify the file you want to compare as an argument and run it.
If there is a difference, it will print the byte position and line number. Also, if there is no difference, the cmp command will not display any message, only a prompt.

[root@Lion ~]# cat country_list_001.txt ←Display the contents of a file
Japan
Afghanistan
Algeria
Andorra
Angola
Antigua and Barbuda
Argentina
Armenia
Australia
Austria
Azerbaijan
Bahamas
Bahrain
Bangladesh
Barbados
Belarus
Belgium
Belize
Benin
Bhutan
Bolivia
Bosnia and Herzegovina
Botswana
Brazil
Brazil Darussalam
Bulgaria
Burkina Faso
Burundi
Cambodia
Cameroon
Canada

[root@Lion ~]#cat country_list_002.txt ←Display the contents of a file
Japan
Afghanistan
Algeria
Andorra
Angola
Antigua and Barbuda
Argentina
Armenia
Australia
Canada
Austria
Azerbaijan
Bahamas
Bangladesh
Bahrain
Barbados
Belarus
Belgium
Belize
Benin
Bhutan
Bolivia
Bosnia and Herzegovina
Botswana
Brazil
Brazil Darussalam
Bulgaria
Burkina Faso
Burundi
Cambodia
Cameroon
Canada

Comparison of "country_list_001.txt" and "country_list_002.txt".
[root@Lion ~]# cmp country_list_001.txt country_list_002.txt
↑Compare the two files and display the results if there are any differences
country_list_001.txt country_list_002.txt different: bytes 90、line 10 ←difference in line 10

●Show the differences between the three files

On Linux, you can use the diff3 command to compare three files.
Specify the three files to be compared as the arguments of the diff3 command and execute it.

[root@Lion ~]# cat country_list_001.txt     ←Display the contents of the "country_list_001.txt" file
Japan
Afghanistan
Algeria
Andorra
Angola
Antigua and Barbuda
Argentina


[root@Lion ~]# cat country_list_002.txt       ←Display the contents of the "country_list_002.txt" file
Japan1
Afghanistan
Algeria
Bahrain
Angola
Antigua and Barbuda
Argentina
[root@Lion ~]# cat country_list_003.txt       ←Display the contents of the "country_list_003.txt" file
Japan2
Afghanistan
Algeria
Andorra
Angola
Antigua and Barbuda
Cambodia
country_list_001.txt  country_list_002.txt  country_list_003.txt    Comparison
[root@Lion ~]# diff3 country_list_001.txt country_list_002.txt country_list_003.txt
==== ←There are differences between all three files.
1:1c ←The only difference is in line 1 of the first file
Japan
2:1c ←The only difference is in line 1 of the second file。
Japan1
3:1c ←The only difference is in line 1 of the third file.。
Japan3
====2 ←There is a difference in the second file "country_list_002.txt".
1:4c
3:4c
Andorra ←The first and third files have the same contents.
2:4c
Bahrain ←The only difference is in  line 4 of the second file。
====3 ←There is a difference in the third file "country_list_003.txt".
1:7c
2:7c
Argentina ←The first and second files have the same contents.
3:7c
Cambodia ←The only difference is in line 7 of the third file.。
タイトルとURLをコピーしました