Linux: Find files older than a given date to delete

Today I had reason to need to find all the files in a directory older than a given date and delete them.

Doing this over SSH meant it wasn’t a case of re-ordering and selecting with the mouse…

So… Here’s what I did:

First touch a file with a timestamp of the date you are looking for:<

touch -t 20140101000 /tmp/timestamp

If you want to check the size of them do:

find . -type f ! -newer timestamp -print0 | xargs -0 du -hc | tail -n1

Finally to delete them:-

find . -type f ! -newer /tmp/timestamp -delete

or

find . -type f ! -newer /tmp/timestamp -exec rm {} \;

There you go, files deleted quickly and easily.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.