diff Command: Tutorial & Examples

Compare the content of files

The diff command is a Unix utility that compares the contents of two files or directories and displays the differences between them. It is commonly used to compare the contents of two versions of the same file or to see what changes have been made to a file over time.

The diff command compares the files line by line, and it displays the lines that are different between the two files. It does this by showing the line numbers of the changed lines and by displaying the lines themselves, with a symbol preceding each line to indicate whether it was added (+), removed (-), or unchanged (a space). For example, the output of diff might look like this:

1,3c1,3
< This is the first line of the first file.
< This is the second line of the first file.
< This is the third line of the first file.
---
> This is the first line of the second file.
> This is the second line of the second file.
> This is the third line of the second file.

In this example, the < symbol indicates a line that is present in the first file but not in the second file, while the > symbol indicates a line that is present in the second file but not in the first file. The c at the beginning of the output indicates that there were changes made to the lines listed.

The diff command has many options that allow you to customize its behavior, such as ignoring whitespace, ignoring case, and specifying the output format. It is a useful tool for comparing files and seeing the changes that have been made to them.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA