Linux find
: Find Files in a Folder That Changed Today
The find
command in Linux is a powerful tool for searching files and directories based on various criteria, including modification time. If you need to find files in a specific folder that were modified today, you can use the -mtime
option.
Basic Command
To list all files in a directory that were modified within the last 24 hours, run:
find /path/to/search -mtime -1
How It Works
find
– The command used to search for files and directories./path/to/search
– Replace this with the directory where you want to perform the search.-mtime -1
– Finds files modified within the last 24 hours.
Understanding -mtime
Values
-mtime 0
→ Finds files modified today (since the last midnight).-mtime -1
→ Finds files modified in the last 24 hours.-mtime +1
→ Finds files modified more than a day ago.
Include Subdirectories
By default, find
searches recursively within all subdirectories. If you want to restrict the search to the current folder only, use:
find /path/to/search -maxdepth 1 -mtime -1
Filtering by File Type
To find only files (excluding directories):
find /path/to/search -type f -mtime -1
To find only directories:
find /path/to/search -type d -mtime -1
Sorting Results
If you want to sort the results by modification time (newest first), you can combine find
with ls
:
find /path/to/search -mtime -1 -type f -exec ls -lt {} +
Finding Files Modified in the Last X Hours
If you need more precision (e.g., finding files modified within the last 6 hours), use the -mmin
option:
find /path/to/search -mmin -360
(360
minutes = 6 hours)
Executing a Command on Found Files
To delete files modified within the last 24 hours, use:
find /path/to/search -mtime -1 -type f -delete
⚠️ Be careful with -delete
—there is no undo!
Alternatively, to compress the found files:
find /path/to/search -mtime -1 -type f -exec tar -czf modified_today.tar.gz {} +
Conclusion
The find
command is an essential tool for system administrators and developers who need to locate and manage recently modified files efficiently. Whether you’re looking for logs, recent uploads, or system changes, these techniques will help streamline your workflow.
Would you like me to add a troubleshooting section or more examples? 🚀