Ubuntu – How to remove only files created before a specific date and time

bashdatefilesrmscripts

l would like to remove from my directory files which have been created before 04/29/2018 at 4:00 pm.

Thank you

Best Answer

Linux doesn't keep record of creation time, there are only 3 time records for files: last access, last modification of contents and last modification of the inode. So you are left with 3 options:

To delete all files modified before 04/29/2018 at 4:00 pm:

find . -type f ! -newermt '04/29/2018 16:00:00' -exec rm -f {} \;

To delete all files accessed before 04/29/2018 at 4:00 pm:

find . -type f ! -newerat '04/29/2018 16:00:00' -exec rm -f {} \;

To delete all files which had their permission changed before 04/29/2018 at 4:00 pm:

find . -type f ! -newerct '04/29/2018 16:00:00' -exec rm -f {} \;

You probably wouldn't want to run the above commands as root, and remember to backup any important files.

Important note!

You should treat date values with caution. Even though I did a complete format to my hard drive last month, I have some files in my home directory dating back to 2014!

Sources: [1][2][3]