Comparing Documents In Text File
UltraCompare is a file and folder compare utility that works for text files, Word documents, zip files, and jar archives. In addition to local/network directory compare, it also supports FTP compare. It’s available for Windows for $49.95. With the Copyleaks compare documents tool, you can compare documents of these types: Compare online content by submitting urls; Compare raw text – simply type in your text in a text box; Compare text types: Compare text file types: html, txt, pdf, doc, docx, rtf. You can compare two similar PDF documents (e.g., two versions or revisions. The comparison result is displayed in a temporary file that you can save if required. Choose a comparison type: View visual and text differences (Visual) or View.
This guide will show you how to use Linux to compare two files and output their difference to the screen or to a file.
You do not need to install any special software in order to compare files using Linux but you do need to know how to open a terminal window.
As the linked guide shows there are many ways to open a terminal window using Linux. The simplest is to press the CTRL, ALT and T keys at the same time.
Creating The Files to Compare
In order to follow along with this guide create a file called 'file1' and enter the following text:
You can create a file by following these instructions:
Open the file by typing the following command: nano file1
Press CTRL and O to save the file
Press CTRL and X to exit the file
Now create another file called 'file2' and enter the following text:
You can create a file by following these instructions:
Open the file by typing the following command: nano file2
Type the text into the nano editor
How to Compare Two Files Using Linux
The command used within Linux to show the differences between 2 files is called the diff command.
The simplest form of the diff command is as follows:
Comparing Documents In Text File Free
If the files are the same then there will be no output when using this command, however, as there are differences you will see output similar to the following:
Initially, the output may seem confusing but once you understand the terminology it is fairly logical.
You can see that the differences between the 2 files are as follows:
- The second file only has three lines whereas the first file has 4.
- The second file says '1 green bottle' on the third line whereas the first file says 'one green bottle'
- The second file says 'there'd' instead of 'there would' on the final line
The output from the diff command shows that between lines 2 and 4 of the first file and lines 2 and 3 of the second file there are differences.
It then lists the lines from 2 to 4 from the first file followed by the 2 different lines in the second file.
How to Just Show If the Files Are Different
If you just want to know if the files are different and you aren't interested in which lines are different you can run the following command:
If the files are different the following will be displayed:
If the files are the same then nothing is displayed.
How to Show a Message If the Files Are the Same
Comparing Documents In Text File Free
When you run a command you want to know that it has worked correctly, so you want a message to be displayed when you run the diff command regardless as to whether the files are the same or different
In order to achieve this requirement using the diff command, you can use the following command:.
Now if the files are the same you will receive the following message:
How to Produce the Differences Side by Side
If there are lots of differences then it can very quickly become confusing as to what the differences actually are between the two files.
You can change the output of the diff command so that the results are shown side by side. In order to do this run the following command:
The output for the file uses the symbol to show a difference between the two lines, a < to show a line that has been removed and a > to show a line that has been appended.
Interestingly if you run the command using our demonstration files then all the lines will show as different except for the last line of file 2 which will be shown as having been deleted.
Restricting the Column Widths
When comparing two files side by side it can be hard to read if the files have lots of columns of text.
To restrict a number of columns use the following command:
How to Ignore Case Differences When Comparing Files
If you want to compare two files but you don't care whether the case of the letters is the same between the two files, then you can use the following command:
How to Ignore Trailing White Space at the End of a Line
If when comparing the files you notice loads of differences and the differences are caused by white space at the end of the lines you can omit these as showing up as changes by running the following command:
How to Ignore All White Space Differences Between Two Files
If you are only interested in the text in a file and you don't care whether there are more spaces in one than the other you can use the following command:
How to Ignore Blank Lines When Comparing Two Files
If you don't care that one file may have extra blank lines in it then you can compare the files using the following command:
Summary
Diff
You can find more information by reading the manual for the diff command.
The diff command can be used in its simplest form to just show you the differences between 2 files but you can also use it to create a diff file as part of a patching strategy as shown in this guide to the Linux patch command.
Another command you can use to compare files is the cmp command as shown by this guide. This compares files byte by byte.