Grep is a command line tool that is most commonly used to find and match text content using patterns. You can use Grep patterns in a variety of ways to parse text data and get matches. One such Grep pattern allows you to view diff between two files. Assuming that there are two files “file1” containing 1 to 5 numbers on each line and and “file2” containing 1 to 10 numbers on each line, you can use the following grep command to view diff between these files:
After running the command stated above, you should get the following output:
The “F” switch in the command above considers text data as a list of fixed strings, each one of them separated by a line break or new line. The “x” switch matches only whole lines. The “v” switch is used to get inverse matches. You can use it to select non-matching lines. The “f” switch is used to get patterns from a file, each pattern being a new line. Note that the order of arguments is important when you use the diff command stated above. It finds those lines that exist in “file2” but do not exist in “file1”. Try running the command below by reversing the file order, you will get no output:
This is because “file1” doesn’t have any lines different from “file2”. Grep command is available by default in all major Linux distributions. For more information on Grep command, use the following two commands:
$ grep --help
Diff is a command command line utility that can be used to compare files line by line. Its output hints at lines that needed to be added or removed to match both files with each other. Using the same files mentioned in the example above, run the following command:
You should get the following output:
The output tells you that in order to match “file2” with file1, you need to delete (“d”) all lines starting from the 6th line to the 10th line to match the second file upto 5th line of the first file. Try reversing the command:
You should get the following output:
The output tells you that after the 5th line, add (“a”) 6 to 10 lines to “file1” to match it to “file2”. You can also do a side-by-side comparison using the “y” switch.
Diff command is available by default in most Linux distributions. For further information, you can run these two commands:
$ diff --help
Meld is a graphical application that can be used to find diff between two or more files and merge changes at the same time. It can also be used to recursively compare files in a directory, including folders under version control. Meld visually indicates what changes need to be made to match two files being compared. It can also show real time diff as you continue to edit one or both files.
To install Meld in Ubuntu, use the command specified below:
Meld is available in default repositories of most Linux distributions, so you can download it from package manager. You can also get more packages and source code from its homepage.
Kompare is a free and open source application that can be used to create diff between two files and merge changes to match their contents. Developed by the KDE team, It can also be used to recursively parse directories to compare differences between files. Kompare visually indicates diff between two files and you can also use it to create and apply patches.
To install Kompare in Ubuntu, use the command specified below:
Kompare is available in default repositories of most Linux distributions, so you can download it from the package manager. You can get more packages and source code from its webpage. A snap package is also available here.
Git Diff Command
Git is one of the most widely used and popular distributed version control systems. It comes with a lot of command line utilities, including a “diff” command that can be used to compare two files. You can use this command to compare two files extensively even if you are not using Git version control and it comes with numerous command line options to tweak the behavior of diff algorithms. In its simplest form, you can run this command to compare two files:
The colored output with “+” sign shows additional lines available in “file2”. To install Git and Git Diff in Ubuntu, use the command specified below:
Git Diff is part of the Git package and it is available in default repositories of most Linux distributions, so you can download it from the package manager. For further information on Git Diff, use the following two commands:
$ git diff --help
Kdiff3 is another GUI diff tool created by the KDE team. It comes with all bells and whistles you would expect from a diff tool. It can compare files and directories and automatically merge changes if chosen so. It also comes with a built-in editor and it visually indicates what changes need to be made to match the files and remove differences.
To install KDiff3 in Ubuntu, use the command specified below:
KDiff3 is available in default repositories of most Linux distributions, so you can download it from the package manager. You can get more packages and source code from its webpage.
These are some of the most useful command line and graphical utilities that can be used to compare multiple files. These diff tools provide a good way to track file changes, merge differences and create patches. You can also use them to manually revert files to their earlier state if need arises.