There are many GUI tools available to compare files & folders to check for any changes or dissimilarities, but it does not mean that one can’t perform comparison or diff using CLI on a Linux systems. In this tutorial, we will learn how to compare files in Linux systems.

Recommended Read: Important Docker commands for Beginners

Also Read: SSH server: Restrict SSH access on Linux


Compare files in Linux


Diff Command

First command that one can use to compare files in Linux is Diff Command. Diff command is available on almost all Linux distributions by default & can be used directly without any packages installation.

Syntax for using diff command is,

$ diff file1 file2

& it will present differences among the two files as output on the screen. Let’s see an example & a sample output for two files. We have two files with almost identical content with a few minor changes here & there, now let’s perform diff operation of these two files,

$ diff test1.txt test2.txt

& we get the following output telling the difference among the two files,

compare files in Linux

Similarly, we can perform the diff operation on two folders to see if there are any differences in the files stored on the two folders.

Note:- We can also compare more than two files with single command.

As we saw that diff command can perform the diff operations with ease but output can get pretty messy when handling big files. So to get rid of this issue we can use another CLI utility to compare files on Linux called VIMDIFF.

Vimdiff Command

Vimdiff command is part of vim command, so to be able to use vimdiff we need to have vim installed on our systems. Install vim by using the following command,

CentOS/RHEL/Oracle Linux

$ sudo yum install vim

Ubuntu/Debian/Linux Mint

$ sudo apt-get install vim

Fedora

$ dnf install vim

Syntax for using vimdiff command is similar to that of diff command, i.e.

$ vimdiff file1 file2

Or if comparing folders, use

$ vimdiff folder1 folder2

Now let’s see an example to see how output on vimdiff looks like,

$ vimdiff test1 test2

& we get the following output,

compare files in Linux

Looks pretty neat, right ? So let’s discuss what these different colored lines mean,

Red Colored Lines- Red color lines show that there are partial differences, i.e. only part of a line is changed & not the whole line.

Blue Lines & Hyphens-- These lines denote that complete line has been changed.

Lines with no difference appear as it is. For navigating the diffed files, we can use the following keyboard shortcuts,

]c : move to the next difference in the compared files,

[c : go back to previous difference in the file

dp : put the difference from current window to a new window

do : get the changes from another window to current window

CTRL-W + CTRL-W : to switch between the split windows

zo : Open the folded content of files

zc : close the folded content

:diffupdate : rescan the files for any changes

That’t it guys, we now end this tutorial on how to compare files in Linux. Please do send us your valuable comments or feedback using the comment box below.

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Donate us some of your hard earned money: [paypal-donation]

Linux TechLab is thankful for your continued support.