-2

I have a directory with a bunch of folders named in yyyy-MM-dd format. I want to delete all the folders where the yyyy-MM-dd string is older than X days.

# ls
2018-01-01
2018-01-02
2018-01-03
...
2018-02-01
2018-03-05
...
2019-01-02
2019-01-02

Is it possible to find like this?

Update:

This question is not a duplicate of Delete files older than specific date in linux because that talks about finding files by mtime and I want to find by file name where the file name matches a date string. For example, the name of a directory could be 2012-01-01 but it's mtime could be 2018-03-11. This is why mtime or any of the other date/time metadata will not work for my needs.

IMTheNachoMan
  • 5,343
  • 5
  • 40
  • 89
  • 1
    Did you try anything for yourself? Post those efforts into the question – Inian Jan 10 '19 at 03:18
  • I didn't because I'm not even sure where to start. I mean I know how to find files and all that, and I know about `mtime` or `ctime` but that won't work for me... – IMTheNachoMan Jan 10 '19 at 03:22
  • Which platform are you working on? MacOS, Windows10, Ubuntu or other Linux/Unix system? – tshiono Jan 10 '19 at 03:32
  • `linux` and `bash`. – IMTheNachoMan Jan 10 '19 at 03:39
  • @tripleee I updated the question with details on why it is **not** a duplicate of the one you marked. – IMTheNachoMan Jan 10 '19 at 14:33
  • This is unlikely to be reopened until you demonstrate some coding effort of your own. Feel free to ping me for a possible reopen once you have made an [edit] to show what you have tried so far and where exactly you are stuck. – tripleee Jan 10 '19 at 17:05
  • Having said that, we have a cubic light-year of very similar questions, so you are likely to find (or be directed to) another duplicate once we see what precisely you find challenging. – tripleee Jan 10 '19 at 17:07
  • I'm not entirely sure how to add more detail. I haven't tried anything because I don't even know where to start. But I think one of the answers below might work so I'm going to give that a try and see. – IMTheNachoMan Jan 11 '19 at 02:38

2 Answers2

2

You can get the current date in days since the UNIX epoch by

echo $(($(date +%s)/86400))

You can do the same for a string formatted the same way that you have above:

echo $(($(date +%s --date "2018-01-01"))/86400)

Then the number of days between "today" and an arbitrary date:

echo $(($(date +%s)/86400 - $(date +%s --date "2018-01-01")/86400))

Wrap it up in a loop, and you should be good to go:

for DIR in `ls -1`
do
    # if the directory name is parseable as a date...
    if date "$DIR" &>/dev/null
    then
        age=$(($(date +%s)/86400 - $(date +%s --date "$DIR")/86400))
        echo "$DIR is $age days old"
        # do something based on its age
        if [ $age -gt 30 ]
        then
            #rm -fr "$DIR"
            echo $DIR
        fi
    fi
done
dtanabe
  • 1,611
  • 9
  • 18
0

As always mentioned, bash does not support a built-in date&time function. Although date command is sometimes useful to compensate it, the command has a lot of variations and often causes compatibiliy problems. It is recommended to make use of other programming languages which natively support the date&time arithmetic.
Here's an example in python:

#!/usr/bin/python

import glob
import re
import datetime
import shutil
import os

retention = 100     # or whatever period you want to preserve
now = datetime.datetime.now()

for dir in glob.glob("*-*-*"):
    if re.search("^\d{4}-\d{2}-\d{2}$", dir):
        dt = datetime.datetime.strptime(dir, "%Y-%m-%d")
        if ((now - dt).days > retention):
            shutil.rmtree(dir)

Hope this helps.

tshiono
  • 21,248
  • 2
  • 14
  • 22