Find Command for File Discovery

Uses the find command with various predicates, actions, and exec options to locate, filter, and process files based on metadata and patterns.

BashFile Upload

Code

snippet
#!/usr/bin/env bash
set -euo pipefail

# Find by name pattern
find /etc -maxdepth 2 -name "*.conf" -type f 2>/dev/null | head -10

# Find by size
echo "=== Large files (>1MB) ==="
find /var/log -type f -size +1M -exec ls -lh {} \; 2>/dev/null | head -5

# Find by modification time
echo "=== Recently modified (last 24h) ==="
find /tmp -type f -mtime -1 2>/dev/null | head -5

echo "=== Old files (>30 days) ==="
find /var/log -type f -mtime +30 -name "*.log" 2>/dev/null | head -5

# Find by permissions
echo "=== World-writable files ==="
find /tmp -type f -perm -o+w 2>/dev/null | head -5

# Find and execute
echo "=== Count lines in config files ==="
find /etc -maxdepth 1 -name "*.conf" -type f -exec wc -l {} + 2>/dev/null | tail -5

# Find with multiple conditions
echo "=== JS or TS files modified recently ==="
find . -type f \( -name "*.js" -o -name "*.ts" \) -mtime -7 2>/dev/null | head -10

# Find empty directories
echo "=== Empty directories ==="
find /tmp -maxdepth 3 -type d -empty 2>/dev/null | head -5

# Find with printf for custom output
echo "=== Formatted output ==="
find /etc -maxdepth 1 -type f -printf "%f\t%k KB\t%TY-%Tm-%Td\n" 2>/dev/null | head -5

# Find newest files
echo "=== 5 newest files ==="
find /var/log -type f -printf "%T@ %p\n" 2>/dev/null | sort -rn | head -5 | cut -d" " -f2-

# Find and process in batches with null delimiter
echo "=== Batch processing ==="
find /etc -name "*.conf" -type f -print0 2>/dev/null |
    xargs -0 -I{} -P4 sh -c "echo Processing: {}; wc -l {}" 2>/dev/null | head -10

# Find with prune to exclude directories
echo "=== Exclude node_modules ==="
find . -type f -name "*.js" -not -path "*/node_modules/*" 2>/dev/null | head -10