6.1 Directory and File Management Using the Command Line
6.1.6 Comparing files
Using the diff Command
The
diff (difference) command is used to compare two text files and find differences between them. The wc command can be used to compare files since it counts lines, words and bytes. It is possible for two files to have the same line, word and byte counts but have different characters and words. The diff command can actually find the differences between the files.
Command Format:
diff [option] file1 file2

The output of this command will display line-by-line differences between two text files. There are two options with the diff command; -i and -c.

  • The -i option ignores the case of the letters; for example, A is equal to a.
  • The -c option performs a detailed comparison and produces a listing of differences with three lines of context. With this option, the output begins with identification of the files involved and their creation dates.

Following a line of a dozen asterisks (*), the line numbers of file1 that are to be displayed are listed. The actual lines from file1 are then displayed, with a minus symbol (-) in front of those lines that are different from file2. The same display follows as it applies to file2, with a plus sign (+) for those lines that are different from file1. Figure shows the output from the comparison of the fruit and fruit2 files. Figures and show the output from the individual fruit and fruit2 files.

Interactive Media Activity (Flash, 95 kB)
  Using wc and diff

In this media activity, you are logged in as user2 and your current working directory is: /home/user2. Refer to the Class File Tree Structure by clicking the 'tree' button located on the Menu Bar and type the commands that would accomplish the requested objectives. 
Note: Be sure to press enter after each one. Click on step 1 to begin.

 

Lab Activity (Adobe Acrobat Reader, 51 kB)
  File Information Commands

In this lab, you will work with various informational commands. These are important because they allow you to investigate and discover information about files. You will use commands to help you determine what type a file is and what application created it. You will also work with several commands that let you see the contents of text files and compare them.