Delete files older than specific date in linux

雨燕双飞 提交于 2019-11-29 01:11:44

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.

This works for me:

find /path ! -newermt "YYYY-MM-DD HH:MM:SS" | xargs rm -rf
find ~ -type f ! -atime 4|xargs ls -lrt

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

The accepted answer pollutes the file system and find itself offers delete. 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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!