The diff command in Linux is used to compare the content of two text files line by line and display the differences between them. It is a handy tool for identifying changes made to files, such as configuration files, source code files, or any other text-based documents.
The basic syntax of the diff command is:
diff [options] file1 file2Where:
file1andfile2are the names of the two files you want to compare.
Here’s a table showing some common options of the diff command:
| Option | Description |
|---|---|
-q or --brief | Report only whether the files differ, without showing the actual differences. |
-r or --recursive | Recursively compare directories and their contents. |
-u or --unified | Output differences in a unified format, showing context lines. |
-c | Print a side-by-side comparison of the two files. |
-s | Suppress all output, except for the names of the files that differ. |
--ignore-case | Perform a case-insensitive comparison. |
--ignore-space-change | Ignore changes in the amount of white space. |
--help | Display help information for the diff command. |
Examples:
- Compare two files and display the differences in a side-by-side format:
diff file1.txt file2.txtThis command will show the differences between file1.txt and file2.txt, indicating which lines are unique to each file.
- Compare two files and show if they differ or not (brief output):
diff -q file1.txt file2.txtThis command will only report whether file1.txt and file2.txt differ or not, without displaying the actual differences.
- Compare two directories and their contents recursively:
diff -r directory1 directory2This command will recursively compare the contents of directory1 and directory2, showing differences between the files present in each directory.
- Compare two files in a unified format with context lines:
diff -u file1.txt file2.txtThis command will display the differences between file1.txt and file2.txt in a unified format, including some context lines to provide additional context for the changes.
- Ignore changes in the amount of white space when comparing files:
diff --ignore-space-change file1.txt file2.txtThis command will compare file1.txt and file2.txt, ignoring any changes in the amount of white space (spaces, tabs, etc.).
- Perform a case-insensitive comparison of two files:
diff --ignore-case file1.txt file2.txt
# Suppress all output, except for the names of the files that differ between the files file1.txt and file2.txt.
diff -s file1.txt file2.txt
# Generate a unified diff file that can be used to create a patch file for the differences between the files file1.txt and file2.txt.
diff -u file1.txt file2.txt > diff.patchThis command will compare file1.txt and file2.txt in a case-insensitive manner, treating upper and lower case characters as equal.
- Display help information for the
diffcommand:
diff --helpThis command will provide detailed information on the various options available with the diff command.
The diff command is useful for identifying changes between different versions of files and for reviewing modifications made to files by users or during software development. It allows you to quickly see the differences between text files and understand what changes have been made.