diff (A Linux Command)

diff is one of my favorite Linux commands, which is very useful. What does it do? It does compare two text documents and shows their differences if there are any. The syntax is as simple as

diff textfile1 textfile2

and so it is easy to use even if you are not so familiar with command lines. Here is an example.

I am currently working on my old MacBook, so you see an Apple terminal in the above image. But diff is a common Linux command for any Linux distribution, of course, including Ubuntu. I have two files named caesar.py and caesar2.py in my ~/Desktop/python folder. I ran diff command to compare these two. What the output says is that caesar.py has the following two lines:

# every possible symbol that can be encrypted
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

that caesar2.py does not have. Pay attention to the direction of the arrow <. This is particularly useful when you are programming. Suppose that you have a backup of the original code say caesar.py and you are editing its copy caesar2.py. Somehow you deleted or incorrectly altered certain lines by mistake and you couldn’t identify your mistakes. In that case, you can solve the issue by simply running the command diff as shown in the example.

This entry was posted in Linux, macOS. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *