Linux diff Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux diff command to compare the contents of two text files and understand the differences between them. The lab covers the purpose and basic syntax of the diff command, as well as exploring its advanced options. You will practice using the diff command to compare files and interpret the output, which can be useful for tasks such as code review, file synchronization, and troubleshooting. The lab provides a practical and hands-on approach to mastering this essential text processing and editing tool in the Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/VersionControlandTextEditorsGroup -.-> linux/diff("`File Comparing`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") subgraph Lab Skills linux/cat -.-> lab-422633{{"`Linux diff Command with Practical Examples`"}} linux/echo -.-> lab-422633{{"`Linux diff Command with Practical Examples`"}} linux/diff -.-> lab-422633{{"`Linux diff Command with Practical Examples`"}} linux/ls -.-> lab-422633{{"`Linux diff Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the diff Command

In this step, you will learn about the purpose and basic syntax of the diff command in Linux. The diff command is a powerful tool used to compare the contents of two files and display the differences between them.

First, let's create two sample text files to work with:

cd ~/project
echo "This is file1.txt" > file1.txt
echo "This is file2.txt" > file2.txt

Now, let's use the diff command to compare the two files:

diff file1.txt file2.txt

Example output:

1c1
< This is file1.txt
---
> This is file2.txt

The output shows that the first line (1c1) of the two files is different. The < symbol indicates the line from the first file, and the > symbol indicates the line from the second file.

The basic syntax of the diff command is:

diff [options] file1 file2

Here, file1 and file2 are the two files you want to compare.

Some common options for the diff command include:

  • -c: Display the differences in a context format, showing the lines around the changes.
  • -u: Display the differences in a unified format, showing the lines around the changes.
  • -r: Recursively compare directories and their contents.
  • -w: Ignore white space differences.

We will explore more advanced options of the diff command in the next step.

Compare Two Text Files Using the diff Command

In this step, you will learn how to use the diff command to compare the contents of two text files and understand the output.

First, let's create two new text files with some differences:

cd ~/project
echo "This is line 1 in file1.txt" > file1.txt
echo "This is line 1 in file2.txt" > file2.txt
echo "This is line 2 in file1.txt" >> file1.txt
echo "This is line 2 in file2.txt" >> file2.txt

Now, let's use the diff command to compare the two files:

diff file1.txt file2.txt

Example output:

1c1
< This is line 1 in file1.txt
---
> This is line 1 in file2.txt
2c2
< This is line 2 in file1.txt
---
> This is line 2 in file2.txt

The output shows the differences between the two files. The 1c1 line indicates that the first line in file1.txt is different from the first line in file2.txt. The < symbol shows the line from file1.txt, and the > symbol shows the line from file2.txt.

Similarly, the 2c2 line indicates that the second line in file1.txt is different from the second line in file2.txt.

You can also use the -c or -u options to display the differences in a more readable format:

diff -c file1.txt file2.txt

Example output:

*** file1.txt	2023-04-24 11:46:27.000000000 +0000
--- file2.txt	2023-04-24 11:46:32.000000000 +0000
***************
*** 1 ****
! This is line 1 in file1.txt
--- 1 ----
! This is line 1 in file2.txt
***************
** 2 ****
! This is line 2 in file1.txt
-- 2 ----
! This is line 2 in file2.txt

The -c option displays the differences in a context format, showing the lines around the changes.

Explore Advanced Options of the diff Command

In this step, you will learn about some advanced options of the diff command to enhance its functionality.

Let's start by creating a new directory and some files to work with:

cd ~/project
mkdir dir1 dir2
echo "This is file1.txt in dir1" > dir1/file1.txt
echo "This is file2.txt in dir1" > dir1/file2.txt
echo "This is file1.txt in dir2" > dir2/file1.txt
echo "This is file2.txt in dir2" > dir2/file2.txt

Now, let's use the -r (recursive) option to compare the contents of the two directories:

diff -r dir1 dir2

Example output:

Only in dir1: file1.txt
Only in dir1: file2.txt
Only in dir2: file1.txt
Only in dir2: file2.txt
diff dir1/file1.txt dir2/file1.txt
1c1
< This is file1.txt in dir1
---
> This is file1.txt in dir2
diff dir1/file2.txt dir2/file2.txt
1c1
< This is file2.txt in dir1
---
> This is file2.txt in dir2

The -r option allows diff to recursively compare the contents of the two directories and their files.

Another useful option is -w, which ignores white space differences:

echo "  This is file3.txt in dir1  " > dir1/file3.txt
echo "This is file3.txt in dir2" > dir2/file3.txt
diff -w dir1/file3.txt dir2/file3.txt

Example output:

1c1
< This is file3.txt in dir1
---
> This is file3.txt in dir2

The -w option ensures that the differences in white space (e.g., leading/trailing spaces) are ignored.

You can also use the diff command to compare the contents of two directories and only show the files that are different:

diff -q dir1 dir2

Example output:

Files dir1/file1.txt and dir2/file1.txt differ
Files dir1/file2.txt and dir2/file2.txt differ

The -q option only shows the file names that are different, without displaying the actual differences.

Summary

In this lab, you learned about the purpose and basic syntax of the diff command in Linux, which is used to compare the contents of two files and display the differences between them. You created sample text files and used the diff command to compare them, understanding the output format and common options such as -c, -u, -r, and -w. You also explored how to use the diff command to compare the contents of two text files with differences and interpret the output, which shows the lines that are different between the files.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like