0
0
Linux CLIscripting~5 mins

find by modification time in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to find files that were changed recently or a long time ago. The Linux find command can search files based on when they were last modified. This helps you quickly locate files by their modification date.
When you want to delete files not changed in the last 30 days to free up space
When you need to back up files modified in the last 24 hours
When you want to check which files were changed today in a project folder
When you want to list files modified more than a year ago for archiving
When you want to find files modified exactly 7 days ago for review
Commands
This command finds files in /home/user/documents modified less than 1 day ago. The -mtime -1 means files changed within the last 24 hours.
Terminal
find /home/user/documents -mtime -1
Expected OutputExpected
/home/user/documents/report.txt /home/user/documents/notes.txt
-mtime - Find files by modification time in days
This command finds files modified more than 30 days ago in /home/user/documents. The +30 means files older than 30 days.
Terminal
find /home/user/documents -mtime +30
Expected OutputExpected
/home/user/documents/old_data.csv /home/user/documents/archive.zip
-mtime - Find files by modification time in days
This command finds files modified exactly 7 days ago in /home/user/documents. The 7 means files changed between 7 and 8 days ago.
Terminal
find /home/user/documents -mtime 7
Expected OutputExpected
/home/user/documents/weekly_report.docx
-mtime - Find files by modification time in days
Key Concept

If you remember nothing else from this pattern, remember: -mtime lets you find files by how many days ago they were last changed.

Common Mistakes
Using -mtime 1 to find files changed in the last 1 day
-mtime 1 finds files changed between 1 and 2 days ago, not within the last day
Use -mtime -1 to find files changed less than 1 day ago
Confusing + and - signs with -mtime
+ means older than, - means newer than, missing sign means exactly that many days ago
Use +N for older than N days, -N for newer than N days, N for exactly N days ago
Summary
Use find with -mtime to search files by modification time in days.
-mtime -N finds files modified within the last N days.
-mtime +N finds files modified more than N days ago.
-mtime N finds files modified exactly N days ago.