Comparing files

A comparison between two similar files makes sense in order to know what differences exist between the two files. For example, comparing the results obtained by a command ran on two sets of data. Another example can be comparing an older and a newer version of a shell script file in order to know what modifications have been made in script. Shell provides the diff command for file comparison.

Files comparison using diff

The diff command is used to compare files line by line. The syntax of using the diff command is as follows:

diff [option] file1 file2

Where, file1 and file2 are the files to be compared.

The options of the diff command are explained in the following table:

Option

Description

-q

Only print if files differ

Get Linux Shell Scripting Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.