NeuroAgent

Complete Guide to Recursive File Searching

Master recursive file searching with wildcard patterns on Linux, macOS, and Windows. Includes find command, PowerShell, and cross-platform solutions with practical examples.

Question

How can I recursively find all files in current and subdirectories using wildcard pattern matching?

NeuroAgent

Recursive file searching using wildcard patterns is a fundamental operation that works differently across operating systems. In Linux and macOS, you can use the find command with wildcard patterns like * and ?, while Windows offers the dir command with /s switch or PowerShell’s Get-ChildItem with -Recurse parameter. Each platform has its own syntax and capabilities for pattern matching and recursive traversal.

Contents


Linux and macOS Methods

Using the find Command

The find command is the most powerful and flexible tool for recursive file searching on Unix-like systems.

Basic syntax:

bash
find [path] [expression]

Common patterns:

  • Find all files: find . -type f
  • Find all .txt files recursively: find . -name "*.txt"
  • Find files matching multiple patterns: find . \( -name "*.txt" -o -name "*.md" \)
  • Case-insensitive search: find . -iname "*.TXT"

Advanced examples:

bash
# Find all Python files excluding virtual environments
find . -name "*.py" -not -path "./venv/*" -not -path "./.env/*"

# Find files larger than 1MB
find . -type f -size +1M

# Find modified files in last 7 days
find . -type f -mtime -7

Using globstar in Bash

Modern Bash versions support globstar for recursive pattern matching:

bash
# Enable globstar
shopt -s globstar

# Recursive pattern matching
for file in **/*.txt; do
    echo "Found: $file"
done

Using grep with Recursive Search

For searching within file contents:

bash
# Search for text recursively
grep -r "search_term" .

# Exclude directories
grep -r "search_term" --exclude-dir=node_modules .

Windows Methods

Command Prompt (CMD)

The traditional dir command supports basic recursive searching:

cmd
# Recursive directory listing
dir /s

# Find specific file types
dir /s *.txt

# Find multiple extensions
dir /s *.txt *.md *.pdf

PowerShell

PowerShell provides more sophisticated pattern matching capabilities:

powershell
# Basic recursive search
Get-ChildItem -Recurse -Name "*.txt"

# Find with multiple patterns
Get-ChildItem -Recurse -Include "*.txt", "*.md", "*.pdf"

# Exclude specific directories
Get-ChildItem -Recurse -Path . -Include "*.py" -Exclude "venv", ".git"

# Find files by size
Get-ChildItem -Recurse | Where-Object { $_.Length -gt 1MB }

Windows Search Feature

Windows provides built-in search functionality:

  1. Open File Explorer
  2. Use the search bar at the top
  3. Enter patterns like *.txt
  4. Click “Search in this folder” to enable recursive search

Advanced Pattern Matching Techniques

Regular Expressions

Most modern tools support regular expressions for complex patterns:

Linux/macOS:

bash
# Find files with numbers in name
find . -regex '.*[0-9].*'

# Find image files
find . -regex '.*\.\(jpg\|jpeg\|png\|gif\)'

PowerShell:

powershell
# Using -match operator
Get-ChildItem -Recurse | Where-Object { $_.Name -match '.*[0-9].*' }

# Using regex directly
Get-ChildItem -Recurse -Filter "test[0-9]*.txt"

Complex Pattern Combinations

Linux/macOS:

bash
# Find files modified after a certain date
find . -name "*.log" -newermt "2024-01-01"

# Find empty files
find . -type f -empty

# Find executable files
find . -type f -executable

PowerShell:

powershell
# Find files with specific properties
Get-ChildItem -Recurse | Where-Object {
    $_.Extension -eq ".txt" -and $_.LastWriteTime -gt (Get-Date).AddDays(-7)
}

Cross-Platform Solutions

Python Script

A cross-platform Python solution:

python
import os
import glob

def find_files(pattern, root_dir='.'):
    """Recursively find files matching pattern"""
    matches = []
    for root, dirs, files in os.walk(root_dir):
        for file in files:
            if glob.fnmatch.fnmatch(file, pattern):
                matches.append(os.path.join(root, file))
    return matches

# Usage
txt_files = find_files('*.txt')
print(f"Found {len(txt_files)} text files")

Using fd Command

A modern, fast alternative to find:

bash
# Install fd (fd-find)
# Ubuntu/Debian: sudo apt install fd-find
# macOS: brew install fd

# Basic usage
fd "*.txt"

# Exclude directories
fd "*.txt" --exclude-dir=node_modules

# Case-insensitive
fd "*.txt" -i

Using ripgrep (rg)

Another fast alternative with regex support:

bash
# Find files containing text
rg -l "search_term" .

# Find files by pattern
rg -g "*.txt" .

# Recursive search with exclusion
rg -g "*.py" --exclude-dir=venv .

Common Use Cases and Examples

Finding Configuration Files

Linux/macOS:

bash
find ~ -name "*.conf" -o -name "*.config" -o -name "*.ini"

Windows PowerShell:

powershell
Get-ChildItem -Recurse -Path $env:USERPROFILE -Include "*.conf", "*.config", "*.ini"

Finding Temporary Files

Linux/macOS:

bash
find /tmp -name "*.tmp" -mtime +7  # Files older than 7 days

Windows PowerShell:

powershell
Get-ChildItem -Recurse -Path $env:TEMP -Filter "*.tmp" | 
    Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) }

Finding Large Files

Linux/macOS:

bash
find . -type f -size +100M  # Files larger than 100MB

Windows PowerShell:

powershell
Get-ChildItem -Recurse -Path . | 
    Where-Object { $_.Length -gt 100MB } | 
    Sort-Object Length -Descending

Backup Specific File Types

Linux/macOS:

bash
find . -name "*.doc" -o -name "*.docx" -o -name "*.pdf" | tar -czf documents.tar.gz -T -

Windows PowerShell:

powershell
$documents = Get-ChildItem -Recurse -Include "*.doc", "*.docx", "*.pdf"
$documents | Compress-Archive -DestinationPath "documents.zip"

Conclusion

Recursive file searching with wildcard patterns is essential for efficient file management across all operating systems. The find command on Linux and macOS offers the most comprehensive pattern matching capabilities, while PowerShell provides powerful features on Windows. For cross-platform needs, Python scripts or modern tools like fd and rg offer excellent alternatives. Understanding each platform’s syntax and limitations will help you choose the most appropriate method for your specific file searching requirements, whether you’re managing configuration files, finding temporary files, or organizing large file collections.

Sources

  1. GNU findutils documentation
  2. PowerShell Get-ChildItem documentation
  3. Bash globstar feature documentation
  4. Windows dir command reference
  5. Python os.walk documentation