Imagine waking up one day to find out that your production systems are down because of a bug that has yet to be traced. One of your worst nightmares right?
And you also discover that you need to compare code from two versions and the pressure is building to restore the systems. Everyone is panicking and it's totally legit!
Luckily, there is a Linux utility called
diff that has got your back.
What is the
diff command in Linux?
Comparing files and finding the differences between them is a widely used operation. This is specially useful when you have to compare complex code or configuration files.
Instead of comparing manually (which has a high chance of human error), Linux gives you a built in and powerful utility called
diff. It also saves time.
To compliment diff commands, Linux also provides another command to apply changes from one file to another called
patch . In this article, we'll be looking into these interesting and versatile commands to see how to use them.
diff command syntax
The syntax for
diff is shared below:
The diff command can show three characters based on the changes:
|c||CHANGE- A change needs to be done.|
|d||DELETE- Something should be deleted.|
|a||ADD- Something needs to be added.|
In the output of the
diff command, the symbol
< points to the first file and the symbol
> points to the second file which is used as a reference.
Let's see some examples of the
diff command in use.
Examples of the Linux
To state that files are the same, we use the flag
diff. In our example, the two files fileA and sameAsfileA contain the same content.
In the next example, there are two files that don't have the same contents. In the output highlighted below, the
diff command shows that lines 11 and 14 in showList_v2.js should change to match lines 11 and 13 in showList_v1.js.
The next way you can use diff is my favorite, as you can see differences side by side.
Just use the
-y flag like this:
diff -y file1 file2
The last example I am going to discuss is unified output. This output is often used as input to the
patch command. We'll see how the patch command works as well:
Below are some other useful flags you can use with
-ito ignore case.
diffis case sensitive by default.
-wto ignore whitespaces in a file. By default whitespaces are considered a difference.
patch Command Syntax
Changes happen all the time in your code, and it is unrealistic and time-consuming to share edited and fixed files for each change. Usually devs share fixes in the code with the team so they are applied instantly.
And using patches is the safest method to distribute improvements only.
Let's see how patching works:
Examples of the Linux
However, there is something wrong in the print function and we need a fixture for that. We send the file print_in_js.js to our colleague who fixes the code and sends it back.
First, our colleague is able to find a type in line #3. They correct the file.
Once file is corrected, and the code is functional, they create a patch.
Let's review the contents of the patch:
Once we have the patch, we apply it as follows:
And yes – our code is fixed!
It is relatively simple and straightforward to create and apply patches using
A similar approach works when you're using version control systems like Git or SVN. Learning the basics really helps you transition to and understand how version control works, which is an important aspect of software development.
Thanks for reading until the end. I would love to connect with you. You can find me here on twitter. Do share your thoughts.
See you around.