How to use the command `diff` (with examples)

How to use the command `diff` (with examples)

The diff command in Linux is used to compare files and directories. It shows the differences between two sets of files by displaying added, modified, and deleted lines. The command is quite versatile and can be used to compare files, directories, and even create patch files for version control systems like Git.

Use case 1: Compare files (lists changes)

Code:

diff old_file new_file

Motivation: The diff command is commonly used to compare two versions of a file and identify the changes made between them. This is useful when tracking modifications in code files or when comparing different versions of a document.

Explanation:

  • old_file: The original file to be compared.
  • new_file: The modified file to be compared.

Example output:

1,3c1,3
< Line 1
< Line 2
< Line 3
---
> New Line 1
> New Line 2
> New Line 3

The output shows the differences between the two files, where < denotes lines present in the old_file and > denotes lines present in the new_file.

Use case 2: Compare files, ignoring white spaces

Code:

diff --ignore-all-space old_file new_file

Motivation: In some cases, the differences between files may be due to whitespace changes. By ignoring all whitespaces, the diff command can focus on the substantive changes, making it easier to identify the significant modifications.

Explanation:

  • --ignore-all-space: Ignores all white space changes between lines when comparing files.

Example output:

1c1
< Line 1
---
> Line  1

In this example, the only difference between the two files is an extra space character in the new_file. However, since we have used the --ignore-all-space option, the command has identified this as a minor difference and focused on the substantive change.

Use case 3: Compare files, showing differences side by side

Code:

diff --side-by-side old_file new_file

Motivation: Side-by-side comparison is useful in visually comparing two files, as it presents the differences in a clear and organized manner. This approach makes it easier to see the changes made to individual lines.

Explanation:

  • --side-by-side: Displays the differences in a side-by-side format, with changes in each file shown side by side.

Example output:

Line 1                                       Line 1
Line 2                                       New Line 2
Line 3                                       Line 3

The diff command presents the differences side by side, helping users easily identify changes in the two files.

Use case 4: Compare files, showing differences in unified format

Code:

diff --unified old_file new_file

Motivation: The unified format is commonly used in version control systems like Git. By displaying differences in this format, the diff command allows users to easily understand and apply the changes as patches to their files.

Explanation:

  • --unified: Shows differences in a unified format, similar to the output produced by git diff.

Example output:

--- old_file
+++ new_file
@@ -1,3 +1,3 @@
-Line 1
-Line 2
-Line 3
+New Line 1
+New Line 2
+New Line 3

The output contains a header indicating the files being compared, followed by the unified difference. Each change is preceded with a line starting with either - (indicating a removal) or + (indicating an addition).

Use case 5: Compare directories recursively

Code:

diff --recursive old_directory new_directory

Motivation: Comparing directories can be helpful when identifying changes made to multiple files within a directory structure. By using the diff command with the --recursive option, it becomes possible to find both differing files and changes made to files recursively.

Explanation:

  • --recursive: Compares directories recursively, including subdirectories.

Example output:

diff old_directory/file1 new_directory/file1
1c1
< Line 1
---
> Modified Line 1

diff old_directory/subdirectory/file2 new_directory/subdirectory/file2
1c1
< Line 1
---
> Modified Line 1

The output shows the differences between files within the directories and subdirectories being compared.

Use case 6: Compare directories, only showing differing file names

Code:

diff --recursive --brief old_directory new_directory

Motivation: Sometimes, it is only necessary to know if files differ between directories, without displaying the detailed changes. The --brief option allows for a simplified output, showing only the names of the files that differ.

Explanation:

  • --brief: Displays only whether files differ, without showing the actual differences.

Example output:

Files old_directory/file1 and new_directory/file1 differ
Files old_directory/subdirectory/file2 and new_directory/subdirectory/file2 differ

The output shows a brief message that indicates which files differ between the directories being compared.

Use case 7: Create patch file for Git from differences between text files

Code:

diff --text --unified --new-file old_file new_file > diff.patch

Motivation: When working with version control systems like Git, creating patch files for changes is a common practice. The diff command can be used to generate a patch file in a format compatible with Git.

Explanation:

  • --text: Treats the input files as text files, even if they do not end with a newline character.
  • --unified: Shows the differences in a unified format.
  • --new-file: Treats non-existent files as empty.

Example output: (diff.patch)

--- old_file
+++ new_file
@@ -1,3 +1,3 @@
-Line 1
-Line 2
-Line 3
+New Line 1
+New Line 2
+New Line 3

The patch file contains the unified differences between the old_file and the new_file in a format that can be applied using git apply or similar Git commands.

Conclusion:

The diff command is a powerful tool for comparing files and directories in Linux. With its various options, it allows for fine-grained analysis of changes, from comparing individual lines to recursive comparisons of directories. By understanding and utilizing these use cases, users can efficiently identify differences and track modifications in their files and directories.

Related Posts

How to use the command 'opkg' (with examples)

How to use the command 'opkg' (with examples)

Opkg is a lightweight package manager used to install OpenWrt packages.

Read More
How to use the command `rev` (with examples)

How to use the command `rev` (with examples)

The rev command is used to reverse a line of text or an entire file.

Read More
How to use the command 'go fix' (with examples)

How to use the command 'go fix' (with examples)

The go fix command is used to update packages to use new APIs in Go.

Read More