There are an infinite number of situations in which you will be required to compare two files to see where they are the same or different. This is especially important when managing multiple configuration files in a private server. You can analyze each file yourself line by line, but that could take a ridiculously long time, and there’s now way to be sure whether or not your hard analyses introduced even more errors.
- How To Use The
- Practical Use Cases For Diff
How To Use The
In order to use the
diff command you will of course need to have SSH access to your server. These commands are all run from a command prompt.
Comparing Two Files
Classic example of a
diff <first-file> <second-file>
Here’s a working example, and it’s output:
diff first-file second-file
@@ -1 +1 @@ -This is the first file \ No newline at end of file +This is the second file \ No newline at end of file
Some terminal emulators will color the output to point out exactly what is different:
Useful and Interesting
Most interestingly, if you don’t need to know anything other than whether or not two files are identical, you can use the
-s option. For this example I have created two files called “cat” and “dog” which both have identical contents. Using the
-s option you can see they are exactly alike:
diff -s cat dog
The result is:
Files cat and dog are identical
If the files are not identical, the command will result in nothing.
In some cases, it can be helpful to paginate the output. As in the case of printing an output for review, you can have
diff paginate its output using the
diff -l cat dog
2021-06-15 10:32 diff -l first-file second-file Page 1 1c1 < This is the first file \ No newline at end of file --- > This is the second file \ No newline at end of file
Notice the output contains a header with date, files compared, and a page number. This command can be output to a command line printer program like
lpr and printed on paper.
For a complete listing of options be sure to visit the complete diffutils documentation.
Practical Use Cases For Diff
There are many practical use cases for applying
diff in real world situations. You can probably think of a dozen, but here are a few that you are sure to encounter at some point, and for which you could easily justify coming up with a
diff workflow today.
Server Configuration Files
Your server configuration is precious. Many mission-critical systems running inside your server depend on a few configuration files to make sure all services are tweaked properly and running efficiently. One little change in these configurations can cause a potential catastrophe.
There are instances in which other programs can write data to configuration files. If you find yourself in such an instance you can use
diff to compare a current configuration with a backup. Assuming that the configuration you backed up previous was working as expected, you can use
diff to compared current files with archived files to isolate the cause of system issues.
Files Edited by Other Users
Imagine that multiple users are accessing the same set of files. If someone changed a file, how would you know? You are sure to be aware of the error if something breaks, but that would be late. But with the
diff command you can check any mission critical files for changes and easily identify potential issues, or at least have an easier time triangulating the most likely cause of an error.