#Modules used
use CGI qw(param);
#You should only need to change the directory to start in and maybe the scriptname (if you changed it).
my $dir = "/var/www/html/blog";
my $script = "diff_files.pl";
my @devices = `ls $dir`;
my $query = param('device');
my $file1 = param('file1');
my $file2 = param('file2');
my $diff;
my @filelist;
print <<END_of_Start;
Content-type: text/html
<HTML>
<HEAD>
<TITLE>Find the differences on files</TITLE>
</HEAD>
<BODY>
END_of_Start
if ($query eq "") {
print"
<p>Select folder where you would like to compare files on</p>\n
<FORM ACTION=\"$script\">
<TABLE><TR><TD>Choose a folder</TD></TR><TR><TD><SELECT NAME=device> ";
foreach $device (@devices) {
chomp $device;
print "<option value=$device>$device</option>";
}
print "</SELECT></td></tr><TR><TD><INPUT TYPE=submit></TD></TR></table></form>";
}
elsif ( ($query =~ /\S+/) && ($file1 eq "") ) {
@filelist = `ls $dir/$query`;
print "<p>Device $query, choose the files to compare</p>
<FORM ACTION=\"$script\">
<TABLE><TR><TD>
<SELECT NAME=file1>";
foreach $filelist (@filelist) {
chomp $filelist;
print "<option value=$filelist>$filelist</option>";
}
print "</SELECT></TD><TD>
<SELECT NAME=file2>";
foreach $filelist (@filelist) {
chomp $filelist;
print "<option value=$filelist>$filelist</option>";
}
print "</SELECT></TD></TR><TR><TD><INPUT TYPE=submit><INPUT TYPE=hidden name=device value=$query></TD></TR></table></form>";
}
elsif ( ($file1 =~ /\S+/) && ($file2 =~ /\S+/) ) {
print "<p>comparison of $file1 and $file2</p>";
$diff = `diff -bys $dir/$query/$file1 $dir/$query/$file2`;
#Clean up results
if ($diff =~ /are identical/) {
$diff = "Files are identical.";
}
print "<TABLE width=750><PRE>$diff</PRE><a href=\"$script\">GO BACK</a></TABLE>";
}
This is a script to compare two files and find the difference or diff on them in a web based format. The script uses perl cgi and outputs a side by side comparison of the 2 files, if they are the same the script will report that. If they are different you will see the first file in the left column and the second file in the right with the changes marked, this is handy for a quick diff on large directories or to allow people to compare files without having shell access.