Home / Software / How to compare two files in Linux

How to compare two files in Linux

diff command
Question: I have two scripts in a directory on my server. One is the right one and one is not, but I do not remember which is which. How can I compare the two files?

Answer: Linux and UNIX-like operating systems usually come with a comparison command called “diff”. This command will display line-by-line differences between two files. It is useful for software developers, but web developers and web application managers will find it useful as well.

To use it, just enter:

diff script1.php script2.php

It will search through both PHP scripts and find any lines that are different. The output will look something like this:

header('HTTP/1.0 404 Not Found');
header('Location: ../../');
> exit();

In the first file “exit();” is not present, while the second file correctly contains it. Tomorrow we will learn how to merge the two differing files into one correct file.

Check Also

Server collocation

Server colocation

Server colocation is an incredible good choice for people as well as small businesses who …