filterdiff Command in Linux
The filterdiff command in Linux manipulates and processes files. It is essentially used to include or exclude a specific part of the or diff file based on the specified pattern.
The filterdiff command is beneficial in cases when only selective changes are required. The developer can review the specific portion of the file by extracting it or cleaning the unnecessary lines.
Table of Contents
Here is a comprehensive guide to the options available with the filterdiff command −
- Prerequisites to Use filterdiff Command
- Syntax of filterdiff Command
- filterdiff Command Options
- Examples of filterdiff Command in Linux
Prerequisites to Use filterdiff Command
The filterdiff command may not be included in Linux distributions by default. To use the filterdiff command, it needs to be installed. To view whether it is installed or not, use the following command −
filterdiff --version

If the above command shows the version, then it is installed. If it does not show any output, then use the following instructions to install it.
The filterdiff command is a part of the utils suite. The utils offers various command-line utilities to work with es.
To install the utils package on Ubuntu, Kali Linux, Raspberry Pi OS, Debian, and other Debian-based distributions, use the following command −
sudo apt install utils
To install it on Arch Linux, use −
sudo pacman -S utils
To install the utils package on CentOS, use the following command −
sudo yum install utils
To install it on Fedora, use the following command −
sudo dnf install utils
Syntax of filterdiff Command
The syntax of the Linux filterdiff command is as follows −
filterdiff [options] [file]
The [options] field is used to specify the options to modify the commandâs behavior. The [file] field is used to specify the files that need to be processed.
filterdiff Command Options
The options of the filterdiff command are listed below −
Flags | Options | Description |
---|---|---|
-i pattern | --include | To include only files that match specified pattern (suppresses other lines) |
-I | --include-from-file | To include only files that match the pattern specified in the file (suppresses other lines) |
-x | --exclude | To exclude only files that match specified pattern (displays other lines) |
-X | --exclude-from-file | To exclude only files that match the pattern specified in the file (displays other lines) |
-p | --stripe-match | To ignore the specified number of components of pathname when matching files |
-# | --hunks | To include only specific hunks based on their numbers |
--lines | To Include only hunks that affect specific lines in the original files | |
--files | To include only specific files based on their order in the | |
--annotate | To annotate the hunks with the filename or hunk number | |
--format | To choose between the unified or context formats for the output | |
--addprefix | To add a prefix to all pathnames in the output | |
--addoldprefix | To add a prefix only to old (original) file paths | |
--addnewprefix | To add a prefix only to new (updated) file paths | |
--as-numbered-lines | To display the lines of selected hunks with their line numbers | |
--remove-timestamps | To skip including the timestamps in the output | |
-v | --verbose | To get the detailed output |
--clean | To remove the non-diff (comments) lines from the output | |
-z | --decompress | To decompress the -gz and .bz2 files |
--help | To display the command help | |
--version | To display the command version | |
--list | It runs as lsdiff | |
--grep | It runs as grepdiff |
Examples of filterdiff Command in Linux
This section demonstrates the usage of the filterdiff command in Linux with examples.
The sample file that will be used in the following examples is given below −

- Including and Displaying Specific File es
- Excluding and Displaying Specific File es
- Annotating Hunks
- Displaying the Specific Hunks
- Removing the Non-Diff Lines
- Displaying Selected Hunk Lines with Line Numbers
- Removing Timestamps
- Listing Files
Including and Displaying Specific File es
Use the -i or --include options to display specific file es. For example, to display the changes related to the example.c file, use the filterdiff command in the following way −
filterdiff -i 'example.c' example.

The command will only include the diffs for example.c.
Excluding and Displaying Specific File es
To exclude and display the specific file es, use the -x or --exclude option −
filterdiff -x 'example.c' example.

Annotating Hunks
To annotate each hunk with the file name and hunk number, use the --annotate option −
filterdiff --annotate example.

Displaying the Specific Hunks
To display the specific hunk, use the -# or --hunks options. Two hunks can be seen in the following file.

To display the second hunk, use the following command −
filterdiff -#2 multi.

Removing the Non-Diff Lines
The comments are considered non-diff lines in files. The comments can be seen in the file given below −

The comments in the files start with # sign.
To remove the non-diff lines from the files, use the --clean option
filterdiff --clean example.

Displaying Selected Hunk Lines with Line Numbers
Instead of displaying fragments, displaying only lines of the selected hunk with line numbers before or after the is applied, use the --as-numbered-lines option −
filterdiff --as-numbered-lines=before example.

Removing Timestamps
To remove the timestamps from the output, use the --remove-timestamps option −
filterdiff --remove-timestamps example.

Listing Files
To list filenames that are modified, use the --list option −
filterdiff --list example.

To show the line number in the file where the begins, use the -n option −
filterdiff --list -n inc.

Conclusion
The filterdiff command in Linux is used to process files. It is a handy tool to review the combined files. It offers various options for modifying the commandâs output.
The filterdiff command is a part of the utils package. Therefore, the utils package must be installed to use it.