Diff a Directory for Only Files of a Specific Type on Linux

In this article, we are going to learn how to diff a directory for only files of a specific type in Linux. This is particularly useful when you need to compare configuration files, source code, or documentation across directories while ignoring irrelevant file types.

The diff Command Overview

The diff command is a powerful built-in Linux tool that compares files and directories line by line. When comparing directories, it identifies differences in file contents, missing files, and additional files. However, by default, it processes all files, which can be overwhelming in directories with mixed file types.

Basic Directory Comparison

To compare two directories, use the basic syntax

$ diff directory1 directory2

Example

$ diff dev home
Only in home: cg
Only in dev: core
Only in dev: fd
Only in dev: full
Only in home: objc
Only in dev: null
Only in dev: random
Only in dev: zero

Filtering Files by Type

To compare only specific file types, combine diff with shell patterns and the -x option to exclude unwanted files, or use find command for more precise control.

Method 1: Using Wildcards with --include

# Compare only .txt files
$ diff --recursive --exclude="*" --include="*.txt" dir1 dir2

# Compare only shell scripts
$ diff -r --include="*.sh" dir1 dir2

Method 2: Using find with diff

# Find and compare only .conf files
$ find dir1 dir2 -name "*.conf" -exec diff {} +

# Compare Python files specifically
$ for file in $(find dir1 -name "*.py"); do
    relative=${file#dir1/}
    if [[ -f "dir2/$relative" ]]; then
        diff "$file" "dir2/$relative"
    fi
done

Useful diff Options

Side-by-Side Comparison

Use the -y option for better visual comparison

$ diff -y config1.sh config2.sh
TCL_MAJOR_VERSION='8'    | TCLOO_LIB_SPEC="7"
TCL_MINOR_VERSION='6'    | TCLOO_STUB_LIB_SPEC="4"
TCL_PATCH_LEVEL='.8'     | TCLOO_INCLUDE_SPEC="6"
TCL_VERSION='8.6'        | TCLOO_PRIVATE_INCLUDE="1"

Unified Format

The -u option provides context with line numbers

$ diff -u file1.sh file2.sh
--- file1.sh 2019-05-11 15:50:49.000000000 +0000
+++ file2.sh 2019-05-11 15:50:43.000000000 +0000
@@ -1,3 +1,5 @@
 It will create shell variables
 This script is intended to be included
 The information in this file is specific
+These are mostly empty
+All libraries and include files are just part of Tcl

Practical Examples

File Type Command Example Use Case
Configuration files diff -r --include="*.conf" /etc/dir1 /etc/dir2 System configuration comparison
Log files diff -u --include="*.log" logs1/ logs2/ Log analysis and debugging
Source code diff -r --include="*.c" --include="*.h" src1/ src2/ Code version comparison

Advanced Filtering

For complex filtering, create a custom script

#!/bin/bash
# Compare only specific file extensions
EXTENSIONS=("*.txt" "*.conf" "*.sh")
for ext in "${EXTENSIONS[@]}"; do
    echo "Comparing $ext files:"
    diff -r --include="$ext" "$1" "$2"
done

Conclusion

Filtering directory comparisons by file type makes the diff command more focused and efficient. By using options like --include, --exclude, and combining with find, you can precisely target the files you need to compare while ignoring irrelevant content.

Updated on: 2026-03-17T09:01:38+05:30

823 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements