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
: Thediff
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 thediff
command to thediffstat
command.diffstat
: Thediffstat
command takes the output of thediff
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 thediff
command to thediffstat
command.diffstat
: Thediffstat
command takes the output of thediff
command.-t
: This option instructsdiffstat
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.