Using diff Tool to Quickly Find Differences Between Files

Hesam Gholami
3 min readMay 14, 2021

You may come across the issue where you want to compare two files with each other and see the difference between them.

You can use bunch of graphical interfaces like Meld for comparing and even merging files.

Even many text editors and IDEs offer built-in or plugin-based diff tools.

But in this article I want to talk about diff tool which you can use in terminal. diff is a very simple command line utility which can be used to quickly view difference between files in terminal.

Specially this tool can be useful for developers, but all kind of users can use diff.

One of the main situations where diff can be useful, is where you maybe log in to your web server which has no desktop environment or no window manager. So a terminal based tool is really helpful to find differences between files.

In this article we assume we have two files named file1.cpp and file2.cpp. We use these files to demonstrate diff tool.

Content of file1.cpp:

file1.cpp

And here is content of file2.cpp:

file2.cpp

You can see there is a slight difference between these two files. Now we are going to find the difference between these two files using diff.

That’s easy, we can pass both file names to diff to find out the difference:

Diff between two files in terminal
Normal diff between two files in terminal

You can see the command is simple as diff ./file1.cpp ./file2.cpp . This will show us a normal difference, but there is a lot of options that we can use to tweak the output.

For example we can use -u option for a unified output:

Unified diff in terminal
Unified diff between two files in terminal

We can also do a side-by-side diff on these files using -y:

Side-by-side diff between two files in terminal

You can find more about diff options using diff --help or this trick.

Conclusion

Now you can see how diff tool could be useful for some simple comparisons quickly on terminal.

I hope you enjoy this article and have fun!

--

--

Hesam Gholami

Rustacean 🦀, C++ and Scala ninja by day and JS/TS guru by night, developer since 2011, FOSS lover, creator of puresoftware.org