Learn how to search for files whose last modification time is older than n minutes using bash in linux.

For the latest project i've been working on (removeimagebg.io) I had to implement a feature where basically, all of the uploaded files in the server should be deleted where from the date and time of upload, there are 10 minutes difference to the current date and time. This happens basically because we can't afford too much storage and it could become a trouble sooner or later due to the privacy and all the related stuff, so the solution is basically to program a cronjob that runs every 15 minutes, searches the files where 10 minutes have passed since the upload time and delete them, that simple.

In this article, I will share with you a very simple command to search for files older than x minutes from the current time and delete them using bash.

Finding the files

Instead of working with the programming language of the project, I decided to simply write a very simple script that does the mentioned algorithm, so I had to research as first, how to find the files that are older than 10 minutes from the current time. To do this, I simply used the find command:

find ./your-directory -daystart -maxdepth 1 -mmin +10 -type f

The command is described as follows:

  • ./your-directory: the absolute or relative directory where the search should be executed. Personally I recommend to use absolute paths, it will prevent possible headaches in the future.
  • -daystart: measure times from the beginning of today.
  • -maxdepth 1: limiting search to the specific directory given as first argument. You can remove this if you have subdirectories where the search should be executed as well.
  • -mmin +<minutes>: The mmin option is used to find files/directories with last modified in minutes (replace minutes with the amount of minutes as an integer). In our case, we want to search files older than 10 minutes. If you need to search for files older than 20 minutes, you would simply use -mmin +20.
  • -type f: Limit search results to files.

It's safe to run the previous command as it will only display the list of files that match the search, they will not be deleted or modified, for example a possible output would be:


Filtering files

If you need to filter the files by extension, filename or something like that, don't forget that you can add a filter making use of the -iname parameter and use the asterisk as a wildcard placeholder:

# Search for jpeg files older than 10 minutes
find ./your-directory -daystart -maxdepth 1 -mmin +10 -type f -iname "*.jpeg"

Deleting the files

Now as I mentioned, the last step of my script was to simply remove the files that match the search. Fortunately, the most problematic part was done. To remove the result of your search, all you need to do is to add the -delete argument to the search command:

find ./your-directory -daystart -maxdepth 1 -mmin +10 -type f -delete

Happy coding ❤️!

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.