By : Robert Christopher, Priyanka Catherine Lobo
Introduction
The 'diff' command compares files line by line. This command has a whole range of switches that can be used to compare only elements that are required for us. On the other hand, the 'find' command is used to search for files in a directory, evaluating the filename from left to right.
Example
Let's assume, we have two computer programmers trying to solve sum of two numbers problem simultaneously using two files (sumone.c and sumtwo.c) which are stored in one folder (assignment). We can use the diff / find command of the UNIX shell for controlling the version and also to track changes in the source code file. Although both the commands have similar feel, they are different. We shall first look at examples on the ‘diff’ command and later some examples on the ‘find’ command.
Diff Scenario 1: To see the difference in two files (sumone.c and sumtwo.c).
robertadmin@Robert:~/assignment$ diff sumone.c sumtwo.c
Output: Shows the general differences between sumone.c and sumtwo.c
Diff Scenario 2: To find the author’s name of the files.
robertadmin@Robert:~/assignment$ diff sumone.c sumtwo.c|grep Author
Output: Gives only the Author’s names after extraction from the file.
Diff Scenario 3: To view the files side by side for changes.
robertadmin@Robert:~/assignment$ diff -y sumone.c sumtwo.c
Output: Shows the files side by side for viewing the changes visually.
Diff Scenario 4: To check if difference occurs between two files.
robertadmin@Robert:~/assignment$ diff -q sumone.c sumtwo.c
Output: Gives a quick check of the occurrence of differences in files.
Find Scenario 1: To find the files in a directory (assignment).
robertadmin@Robert:~$ find ./assignment
Output: Shows only the files in the assignment directory.
Find Scenario 2: To find specific type of files.
robertadmin@Robert:~/assignment$ find -type f
Output: Gives only regular type files from the assignment directory.
Find Scenario 3: To find specific file names.
robertadmin@Robert:~/assignment$ find -iname *one.c
Output: Gives all the files ending with one.c case insensitive.
Find Scenario 4: To find the files that are newer than a given file.
robertadmin@Robert:~/assignment$ find -newer sumone.c
Output: Gives the files which were modified more recently than sumone.c file.