Linux diffstat Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux diffstat command, which is a utility that summarizes the changes made to a set of files. You will start by understanding the purpose and functionality of the diffstat command, including how it can be used to analyze the output of the diff command and Git diffs. Then, you will explore the basic usage of the diffstat command and learn how to customize its output. Finally, you will apply the diffstat command to analyze patch files and Git diffs, which can be useful when reviewing large changes.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/wc("`Text Counting`") linux/BasicFileOperationsGroup -.-> linux/less("`File Paging`") linux/VersionControlandTextEditorsGroup -.-> linux/diff("`File Comparing`") subgraph Lab Skills linux/cat -.-> lab-422635{{"`Linux diffstat Command with Practical Examples`"}} linux/wc -.-> lab-422635{{"`Linux diffstat Command with Practical Examples`"}} linux/less -.-> lab-422635{{"`Linux diffstat Command with Practical Examples`"}} linux/diff -.-> lab-422635{{"`Linux diffstat Command with Practical Examples`"}} end

Understand the Purpose and Functionality of the diffstat Command

In this step, you will learn about the purpose and functionality of the diffstat command in Linux. The diffstat command is a utility that summarizes the changes made to a set of files, typically used to analyze the output of the diff command.

The diffstat command reads the output of the diff command and produces a histogram-like summary of the insertions, deletions, and modifications in the files. This can be useful when reviewing large diffs, as it provides a high-level overview of the changes made.

Let's start by running the diffstat command on a simple example:

$ diff file1.txt file2.txt | diffstat
 file1.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

In this example, the diffstat command is used to analyze the output of the diff command, which compares the contents of file1.txt and file2.txt. The output of diffstat shows that one file was changed, with one insertion and one deletion.

The diffstat command can also be used to analyze the output of Git diffs. For example, to see a summary of the changes in the last Git commit, you can run:

$ git diff HEAD~1 HEAD | diffstat
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

This command compares the current commit with the previous commit and displays a summary of the changes using diffstat.

The diffstat command provides several options to customize the output, such as controlling the width of the histogram, the maximum number of files to display, and the order of the files. You can explore these options by running man diffstat to learn more.

Explore the Basic Usage of the diffstat Command

In this step, you will explore the basic usage of the diffstat command and learn how to customize its output.

First, let's create a simple example to demonstrate the basic usage of diffstat. We'll create two text files, make some changes, and then use diffstat to analyze the differences.

## Create two text files
$ echo "This is file1.txt" > file1.txt
$ echo "This is file2.txt" > file2.txt

## Make some changes to file2.txt
$ echo "Added a new line" >> file2.txt
$ echo "Deleted a line" >> file2.txt

## Use diffstat to analyze the differences
$ diff file1.txt file2.txt | diffstat
 file1.txt | 1 +
 file2.txt | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

The output of diffstat shows that two files were changed, with one insertion and one deletion in file2.txt.

Now, let's explore some of the options available with the diffstat command:

## Specify the maximum number of files to display
$ diff *.txt | diffstat -w 80 -n 1
 file1.txt | 1 +
 file2.txt | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

## Change the width of the histogram
$ diff *.txt | diffstat -w 120
 file1.txt | 1 +
 file2.txt | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

## Sort the files by the number of changes
$ diff *.txt | diffstat -s
 file2.txt | 2 +-
 file1.txt | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

As you can see, the diffstat command provides several options to customize the output, such as controlling the width of the histogram, the maximum number of files to display, and the order of the files.

Apply diffstat to Analyze Patch Files and Git Diffs

In this final step, you will learn how to use the diffstat command to analyze patch files and Git diffs.

First, let's create a simple patch file and use diffstat to analyze it:

## Create a patch file
$ diff file1.txt file2.txt > changes.patch

## Use diffstat to analyze the patch file
$ cat changes.patch | diffstat
 file1.txt | 1 +
 file2.txt | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

The diffstat command can read the patch file directly and provide a summary of the changes.

Next, let's try using diffstat with Git diffs. We'll create a new Git repository, make some changes, and then use diffstat to analyze the differences.

## Initialize a new Git repository
$ git init
Initialized empty Git repository in ~/project/.git/

## Create a new file and commit it
$ echo "This is file1.txt" > file1.txt
$ git add file1.txt
$ git commit -m "Add file1.txt"

## Make some changes and create a new commit
$ echo "Added a new line" >> file1.txt
$ git add file1.txt
$ git commit -m "Update file1.txt"

## Use diffstat to analyze the Git diff
$ git diff HEAD~1 HEAD | diffstat
 file1.txt | 1 +
 1 file changed, 1 insertion(+)

In this example, we first initialized a new Git repository, created a new file, and committed it. We then made some changes to the file and created a new commit. Finally, we used diffstat to analyze the differences between the two commits.

The diffstat command can be a valuable tool when working with patch files and Git diffs, as it provides a concise summary of the changes made to the files.

Summary

In this lab, you first learned about the purpose and functionality of the diffstat command in Linux. The diffstat command is a utility that summarizes the changes made to a set of files, typically used to analyze the output of the diff command. It can provide a high-level overview of the insertions, deletions, and modifications in the files, which can be useful when reviewing large diffs.

Next, you explored the basic usage of the diffstat command, including how to customize its output by controlling the width of the histogram, the maximum number of files to display, and the order of the files. You also learned how to use diffstat to analyze the output of Git diffs, which can be helpful when reviewing changes made in Git repositories.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like