Delete files using date criteria - Linux

Status
Not open for further replies.
Joined
Aug 15, 2005
Messages
2,329
Location
Lexington, KY
Linux gurus, I have one for you:

How can I delete all files in a folder (or tree) that match a simple date criteria, such as modified "after xx/xx/xx" or "before xx/xx/xx" at the Linux command shell?

If there isn't a command that can do this directly perhaps there is a standard program that can list the qualifying files, and this list can be used to actually delete with a standard program like rm.
 
This will work fine, but do not run as root and make darn sure to search only the path you really want to delete files from - you've been warned.

find /path/to/files* -mtime +5 -exec rm {} \;

-mtime is in days "-mtime +5" will match all files older than 5 days.

run "find /path/to/files* -mtime +5" FIRST to see what you're about to delete.

Wayne
 
Yep, find -mtime is the ticket. If you're going to trawl through a huge number of files, you might want to use xargs as well.

In any case, "man find" is your friend.
 
wwillson, Familyguy, thank you!

The examples are very helpful.

I hadn't thought of using 'find' for this purpose.
man find has been added to my mental notebook.

My thoughts got stuck with the more primitive programs like ls and derivatives.
 
You can always mv the files to another location if you want to actually eye-ball them before deleting them, if the idea of having a command decide what gets deleted.

I.E. find /path/to/files* -mtime +5 -exec mv {} /templocation \;
 
Status
Not open for further replies.
Back
Top