Learn The DIFF Command In Linux With 3 Easy Steps

Table of Contents

File management on Linux distributions can be accomplished in many ways. But today we want to compare some files with a simple terminal tool that you likely have on your distribution right now, DIFF. The DIFF command in Linux is a rather robust tool with many options. Today we will scope it down to options that will help us most. DIFF was originally authored by Douglas McIlroy at AT&T Bell Laboratories. Since 1974 many developers have joined the fray. Now that we have a little background let’s jump in.

DIFF Command Basics

To learn the basic use of DIFF options, all we need do is run it in the terminal with the help flag like so.

					diff --help

Notice that you have so many options that it might be a little overwhelming. The important thing is that you try out some new commands with some help. This way you can get comfortable with the tool. After all, DIFF can really be a great way to script out some task that may be repetitive otherwise, comparing against variables. So let’s just break down what the most common options might be with some real world scenarios.

Common DIFF Options

First thing we want to do is make some files to compare so that we do not have any difficulty with our first time. So let’s go ahead and make a directory to place these files while we are at it.

					mkdir ~/test && cd ~/test && echo "this is a test #1" > test_dock-00.txt && echo "this is a test #2" > test_dock-01.txt 

Now we have two documents with slightly different characteristics to compare. Now we can go ahead and do that like this.

					diff test_dock-00.txt test_dock-01.txt

This should give a simple output that will confirm your result are correct.

Now we can try out a first option -q. Let’s only show if the files differ, no other details.

					diff -q test_dock-00.txt test_dock-01.txt

This could be useful if in fact we need to script an operation to only act if the files versions differ. To see the file’s contents with a bit more context, you can also use the -c option here.

					diff -c test_dock-00.txt test_dock-01.txt

That should give you a more context rich readout like this.

Now you might want to compare some files side-by-side. We can accomplish this with the -y option.

					diff -y test_dock-00.txt test_dock-01.txt

The output should give you a nice side-by-side comparison like so.

Now we should be able to compare files in terminal or in a scripting manner to get some more complicated work done.

Scripting With DIFF

To master more simple Unix philosophy applications could really change your work, hobby and in some cases be exactly what you were looking for. To clean up our mess, why not use diff in a script to help us determine what to delete, then execute. Using what ever editor you like.

					# diff-script.sh


# compare files with diff  
if diff test_dock-00.txt test_dock-01.txt > /dev/null ; then
# if files match print "The files match"
  echo "The files match"
# else remove the files with the rm command and print "The files don't match"
  echo "The files don't match"
  rm test_dock-00.txt test_dock-01.txt

The output should look like this.

Now you should have no more test files in your test directory. Thanks for following along and keep coming back for your Linux related information. Thanks for reading.

Meet the Author

Leave a Reply