76

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?

Mike Pierce
  • 1,390
  • 1
  • 12
  • 35
VRK
  • 1,206
  • 3
  • 13
  • 27

4 Answers4

77

This works for me:

find /path ! -newermt "YYYY-MM-DD HH:MM:SS" | xargs rm -rf
Paul Spaulding
  • 1,241
  • 1
  • 13
  • 7
  • 3
    this is very nice, I don't pollute filesystem with temp timestamp file! – andrej Apr 25 '18 at 09:13
  • 12
    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. – jbo5112 Sep 20 '19 at 20:33
  • 1
    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. – tripleee Mar 24 '21 at 07:49
  • Adding -delete argument to the find command also works. – John Ronald Oct 29 '21 at 08:53
  • 1
    Careful with @jbo5112 's command as that will delete everything that isn't a file, the ! needs to be moved to the other side of -type f – Shardj Mar 24 '23 at 12:46
  • 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 -rf` – Mayra Delgado Apr 26 '23 at 08:57
37

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.
  • 3
    Thank you. ! is as good as -not "find . -not -newer /tmp/2014-Jan-01-0000 " – VRK Oct 13 '15 at 16:22
  • 3
    this is polluting the file system. -newer(a/b/c/m/t) timestamp can do it efficiently. please update your answer – yoga Jul 02 '19 at 20:00
  • @yoga Or just delete the file when you're done. It's literally in the "temporary files" directory – Michael Mrozek May 02 '21 at 03:23
30

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
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
yoga
  • 710
  • 5
  • 11
  • 1
    The data fmt is wrong: use find /path -type f -not -newermt "YYYY-MM-DD HH:MI:SS" -delete – josef Oct 14 '19 at 05:30
  • This seems to repeat [an older answer from 2017](https://stackoverflow.com/a/45883352/874188), though the `-delete` option is an improvement. – tripleee Mar 24 '21 at 07:50
7
find ~ -type f ! -atime 4|xargs ls -lrt

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

bunty
  • 71
  • 1
  • 2