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

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
Prerequisites to Use filterdiff

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 −

FlagsOptionsDescription
-i pattern--includeTo include only files that match specified pattern (suppresses other lines)
-I--include-from-fileTo include only files that match the pattern specified in the file (suppresses other lines)
-x--excludeTo exclude only files that match specified pattern (displays other lines)
-X--exclude-from-fileTo exclude only files that match the pattern specified in the file (displays other lines)
-p--stripe-matchTo ignore the specified number of components of pathname when matching files
-#--hunksTo include only specific hunks based on their numbers
--linesTo Include only hunks that affect specific lines in the original files
--filesTo include only specific files based on their order in the
--annotateTo annotate the hunks with the filename or hunk number
--formatTo choose between the unified or context formats for the output
--addprefixTo add a prefix to all pathnames in the output
--addoldprefixTo add a prefix only to old (original) file paths
--addnewprefixTo add a prefix only to new (updated) file paths
--as-numbered-linesTo display the lines of selected hunks with their line numbers
--remove-timestampsTo skip including the timestamps in the output
-v--verboseTo get the detailed output
--cleanTo remove the non-diff (comments) lines from the output
-z--decompressTo decompress the -gz and .bz2 files
--helpTo display the command help
--versionTo display the command version
--listIt runs as lsdiff
--grepIt 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 −

Examples of filterdiff Command
  • 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.
Including Specific File Patche Using filterdiff

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.
Excluding Specific File Patche Using filterdiff

Annotating Hunks

To annotate each hunk with the file name and hunk number, use the --annotate option −

filterdiff --annotate example.
Annotating Hunks Using filterdiff

Displaying the Specific Hunks

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

Displaying Specific Patch Hunks Using filterdiff

To display the second hunk, use the following command −

filterdiff -#2 multi.
Display Second hunk Using filterdiff

Removing the Non-Diff Lines

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

Removing Non-Diff Lines Using filterdiff

The comments in the files start with # sign.

To remove the non-diff lines from the files, use the --clean option

filterdiff --clean example.
Remove Non-Diff Lines Using filterdiff

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.
Displaying Selected Hunk Lines Using filterdiff

Removing Timestamps

To remove the timestamps from the output, use the --remove-timestamps option −

filterdiff --remove-timestamps example.
Removing Timestamps Using filterdiff

Listing Files

To list filenames that are modified, use the --list option −

filterdiff --list example.
Listing Patch Files Using filterdiff

To show the line number in the file where the begins, use the -n option −

filterdiff --list -n inc.
Show Line Number in Patch File

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.