How do you delete files older than specific date in Linux?
Asked Answered
H

5

85

I used the below command to delete files older than a year.

  find /path/* -mtime +365 -exec rm -rf {} \;

But now I want to delete all files whose modified time is older than 01 Jan 2014. How do I do this in Linux?

Hijoung answered 12/10, 2015 at 22:8 Comment(0)
T
41

You can touch your timestamp as a file and use that as a reference point:

e.g. for 01-Jan-2014:

touch -t 201401010000 /tmp/2014-Jan-01-0000

find /path -type f ! -newer /tmp/2014-Jan-01-0000 | xargs rm -rf 

this works because find has a -newer switch that we're using.

From man find:

-newer file
       File  was  modified  more  recently than file.  If file is a symbolic
       link and the -H option or the -L option is in effect, the modification time of the 
       file it points to is always used.
Tithonus answered 12/10, 2015 at 22:18 Comment(3)
Thank you. ! is as good as -not "find . -not -newer /tmp/2014-Jan-01-0000 "Hijoung
this is polluting the file system. -newer(a/b/c/m/t) timestamp can do it efficiently. please update your answerMethoxychlor
@Methoxychlor Or just delete the file when you're done. It's literally in the "temporary files" directoryDefend
I
87

This works for me:

find /path ! -newermt "YYYY-MM-DD HH:MM:SS" | xargs rm -rf
Isolative answered 25/8, 2017 at 14:11 Comment(8)
this is very nice, I don't pollute filesystem with temp timestamp file!Lithology
For just files find /path ! -type f -newermt "YYYY-MM-DD HH:MM:SS" -delete. It saves you from having to pipe everything through xargs, and having to handle filesnames with spaces or other disruptive characters.Sidesman
For what it's worth, -newermt is a non-standard extension, though on Linux systems you will typically have GNU find. This is not portable to other platforms.Conni
Adding -delete argument to the find command also works.Deciliter
Careful with @Sidesman 's command as that will delete everything that isn't a file, the ! needs to be moved to the other side of -type fThermion
You can use date --date="today" "+%Y-%m-%d" to get files older than today. Something like --date="last friday" works too! find /path ! -newermt "`date --date="today" "+%Y-%m-%d"`" | xargs rm -rfCanzonet
Corrected command find /path -type f ! -newermt "YYYY-MM-DD HH:MM:SS" -delete thanks @ThermionSidesman
damn, i should have read Shardj's comment... all folders gone... :DBridgers
T
41

You can touch your timestamp as a file and use that as a reference point:

e.g. for 01-Jan-2014:

touch -t 201401010000 /tmp/2014-Jan-01-0000

find /path -type f ! -newer /tmp/2014-Jan-01-0000 | xargs rm -rf 

this works because find has a -newer switch that we're using.

From man find:

-newer file
       File  was  modified  more  recently than file.  If file is a symbolic
       link and the -H option or the -L option is in effect, the modification time of the 
       file it points to is always used.
Tithonus answered 12/10, 2015 at 22:18 Comment(3)
Thank you. ! is as good as -not "find . -not -newer /tmp/2014-Jan-01-0000 "Hijoung
this is polluting the file system. -newer(a/b/c/m/t) timestamp can do it efficiently. please update your answerMethoxychlor
@Methoxychlor Or just delete the file when you're done. It's literally in the "temporary files" directoryDefend
M
40

This other answer pollutes the file system and find itself offers a "delete" option. So, we don't have to pipe the results to xargs and then issue an rm.

This answer is more efficient:

find /path -type f -not -newermt "YYYY-MM-DD HH:MI:SS" -delete
Methoxychlor answered 2/7, 2019 at 20:6 Comment(2)
The data fmt is wrong: use find /path -type f -not -newermt "YYYY-MM-DD HH:MI:SS" -deleteRugged
This seems to repeat an older answer from 2017, though the -delete option is an improvement.Conni
S
7
find ~ -type f ! -atime 4|xargs ls -lrt

This will list files accessed older than 4 days, searching from home directory.

Stripy answered 23/4, 2018 at 7:3 Comment(1)
Actually ! -atime 4 means "do not match any files accessed exactly 4 days ago". You'd need to use -atime +4 for "match file accessed more than 4 days ago". The full command would be find ~ -type f -atime +4 | xargs ls -lrtGarik
A
0

First I execute this

touch -t 202310080000 /tmp/olddate ---> this creates a file with the specified date

find "path to which you want to apply search" -type f ! -newer /tmp/olddate -delete -----> this deletes files from the path where you want with reference to the file created at the certain date.

Abigailabigale answered 11/10, 2023 at 4:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.