Popular Searches

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

Bash Shell

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.

Lowell Heddings Lowell Heddings
Lowell is the founder and CEO of LifeSavvy Media. He’s been running the show since creating How-To Geek back in 2006. Over the last decade, Lowell has personally written more than 1000 articles which have been viewed by over 250 million people. Prior to starting How-To Geek, Lowell spent 15 years working in IT doing consulting, cybersecurity, database management, and programming work. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.