How to Use Diff Unix Command

In this tutorial we will show you how to use the diff unix command.
The diff command is simply used to compare the data of two files. What this command does is that it prints the lines from the two files which are different.
To understand more about the unix commands of diff follow the steps given below.



Step 1 – List the directory contents
First, let’s list the contents of the directory. With that done, you can see two text files over here.
View the contents of the directory

Step 2 – Enter command to see difference in both files
Now type the command “diff text-output.text text.txt“
With that done you will see the difference in both the files has been printed on the terminal screen.
Insert command to view difference in both files

Step 3 – Check for directories
Now we will show you how the check for the directories using the diff command.
For that, type the command diff dir dir2 and hit enter.
This will compare the directory named “dir” with another directory named “dir2”.
Once we hit enter, you can see a message printed on the screen saying “Only in dir1: 5”
This means that the file number 5 is only present in dir1.
Enter command to check for directories

Step 4 – View dir1
To confirm that, let’s check manually the difference in the two directories. For that, we will enter in the first directory and list the contents over here.
You can see that all the files 1 till 5 are present in this directory.
Open dir1 contents

Step 5– View dir2
Now let’s move into dir2 and list the contents over there. With that done, you can see that only the files 1 till 4 are present in this directory. This goes to show that the diff unix command had successfully printed the difference in the two folders, that the fifth file was only present in the first directory.
And that’s it, from the above tutorial you can learn about the diff commands of diff in Linux.
List contents of dir2