Finding recently modified files is a helpful parameter when troubleshooting your code or server.
What log files were modified? What files changed when I ran this command? The versatile find command can help you get the answers.
The command below will find all the files that have been modified in the last five minutes in the current directory.
find . -type f -mmin -5
That's just one example. Let me share how you can list files that are accessed and created in the last n minutes/days in detail.
Finding modified files in Linux
Before jumping to the explanatory part, first, I'd like to share the syntax of how you can use the find command to find files that are modified at the last n minute.
find [path] -type f -mmin n
Here, n indicates how many minutes you want to check for. But you also have some options such as:
- -n will check for files modified in less than n minutes
- +n will check for files modified in more than n minutes
- n will check for files modified exactly n minutes ago
Similarly, you can also use
-mtime instead of
-mmin to check for files modified days ago.
Finding files modified in the last 5 minutes
So let's suppose I want to list find files that are just modified in the last 5 minutes in the var directory; my command will be as follows:
find .var/ -type f -mmin -5
As you can see, it just throws files, and I don't find the given list useful and readable.
In this case, I'll append
-ls to have a much cleaner look and more info such as the file owner, permissions, and modification time.
find .var/ -type f -mmin -5 -ls
-lsis one such action. You don't necessarily need to use find-exec or xargs for the ls command.
Finding files modified in the last n days
To find modified files in the last n days instead of n minutes, you just have to use
-mtime instead of
Suppose I want to find files that have been modified in the last one day, my command would be:
find /media/sagar/HDD/Downloads -type f -mtime -1 -ls
Find files older than X days
You can use the
mtime parameter to find older files that have not been modified recently.
Let's say you want to find files older than 30 days in the current directory. Use this command:
find . -mtime +30
Find modified directories in the last n minutes or days
The find command can also bring a list of modified directories.
Just change -type f with -type d, which will let you search for directories instead of files.
For demonstration, I'll be showing how you can get the list of modified directories under /.cache/mozilla/firefox:
find .cache/mozilla/firefox/ -type d -mmin -5 -ls
Similarly, you can find directories on which you worked a few days ago.
find Downloads/ -type d -mtime -1 -ls
Finding files that have been recently accessed or created
So how about finding files that were accessed or created last n minutes?
To find files that were accessed in the last n minutes, you'll have to use
-amin instead of
To find files that were accessed in the last 5 minutes inside my preferred directory, I'd be using the given command:
find /media/sagar/HDD/Downloads -type f -amin -5 -ls
In the same way, you can also find files created in the last n minutes by using
-cmin instead of
find /media/sagar/HDD/Downloads -type f -cmin -5 -ls
Basically, there is no limit to the use cases. You can modify it from -n to +n and you can search for older files. Use the
ctime and you can get recently created or ancient files.
Another common example is finding files by their name.
Learning the basics allows you to use the find command as per your need. I believe this tutorial gave you enough understanding of using the time parameter of the find command.