Search code examples
linuxfinddelete-file

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


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?


Solution

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