Bash Script to Find Files Modified Today
find . -type f -newermt "$(date +%Y-%m-%d)" in Bash to find files modified today.Examples
How to Think About It
find command with a date filter that selects files newer than the start of today. This way, only files changed since midnight are listed.Algorithm
Code
#!/bin/bash # Find files modified today find . -type f -newermt "$(date +%Y-%m-%d)"
Dry Run
Let's trace finding files modified today in a directory with files file1.txt (modified today) and file2.txt (modified yesterday).
Get today's date
date +%Y-%m-%d returns 2024-06-15
Run find command
find . -type f -newermt "2024-06-15"
Output files modified after midnight
./file1.txt
| File | Modified Date | Is Modified Today? |
|---|---|---|
| ./file1.txt | 2024-06-15 10:00 | Yes |
| ./file2.txt | 2024-06-14 15:00 | No |
Why This Works
Step 1: Get today's date
The command date +%Y-%m-%d returns the current date in year-month-day format, which is used as a reference point.
Step 2: Use find with -newermt
The -newermt option in find selects files modified more recently than the given date string.
Step 3: Filter regular files
The -type f option ensures only regular files are listed, excluding directories or special files.
Alternative Approaches
#!/bin/bash touch -d "00:00" /tmp/start_of_today find . -type f -newer /tmp/start_of_today rm /tmp/start_of_today
find . -type f -mtime 0
Complexity: O(n) time, O(k) space
Time Complexity
The find command checks each file once, so time grows linearly with the number of files (n).
Space Complexity
The command uses minimal extra memory, storing only matching file paths (k), so space is O(k).
Which Approach is Fastest?
Using -newermt is efficient and clear; creating a reference file adds overhead; -mtime 0 is simpler but less precise.
| Approach | Time | Space | Best For |
|---|---|---|---|
| -newermt with date | O(n) | O(k) | Accurate files modified since midnight |
| Reference file with -newer | O(n) | O(k) | When date parsing is limited |
| -mtime 0 | O(n) | O(k) | Quick approximate last 24 hours |
find . -type f -newermt "$(date +%Y-%m-%d)" for precise files modified since midnight today.-mtime 0 can miss files modified exactly at midnight or include files from late yesterday.