The Linux find
command is incredibly powerful for file searching, but its date comparison functionality primarily focuses on relative time periods (e.g., "modified within the last 7 days"). When you need to find files older than an absolute date (like "before January 1, 2023"), the solution isn't as immediately obvious.
Here's the most precise method using Unix timestamps:
# First, get the Unix timestamp for your target date
date -d "2023-01-01" +%s
# Then use that timestamp with find
find /path/to/search -type f -not -newermt "@$(date -d '2023-01-01' +%s)"
If you prefer not to work with timestamps directly:
# Create a reference file with specific modification time
touch -t 202301010000 /tmp/reference_file
# Find files older than reference
find /path/to/search -type f ! -newer /tmp/reference_file
Finding log files older than 6 months:
find /var/log -name "*.log" -type f -not -newermt "@$(date -d '6 months ago' +%s)"
Locating backup files before a specific date:
find /backups -name "*.tar.gz" -type f -not -newermt "@$(date -d '2022-12-31' +%s)"
1. The -newermt
and -newer
predicates compare modification times
2. Use -not
or !
to invert the condition for "older than" searches
3. For creation time comparisons, you'll need to use stat
with more complex conditions
Example finding large PDF files modified before 2022:
find ~/documents -name "*.pdf" -size +5M -not -newermt "@$(date -d '2022-01-01' +%s)"
While find
excels at relative time searches (like "modified in last 7 days"), absolute date comparisons require a different approach. The man page doesn't explicitly document this functionality, but there are robust solutions available.
Here's the most precise technique using Unix timestamps:
# Convert target date to Unix timestamp
target_date="2023-01-15"
timestamp=$(date -d "$target_date" +%s)
# Find files modified before that date
find /path/to/search -type f -newermt "$target_date" ! -newermt now -exec ls -l {} \;
When you need maximum compatibility across different find versions:
# Create a reference file with specific timestamp
touch -d "2023-01-15" /tmp/reference_file
# Find files older than reference
find /path/to/search -type f ! -newer /tmp/reference_file
For production scripts, consider these enhancements:
# Explicit timezone handling
export TZ=UTC
find /var/log -type f -newermt "2023-01-15 00:00:00" ! -newermt "2023-12-31 23:59:59"
# Combining with other find predicates
find /backups -name "*.tar.gz" -type f -mtime +30 ! -mtime -90
For large filesystems, these optimizations help:
# Limit filesystem traversal
find / -xdev -path /proc -prune -o -type f -newermt "2023-01-15" -print
# Use -daystart for more intuitive day boundaries
find . -daystart -mtime +365