How to use the command diffstat (with examples)

How to use the command diffstat (with examples)

Diffstat is a command-line tool that creates a histogram from the output of the diff command. It provides a visual representation of the changes between two files. The diffstat command is useful for quickly understanding the differences between two versions of a file or a folder.

Use case 1: Display changes in a histogram

Code:

diff file1 file2 | diffstat

Motivation: You have made some changes to a file and want to see a high-level overview of the differences between the original file (file1) and the modified file (file2). By using diffstat, you can quickly identify the areas that have been changed.

Explanation:

  • diff: The diff command compares two files line by line and outputs the differences.
  • file1 file2: These are the two files that you want to compare.
  • | (pipe operator): This redirects the output of the diff command to the diffstat command.
  • diffstat: The diffstat command takes the output of the diff command and creates a visual representation of the changes in the form of a histogram.

Example output:

 file1 |   46 +++
 file2 |   31 +++
 2 files changed, 77 insertions(+)

This output indicates that there have been 46 lines added to file1 and 31 lines added to file2, resulting in a total of 77 insertions.

Use case 2: Display inserted, deleted, and modified changes as a table

Code:

diff file1 file2 | diffstat -t

Motivation: You want to view the changes between two files in a more detailed manner. By using the -t option with diffstat, you can get a table representation of the inserted, deleted, and modified lines.

Explanation:

  • diff file1 file2: This compares two files and outputs the differences.
  • | (pipe operator): This redirects the output of the diff command to the diffstat command.
  • diffstat: The diffstat command takes the output of the diff command.
  • -t: This option instructs diffstat to display the changes as a table.

Example output:

 insertions | deletions | file
   ---------+-----------+------
         46 |         0 | file1
         31 |         0 | file2

The output above shows that there have been 46 insertions and 0 deletions in file1, and 31 insertions and 0 deletions in file2. This table provides a clear overview of the changes made to the files.

Conclusion:

The diffstat command is a powerful tool for visualizing the differences between two versions of a file or a folder. By using diffstat, you can quickly identify the changes made, whether it’s by viewing a histogram or a table representation. This can be particularly useful when reviewing code changes or tracking modifications in files over time.

Related Posts

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

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

The amass command is an in-depth Attack Surface Mapping and Asset Discovery tool.

Read More
How to use the command pactree (with examples)

How to use the command pactree (with examples)

The pactree command is a package dependency tree viewer specifically designed for the pacman package manager used in Arch Linux.

Read More
yt-dlp Examples (with examples)

yt-dlp Examples (with examples)

Download a video or playlist Code: yt-dlp "https://www.youtube.com/watch?v=oHg5SJYRHA0" Motivation: Downloading a video or playlist allows users to access the content offline, without the need for an internet connection.

Read More