Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do you delete a file from a specific date in Linux?

Find / -name " " -mtime +1 -exec rm -f {}; Specify path, filename and time to delete the file.

How do I delete files older than 2 days Linux?

So, when you specify -mtime +1 , it looks for files older more than 1 day. Rather to explain it further, it simply says to match files modified two or more days ago. If you want to delete files older than 1 day, you can try using -mtime +0 or -mtime 1 or -mmin $((60*24)) .


This works for me:

find /path ! -newermt "YYYY-MM-DD HH:MM:SS" | xargs rm -rf

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 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

find ~ -type f ! -atime 4|xargs ls -lrt

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