Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Compare Linux Files and How To Do It With The 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 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

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

#!/bin/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
# 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
fi
			

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.



This post first appeared on Linuxman Tech, please read the originial post: here

Share the post

Compare Linux Files and How To Do It With The DIFF Command

×

Subscribe to Linuxman Tech

Get updates delivered right to your inbox!

Thank you for your subscription

×