How to Find or Delete Files Less Than X Minutes Old on Linux

By Lowell Heddings on July 23rd, 2017

Say you just ran some script that was supposed to move files around or download stuff, or anything else that involves files, and then you realize that you need to figure out what files just got created and where. And maybe you need to delete them.

First you’ll want to use the find command to figure out what files were created, and then using the mmin argument to specify files less than x minutes. If you want to specify only files, you can use the -type f argument to only check for files.

So if you want to check in the current folder, including subfolders, for files created in the last 5 minutes, you can use the following command:

find ./ -type f -mmin -5

To delete files we can use the -exec rm {} \; argument, which is a little confusing, but basically you’re telling the exec argument to remove the files.

So to delete all files in the current folder, including subfolders, created in the last 5 minutes, use this command:

find ./ -type f -mmin -5 -exec rm {} \;

If you wanted to specify files older than X days instead, you could use the -mtime argument — for instance, this command would delete files older than 10 days:

find ./ -type f -mtime +10 -exec rm {} \;

Much easier than looking at the man page.