Comparing files (diff command)

Use the diff command to compare text files. It can compare single files or the contents of directories.

When the diff command is run on regular files, and when it compares text files in different directories, the diff command tells which lines must be changed in the files so that they match.

The following are examples of how to use the diff command:
  • To compare two files, type the following:
    diff chap1.bak chap1
    This displays the differences between the chap1.bak and chap1 files.
  • To compare two files while ignoring differences in the amount of white space, type the following:
    diff -w prog.c.bak prog.c
    If the two files differ only in the number of spaces and tabs between words, the diff -w command considers the files to be the same.