Main index

Introducing UNIX and Linux


Files

Overview
The UNIX directory hierarchy
Filesystems
Manipulating files
      Creating directories
      Creating files
      links
      'Dot' files
Protecting files
      Groups
      File access control
      Changing privileges
File contents
      Text files
      Comparing files
      Filtering files
      Non-text files
Printing files
File archives and file compression
Other relevant commands
Summary
Exercises

Comparing files

A situation that often arises is that you are examining a file, and you discover a very similar file, and need to know the differences between the two files. This can happen when a file has been edited several times, and you lose track of precisely what changes have been made. The command diff ('differences') will come to your aid. Use it followed by the names of two files and it will tell you the changes in the following manner. Suppose we have two files, file1 and file2, where file1 contains the following text:

A
test
file

and file2 contains

A
testing
file

then we would have

diff file1 file2
2c2
< test
--
> testing

indicating that line 2 containing myfile has been removed from file1 and replaced by a line containing testing. Related to diff is cmp ('compare'). Sometimes, especially within shell scripts, the verbosity of diff is not required, and a terse indication of whether or not two files are identical is required - cmp will give a short message if its two arguments are different, otherwise it will stay silent. Also, diff can only compare text files - cmp will compare two files of any type and indicate whether or not their contents are the same.


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck