Diff is a command that can be used when you want to see the difference between two files on the Linux command line.
Understanding the output of the diff command can be tricky if you are a beginner. Highlighting changes in a different color will help you easily notice the changes.
You can color the diff output using the –color parameter as follows:
diff --color file1.txt file2.txt
This will print the diff color output in different colors depending on the colors of your terminal palette.
Always use color output for differences
Typing –color every time you run diff is tedious. If you want to always see the colored diff output, you can easily use an alias in Linux.
alias diff="diff --color"
To make this alias permanent, add the above alias command to your ~ / .bashrc file (assuming you are using bash command).
If you are lazy, you can use the echo command to add an alias to the end of your .bashrc file:
echo "alias diff="diff --color" " >> ~/.bashrc
However, you should use a terminal based text editor like Vim or Nano to edit the file and add all your custom aliases in one place.
After adding the alias to your ~ / .bashrc file, use the source command for the changes to take effect immediately:
Don’t you have the –color option with diff? Here’s What You Can Do!
The –color parameter was included in diff in version 3.4. If you see an error message when using this option with the diff command, you should first check the version of the diff command:
It should show the following output:
[email protected]:~$ diff --version diff (GNU diffutils) 3.7 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Paul Eggert, Mike Haertel, David Hayes, Richard Stallman, and Len Tower.
If you have diff version 3.3 or earlier, you will have to resort to other ways of getting color output using the diff command.
You can use a handy utility called colordiff. Basically, it is a wrapper around the diff command written in Perl.
sudo apt install colordiff
Once installed, you can use it in the same way as the diff command.
colordiff file1.txt file2.txt
Not so difficult, not so difficult. Is not it?
There is a handy utility ydiff that allows you to view the differences between files in parallel.
We hope you find this little Linux tip helpful. Stay tuned for updates and stay subscribed to find out more.