0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Find Files Modified Today

Use the command find . -type f -newermt "$(date +%Y-%m-%d)" in Bash to find files modified today.
📋

Examples

InputFiles in current directory: file1.txt (modified today), file2.txt (modified yesterday)
Output./file1.txt
InputFiles in current directory: report.pdf (modified today), image.png (modified today), notes.txt (modified last week)
Output./report.pdf ./image.png
InputNo files modified today
Output
🧠

How to Think About It

To find files modified today, we compare each file's modification date with today's date. We use the 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

1
Get today's date in YYYY-MM-DD format.
2
Use the find command to search current directory recursively.
3
Filter files that are regular files and modified after today's date at midnight.
4
Print the list of these files.
💻

Code

bash
#!/bin/bash

# Find files modified today
find . -type f -newermt "$(date +%Y-%m-%d)"
Output
./file1.txt ./report.pdf ./image.png
🔍

Dry Run

Let's trace finding files modified today in a directory with files file1.txt (modified today) and file2.txt (modified yesterday).

1

Get today's date

date +%Y-%m-%d returns 2024-06-15

2

Run find command

find . -type f -newermt "2024-06-15"

3

Output files modified after midnight

./file1.txt

FileModified DateIs Modified Today?
./file1.txt2024-06-15 10:00Yes
./file2.txt2024-06-14 15:00No
💡

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

Using find with touch reference file
bash
#!/bin/bash

touch -d "00:00" /tmp/start_of_today
find . -type f -newer /tmp/start_of_today
rm /tmp/start_of_today
Creates a temporary file with today's midnight timestamp and finds files newer than it; requires cleanup.
Using find with -mtime 0
bash
find . -type f -mtime 0
Finds files modified in the last 24 hours, which may include files modified late yesterday depending on current time.

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.

ApproachTimeSpaceBest For
-newermt with dateO(n)O(k)Accurate files modified since midnight
Reference file with -newerO(n)O(k)When date parsing is limited
-mtime 0O(n)O(k)Quick approximate last 24 hours
💡
Use find . -type f -newermt "$(date +%Y-%m-%d)" for precise files modified since midnight today.
⚠️
Using -mtime 0 can miss files modified exactly at midnight or include files from late yesterday.