diffstat Command in Linux



The diffstat command in Linux displays a histogram and a summary from the diff command output. It essentially reads the diff command output through the pipe (|) or from a file and generates the histogram of changes. It can read all formats of the diff command standard outputs such as unified, context, and default.

Table of Contents

Installing the diffstat Command in Linux

Linux distributions may not have the diffstat command line utility installed by default. It can be installed as follows −

To install diffstat on Ubuntu, Debian, or other Debian-based distributions, use −

sudo apt install diffstat

On the Arch Linux

sudo pacman -S diffstat

On CentOS

sudo yum install diffstat

On Fedora, execute the following command −

sudo dnf install diffstat

Syntax for diffstat Command

The general syntax of using the diffstat command is as follows −

diffstat [options] [file]

This syntax is only useful if you have saved the diff command output to a file.

Specify the diffstat options in place of [options] while the [file] argument is used to specify the file containing the diff command output.

The second syntax of using the diffstat command is given below −

diff [options] [file1] [file2] | diffstat [options]

In this syntax, the diffstat is getting output via the pipe (|) symbol.

Options for diffstat Command

The diffstat command options are listed below −

Options Description
-b It ignores changes in white spaces while calculating the summary
-c It inserts the # before each line of stdout to make the output usable for shell scripting
-C It adds colors to the histogram
-d It prints the debug information
-e file It sends the stderr to the specified file
-f option

It sets the output format −

0 for concise output

1 for normal output

2 to fill the histogram with dots

4 to print each value with a histogram

-h It prints the help
-l It lists only file names, not the histogram
-m It merges the count of insert and delete
-o file It redirects the output to a file
-r code It rounds the data shown in the histogram
-s It shows only the summary lines (insertions and deletions)
-t It displays the output in tabular form instead of histogram
-T It prints the numbers of changes and histogram
-u It suppresses the sorting of the filenames
-v It shows progress
-w It specifies the maximum width of the histogram (the width ranges from 10 to 80)

Understand the diffstat Command Output

The diffstat command prints a histogram with figures and a summary that indicates the changes; such as insertions, deletions, and modifications. These changes are indicated by +, , and ! signs respectively.

  • + sign indicates the insertions
  • sign indicates the deletions
  • ! sign indicates the modifications

Typical output is shown in the following image −

Understand the diffstat Command Output 1

The file2.txt | 8 +++---- indicates the filename in which changes are detected and 8 is the total number of changes. While ++++---- shows the changes in the form of signs.

The second line: 1 file changed, 4 insertions (+), 4 deletions (-) is an overall summary that explains the changes across different files. It signifies that 1 file is changed, 4 lines are added and 4 lines are deleted between file1 and file2.

If diffstat is applied to a patch file containing changes across multiple files, the output would resemble the following format −

Understand the diffstat Command Output 2

The patch file contains the differences of multiple files with respect to a reference file.

Using diffstat Command in Linux

This section explores more about the diffstat command through various ways of its use −

Basic Usage of diffstat Command

The diffstat is primarily used to view the large and complex path files. The contents of the files to be compared are shown below −

Basic Usage of diffstat Command 1

To display the comparison, use the diff command −

diff -u file.cpp update.cpp
Basic Usage of diffstat Command 2

A total of five changes are highlighted after comparison.

Lets get the summary of changes, by piping the diff command unified output to diffstat

diff -u file.cpp update.cpp | diffstat
Basic Usage of diffstat Command 3

The diffstat summarizes the changes concisely.

To find the stats of multiple files, first store the differences of different files with respect to a reference file in a patch file.

diff -u file.cpp update1.cpp > patch.diff
diff -u file.cpp update2.cpp >> patch.diff
diff -u file.cpp update3.cpp >> patch.diff

In the above commands, file.cpp serves as the reference file, while update1.cpp, update2.cpp, and update3.cpp are three files that report changes relative to the reference file. The patch.diff file contains the changes of all the update files appended through the diff command.

diffstat patch.diff
Basic Usage of diffstat Command 4

The diffstat detected total changes in each update and displayed a histogram of changes with respect to the reference file.cpp. Overall, 3 files are changed, with a total of 15 insertions and 12 deletions.

In this way, the summary of changes can easily be viewed.

Note − In the following examples, the patch.diff file will be used to display the output.

Formatting the diffstat Output

The histogram can be displayed in different formats. The diffstat command has a built-in format flag -f that takes 3 format settings. These settings are specified by numbers 0, 1,2, and 4.

  • 0 for concise output that shows only values with histogram signs +, -, and !
  • 1 is the default output format of the diffstat command
  • 2 fills out the histogram with dots
  • 4 prints value with histogram
diffstat -f 0 patch.diff
Formatting the diffstat Output 1
diffstat -f 2 patch.diff
Formatting the diffstat Output 2
diffstat -f 4 patch.diff
Formatting the diffstat Output 3

The output can be made more readable by adding colors to the histogram. To add colors -C flag is used −

diffstat -C patch.diff
Formatting the diffstat Output 4

To display the summary line only, use the -s flag −

diffstat -s patch.diff
Formatting the diffstat Output 5

To display the diffstat command output in tabular form, use the -t flag −

diffstat -t patch.diff
Formatting the diffstat Output 6

Each field is separated by a comma.

To list the filenames only, use the -l flag −

diffstat -l patch.diff
Formatting the diffstat Output 7

To merge the inserts and deletes -m flag is employed −

diffstat -m patch.diff
Formatting the diffstat Output 8

Here, the modifications (!) signify the combination of inserts and deletes. One modification (!) is equal to one insert (+) and one delete (-).

Note − These formatting options can also be used with pipe symbol syntax.

Redirecting Output to a File

The diffstat command can be stored in a file using the -o flag with the filename.

diffstat -T patch.diff -o output.txt
Redirecting Output to a File

In the above command -T is a formatting flag.

Conclusion

The Linux diffstat command reads the output of the diff command to display a histogram and summary. This makes it easier for developers to understand and view the changes in each file. Moreover, this tool comes with many formatting options, that help in displaying the output in various formats.

This tutorial covered the diffstat command, its installation, syntax, and usage with different formatting options.

Advertisements