Finding Files by Date in Linux/Unix: A Comprehensive ‘find’ Command Guide

The find command in Linux and Unix-like systems allows you to search for files based on various criteria, including modification time. Here are some examples of how to use find to search for files by date:

1. Find Files Modified Today

To find files that were modified today in the current directory and its subdirectories:

find . -mtime 0
find by date

2. Find Files Modified Yesterday

To find files that were modified yesterday in the current directory and its subdirectories:

find . -mtime 1

3. Find Files Modified Within the Last 7 Days

To find files that were modified within the last 7 days in the current directory and its subdirectories:

find . -mtime -7

4. Find Files Not Modified Within the Last 30 Days

To find files that were not modified within the last 30 days in the current directory and its subdirectories:

find . -mtime +30

5. Find Files Modified Between Two Dates

To find files that were modified between two dates, you can use the -newer and -not -newer options. For example, to find files modified between August 1st and August 5th, 2024, you can use the following steps:

First, create reference files with the timestamps of the start and end dates:

touch -d "2024-08-01 00:00:00" start
touch -d "2024-08-05 23:59:59" end

Then, use find to locate files modified after the start date but before the end date:

find . -newer start ! -newer end

6. Find Files Accessed Within the Last 24 Hours

To find files that were accessed within the last 24 hours in the current directory and its subdirectories:

find . -atime 0

7. Find Files Not Accessed Within the Last 30 Days

To find files that were not accessed within the last 30 days in the current directory and its subdirectories:

find . -atime +30

8. Find Files Changed Within the Last 24 Hours

To find files whose metadata (such as permissions or ownership) were changed within the last 24 hours in the current directory and its subdirectories:

find . -ctime 0

9. Find Files Whose Metadata Was Not Changed Within the Last 30 Days

To find files whose metadata (such as permissions or ownership) were not changed within the last 30 days in the current directory and its subdirectories:

find . -ctime +30

10. Find Files Created Within the Last 24 Hours

To find files that were created within the last 24 hours in the current directory and its subdirectories, you can use the -newermt option with a reference file:

touch -d "2024-08-10 00:00:00" ref
find . -newermt ref ! -name "ref"

Example Usage

Let’s say you want to find all files that were modified within the last 7 days in the /home/user/documents directory and its subdirectories:

find /home/user/documents -mtime -7

Conclusion

These examples demonstrate how to use the find command to search for files based on modification time, access time, and change time. You can combine these options with other criteria, such as file name, size, and type, to create more complex queries. If you have specific requirements or need more advanced functionality, feel free to ask, and I can provide more tailored examples.

This entry was posted in Find Command, File & Directory and tagged , , , . Bookmark the permalink.