When you need to compare two files containing similar text in Linux, using the diff command can make your task much easier. The command compares two files to suggest changes that would make the files identical. Great for finding that extra curly brace that broke your newly updated code.
Diff Output Formats. Diff has several mutually exclusive options for output format. The following sections describe each format, illustrating how diff reports the differences between two sample input files. Sample diff Input: Sample diff input files for examples. Normal: Showing differences without surrounding text. Normally, to compare two files in Linux, we use the diff – a simple and original Unix command-line tool that shows you the difference between two computer files; compares files line by line and it is easy to use, comes with pre-installed on most if not all Linux distributions.
Using the diff command is very simple. Here's the syntax:
But understanding its output is a different thing. Don't worry, I'll explain the output so that you can compare two files and understand the difference between them.
- Diffuse is a small and simple text merge tool written in Python.With Diffuse, you can easily merge, edit, and review changes to your code. Diffuse is free software.
- Nov 05, 2019 It also has a merge capability, an integrated editor and the ability to diff/merge directories as well. KDiff3 works on Unix systems like Linux as well as Windows. Xxdiff is a graphical file and directory comparison and merging tool. You can compare.
Understanding the diff command in Linux
You need a couple of files to get started with. I've generated a list using a random word generator.
I added the list to two different files and then I modified the list by:
- Changing list order
- Adding Letters
- Switching case
I saved these similar files as 1.txt and 2.txt. Here's what they look like before you do anything.
I suggest that you follow the tutorial while reading so please create new files and add the following content to them.
Contents of 1.txt:
cobweb
locket
acoustics
expansion
record
Contents of 2.txt:
cobweb
LOCKET
acoustics
records
expansion
Example 1: Diff without options
Let's see what happens when you run the diff
command without any options.
Confused? You're not alone. The output is not exactly human-friendly. In order to understand what's going on, you need to know more about how diff works.
It may be helpful to know that when the analysis is done, file2 [in the syntax] is treated as the reference document that you are trying to match with. So, you may say that diff works in this way:
This also means you will get different output based on the order you place the file names in.
The order matters
An example of how the output differs depending on the file order:
Important Symbols in the diff command output
Using the table below as a reference, you can better understand what is happening in your terminal.
Symbol | Meaning |
---|---|
A | Add |
C | Change |
D | Delete |
# | Line numbers |
– – – | Separates Files in Output |
< | File 1 |
> | File 2 |
Let's take another look at the diff command output:
Explanation of the diff command output
Let's see the first difference in the output:
Output Line | Explanation |
---|---|
2c2 | The line 2 of file 1, CHANGE with line 2 of file 2. |
< locket — > LOCKET | Change 'locket' to 'LOCKET' to match file 2.txt |
Let's take a look at the next part of the output:
Output Line | Explanation |
---|---|
3a4 | After line 3 of file 1, add line 4 of the file 2. |
> records | That is to add 'records' to create the 4th line in file 1. So that file 1.txt will match file 2.txt |
Similarly:
Output Line | Explanation |
---|---|
5d5 < record | Delete the text 'record' from the 5th line of file 1. So that file 1.txt will match file 2.txt |
There is no spellchecking or dictionary function built into the command. It doesn't recognize 'record' and 'records' as being related. Its only goal is to perfectly match the two files.
Looking at the output, it is still pretty tough to translate. It is unlikely that you would save a lot of time.
Fortunately, there are options that can be added to make things more human-readable. Divinity original sin 2 legendary items. Let's look at a couple of different examples using the same list.
Example 2: Diff in 'Copied' context with -c
The context option gives a more visual representation over the more programmatic information displayed by default. Let's continue with our example text.
More important symbols in the diff command output
Symbol | Meaning |
---|---|
+ | Add |
! | Change |
– | Delete |
*** | File 1 |
– – – | File 2 |
Corel all products universal keygens by core appnee. It is much easier to understand when you see the information in this way. Instead of the alphanumeric output, the new set of symbols helps you to quickly identify the differences between the two files.
The output first displays the first file i.e. 1.txt and its line from 1 to 5. It says that there is a slight change in (part of) line 2 of the file 1.txt and (part of) line 2 of file 2.txt.
It also indicates that line number 5 of file 1 has been deleted (-) in the second file.
— 1,5 —- indicates the start of the second file and it says that line 2 is slightly changed from line 2 of file 1. It also indicates that line 4 has been added (+) in the second file and there is no corresponding line in file 1.
Example 3: Diff in 'Unified' context with -u
This option provides output similar to the copied context format. Instead of displaying the two files separately, it merges them together.
As you can see, it uses the same symbols as before, but instead of the change symbol, it suggests changes to be made using easy to read +
or -
symbols. Here, it recommends that you remove line 2 from 1.txt
and replace it with line 2 from 2.txt
.
Going forward, it also suggests that you add records after line containing acoustics and delete line record after the line containing expansion.
All these changes are suggested for the first file in the diff command. This is another scenario where it helps to remember that the diff program uses the second file listed as the ‘original' or basis for corrections.
For comparing a list like this, I personally find this method easiest to use. It gives you a clear visualization of the text that needs to be changed in order to make the files identical.
Example 4: Compare but ignore cases with -i
Case-sensitive searches are the default for diff but you can turn this off. Let's look at what happens when you do that.
Linux Compare Two Binary Files
- Changing list order
- Adding Letters
- Switching case
I saved these similar files as 1.txt and 2.txt. Here's what they look like before you do anything.
I suggest that you follow the tutorial while reading so please create new files and add the following content to them.
Contents of 1.txt:
cobweb
locket
acoustics
expansion
record
Contents of 2.txt:
cobweb
LOCKET
acoustics
records
expansion
Example 1: Diff without options
Let's see what happens when you run the diff
command without any options.
Confused? You're not alone. The output is not exactly human-friendly. In order to understand what's going on, you need to know more about how diff works.
It may be helpful to know that when the analysis is done, file2 [in the syntax] is treated as the reference document that you are trying to match with. So, you may say that diff works in this way:
This also means you will get different output based on the order you place the file names in.
The order matters
An example of how the output differs depending on the file order:
Important Symbols in the diff command output
Using the table below as a reference, you can better understand what is happening in your terminal.
Symbol | Meaning |
---|---|
A | Add |
C | Change |
D | Delete |
# | Line numbers |
– – – | Separates Files in Output |
< | File 1 |
> | File 2 |
Let's take another look at the diff command output:
Explanation of the diff command output
Let's see the first difference in the output:
Output Line | Explanation |
---|---|
2c2 | The line 2 of file 1, CHANGE with line 2 of file 2. |
< locket — > LOCKET | Change 'locket' to 'LOCKET' to match file 2.txt |
Let's take a look at the next part of the output:
Output Line | Explanation |
---|---|
3a4 | After line 3 of file 1, add line 4 of the file 2. |
> records | That is to add 'records' to create the 4th line in file 1. So that file 1.txt will match file 2.txt |
Similarly:
Output Line | Explanation |
---|---|
5d5 < record | Delete the text 'record' from the 5th line of file 1. So that file 1.txt will match file 2.txt |
There is no spellchecking or dictionary function built into the command. It doesn't recognize 'record' and 'records' as being related. Its only goal is to perfectly match the two files.
Looking at the output, it is still pretty tough to translate. It is unlikely that you would save a lot of time.
Fortunately, there are options that can be added to make things more human-readable. Divinity original sin 2 legendary items. Let's look at a couple of different examples using the same list.
Example 2: Diff in 'Copied' context with -c
The context option gives a more visual representation over the more programmatic information displayed by default. Let's continue with our example text.
More important symbols in the diff command output
Symbol | Meaning |
---|---|
+ | Add |
! | Change |
– | Delete |
*** | File 1 |
– – – | File 2 |
Corel all products universal keygens by core appnee. It is much easier to understand when you see the information in this way. Instead of the alphanumeric output, the new set of symbols helps you to quickly identify the differences between the two files.
The output first displays the first file i.e. 1.txt and its line from 1 to 5. It says that there is a slight change in (part of) line 2 of the file 1.txt and (part of) line 2 of file 2.txt.
It also indicates that line number 5 of file 1 has been deleted (-) in the second file.
— 1,5 —- indicates the start of the second file and it says that line 2 is slightly changed from line 2 of file 1. It also indicates that line 4 has been added (+) in the second file and there is no corresponding line in file 1.
Example 3: Diff in 'Unified' context with -u
This option provides output similar to the copied context format. Instead of displaying the two files separately, it merges them together.
As you can see, it uses the same symbols as before, but instead of the change symbol, it suggests changes to be made using easy to read +
or -
symbols. Here, it recommends that you remove line 2 from 1.txt
and replace it with line 2 from 2.txt
.
Going forward, it also suggests that you add records after line containing acoustics and delete line record after the line containing expansion.
All these changes are suggested for the first file in the diff command. This is another scenario where it helps to remember that the diff program uses the second file listed as the ‘original' or basis for corrections.
For comparing a list like this, I personally find this method easiest to use. It gives you a clear visualization of the text that needs to be changed in order to make the files identical.
Example 4: Compare but ignore cases with -i
Case-sensitive searches are the default for diff but you can turn this off. Let's look at what happens when you do that.
Linux Compare Two Binary Files
As you can see 'locket' and 'LOCKET' are no longer listed as suggested changes.
Example 5: Diff with –color
You can use –color to highlight changes in the diff command output. When the command is run, sections of output will be printed in different colors from the terminal palette.
Example 6: Quick analyze files with diff command options -s and -q
There are a couple of simple ways to check whether or not files are identical. If you use -s
it will tell you the files are identical or it will run diff as normal.
Using -q
will only tell you that the files 'differ'. If they do not, you will get no output.
Bonus Tip: Using diff command in Linux with large text files
You might not always be comparing such simple information. You may have large text files to scan and find differences in. I'll detail a few methods for handling this type of problem.
For this example, I created two files with big chunks of text (lorem ipsum). Each line has hundreds of columns. This obviously made comparing lines difficult.
When diff is run on a file like this, the output generates huge chunks of text and the symbols are difficult to see even with tools like the contextual output.
To conserve space, I took a screenshot of the output for you to look at.
Not very helpful, is it?
You can use some of the same concepts to analyze these kinds of files. They will not work well unless the file is properly formatted. Some large blocks of text do not have line breaks. You have probably encountered a file like this where you needed to enable 'Word Wrap' in order to get all the text to display within the allotted space without using a scrollbar. The reason this happens is some text formats don't create line breaks automatically. This is how you end up with the large chunks of text on only 2-3 lines. There is a pretty easy fix for this.
Use fold to wrap text into lines
This is Linux Handbook so, naturally, we have a solution for you and we can cram a mini tutorial in. There is a great write-up on fold (Unix) and fmt (GNU) here. I'll give a quick example that should be pretty self-explanatory though to move us forward.
The fold command is used to break lines using the number of columns. It can be customized to give you options on how these new line breaks are implemented. Auto macro recorder 5.84 keygen for mac.
In the example here, you are going to separate the file into a standardized width and use the -s
option. This tells the program to break ONLY where there is whitespace, not in the middle of text.
Use fold to quickly insert line breaks
With both files broken up into 31 lines instead of 3, you can compare them much more effectively. Here's an example of your output with the unified context filter.
Use diff with –minimal output
You can make this a little easier to read with the --minimal
tag. This makes larger text files a little easier to read. Let's have a look at the output.
You can combine any of these tips or use some of the other options listed in the diff man pages. This is a powerful and easy to use software utility.
I hope you found this article useful. If you have a tip, don't forget to leave us a comment and tell us about it.
Join the conversation.
The file comparison tools on this page are useful in a few ways. Firstly, they can tell you whether 2 files have identicalcontent or not. Secondly, they can also be used to pinpoint the exact differences between 2 files (especially ifthey are plain text files). Thirdly, for programmers, they can be used to create patch files that can beused to update one of those files to the other (useful for sending small updates to your end users withoutrequiring them to re-download everything).
Note for non-programmers: since you're probably just looking for something to tell you if 2 files arethe same, or to tell you the differences between 2 text files, just ignore any software below where I say thatthe tool is a binary diff or patch tool (ie, stuff like bsdiff and xdelta). Those are mostly meant for programmers.Also note that not all the utilities listed have a visual interface; some of them require you to use thecommand line, so if you're not comfortable with that, get a different one. (As far as possible, I've triedto note in my mini-review below if a particular program is command line based to make it easier for you,but if I miss something, and you find the program doesn't have a window for you to click on, just downloada different program. They're all free, after all.)
Related Pages
Free File Comparison Utilities (diff tools)
This is a free diff program that compares two text files (including Unicode ones) and displays the resultsside by side with the differences highlighted. Bmw f30 bulb sizes. This is a Windows .NET program.
The command line utility bsdiff looks for differences between two binary files (that is, files that are not plain text files)and creates a patch that can be used by bspatch to change (or update) one of the binary files to the other.Such binary diff tools (as they are called) are often used by software developers to deliver updated versionsof their software to their users without requiring their users to download everything all over again, just toupgrade to a new version. (They are also used for things likeversion control software.)In any case, bsdiff is known for its ability to create very small patch files. The program works on numeroussystems, including Linux, the BSDfamily, Windows and probably others. Source code is available.
WinMerge is a Windows file differencing and merging tool. It has a Windows frontend overthe GNU diff and merge utilities. It includes Visual SourceSafe integration, visual differencingof text files, the ability to diff entire directories, multiple level undos, an integratededitor,
xdelta is a command line tool that allows you to make binary diffs of two binary files. Actually, xdelta is apparently morethan that - it is, according to their web page, 'a complete system for managing delta-compressed storage and network transport'.The latest version supports VDIFF / RFC 3284 encoding and decoding.
This is a Java version of xdelta, a program that can make binary diffs oftwo files.
If you need to compare two text files and get a report of which lines have changed, theGNU command line diff utilities can show you those differences. It is a standard part ofUnix-type systems (like Linuxand FreeBSD, etc),but if you have an operating system that does not come with a usefulfile or text comparison utility, you can get the source code for the program here. Note thatthe utilities are command line based, and come only in source form, so you have to compile them yourself.
KDiff3 compares two or three text files and shows the differences between them line byline and even character by character. It also has a merge capability, an integrated editorand the ability to diff/merge directories as well. KDiff3 works on Unix systems likeLinux as well as Windows.
xxdiff is a graphical file and directory comparison and merging tool. You can compare two orthree files with the differences highlighted. You can also compare two directories (usefulif you are trying to synchronise two directories/disks). It also has the ability to mergefiles, unmerge CVS conflicts into two separate files so that you can resolve the conflicts,etc. It uses an external diff program that is compatible with GNU diff (presumably in itsinterface). xxdiff is released under the GNU GPL and runs on IRIX, Linux, HP/UX, and DEC Tru64.
ExamDiff shows you the difference between two ASCII text files (Unicode not supported) by displaying the two filesside by side with the changes in different colours. You can configure the colours used.
DiffMerge allows you to visually compare and merge files by graphically showing you the differences between them. Intra-line highlighting of differencesis supported along with support for editing the files. You can also perform a side-by-side comparison of 2 folders; the folder diff facility will showyou the files that are only present in one folder as well as distinguish between those that are identical and those that are different. It's Merge facilityallows you to see the changes between 3 files, automatic merging when safe, and the ability to edit the resulting file. The programsupports international character encoding, and works on Windows, Mac OS X and Linux. The Windows version features Windows Explorer integration.
Diffuse can compare an arbitrary number of files side by side. You can manually correct the output so that the matching lines arealigned as well as edit the files directly from the utility. It can retrieve files from a variety ofsource code version control management systemsincluding Bazaar, CVS, SVN, Darcs, Git, Mercurial, Monotone, etc, for comparison and merging. Other features include syntaxhighlighting, Unicode (UTF-8) support, unlimited undo, etc. The program requires that you havePython and PyGTK installed.
- How to Register Your Own Domain Name - get one for your software
- How to Register Your Own Domain Name - how to get your own domain name
Linux Binary File Diff
It will appear on your page as: