
lsdiff Command in Linux
lsdiff is a command used in Linux for comparing differences in patch files. When you make changes to code or text files, a patch file is created to show what was added, removed, or modified.
By using lsdiff, you can easily see a summary of these changes that helps you understand the impact of the modifications. Besides that, it also lists the files that have been altered and gives a clear overview of the changes. This is pretty useful for developers and system administrators in case they want to review and manage patches efficiently.
Table of Contents
Here is a comprehensive guide to the options available with the lsdiff command −
- How to Install lsdiff Command in Linux?
- Syntax of lsdiff Command
- lsdiff Command Options
- Examples of lsdiff Command in Linux
How to Install lsdiff Command in Linux?
The command lsdiff is not installed by default on most Linux systems, but you can easily add it using your package manager. Here are the commands to install lsdiff on different Linux distributions −
On Debian-based systems (like Ubuntu) −
sudo apt install patchutils
On Red Hat-based systems (like CentOS) −
sudo yum install patchutils
On Fedora −
sudo dnf install patchutils
On Arch Linux −
sudo pacman -S patchutils
On OpenSUSE −
sudo zypper install patchutils
Syntax of lsdiff Command
The basic syntax for the lsdiff command on Linux is as follows −
lsdiff [options] patchfile
In this command −
- [options] are various flags that you can use to modify the output.
- patchfile is the file containing the differences you want to examine.
lsdiff Command Options
Listed below are few different options that can be used with the Linux lsdiff command −
Options | Description |
---|---|
-# RANGE | Only lists hunks within the specified range. |
-E | Treats empty files as absent for displaying file additions, modifications, and removals. |
-H | Prints the name of the patch file containing each patch. |
-i PAT | Ignores case differences in filenames, treating uppercase and lowercase as the same. |
-I PAT | Includes only files matching the specified pattern. |
-n | Shows line numbers for each change in the patch, making it easier to locate changes. |
-N | Excludes files matching the specified pattern. |
-p | Displays filenames with their full paths, providing more context about where changes occur. |
-s | Displays a summary of the patch, giving an overview of the changes. |
-v | Increases verbosity, providing more detailed output. |
-x PAT | Excludes files matching the specified pattern. |
--addprefix PREFIX | Prefixes the pathname with the specified prefix before displaying it. |
--filter | Filters the output based on specified criteria. |
--grep | Searches for patterns within the patch files. |
--strip | Strips a specified number of leading components from file names. |
Examples of lsdiff Command in Linux
These are a few ways you can practically apply the lsdiff command in Linux.
- Displaying a Summary of Changes
- Showing Line Numbers
- Ignoring Case Differences
- Excluding Specific Files
- Including Only Specific Files
- Stripping Path Components
Displaying a Summary of Changes
If you want a quick overview of the files modified by a patch file, you can use the -s option. It helps you see a summary of changes without diving into details.
lsdiff -s patchfile.patch
This command shows a summary of the patch, and indicates which files were added, modified, or removed, giving you a quick snapshot of the changes.

Note − Here, we have used two files for examples discussed in this guide, one is file1.txt and other is file2.txt. The patch file is created using the diff command.
diff -u file1.txt file2.txt > patchfile.patch
Showing Line Numbers
To display the line numbers where changes occur, the -n option is quite useful. It provides context about where changes are made within the files.
lsdiff -n patchfile.patch
This command lists the files modified by the patch along with the line numbers where the changes start. This helps you locate the changes easily.

Excluding Specific Files
In case you want to exclude certain files from the output, such as log files, use the -x option. This helps focus on relevant files.
lsdiff -x "*.log" patchfile.patch
This command lists the files modified by the patch, excluding any .log files, which can declutter the output.

Including Only Specific Files
If you only want to see changes for specific files, use the -i option with a pattern. This is useful for narrowing down the output.
lsdiff -i "*.txt" patchfile.patch
This command lists the files modified by the patch, including only .txt files, filtering out irrelevant changes.

Stripping Path Components
To remove a specified number of leading components from file names, the --strip option is handy. It simplifies the path display.
lsdiff --strip=2 patchfile.patch
This command removes the specified number of leading components from file paths, making the output more concise.

Conclusion
lsdiff is an essential command for Linux users who need to compare differences in patch files. Whether you're reviewing changes, managing patches efficiently, or understanding the impact of modifications, lsdiff provides the necessary functionality to see a summary and details of changes effectively.
In this tutorial, we explained the syntax, options, and practical examples of using the lsdiff command. Mastering this command will enhance your ability to manage patches and improve the accuracy and efficiency of your development and system administration tasks in a Linux environment.