Search code examples
linuxbashgnu-findutils

How to find directories older than date when the date is encoded to dirname as yyyy-mm-dd


I want to purge old folders with all the content by GNU find. I'm familiar with -mtime but the time stamp of the directories is usually corrupted by rsync - not important. Luckily, the time stamp is encoded to the directory name as yyyy-mm-dd.

How can I do the same by using the dirname instead of time stamp? Optimal read solution is preferred.

EDIT: Corrupted time stamps:

drwxr-xr-x 2 user user 8192 Aug 23 11:12 2016-05-03
drwxr-xr-x 2 user user 8192 Aug 23 11:12 2016-05-04
drwxr-xr-x 2 user user 8192 Aug 23 11:12 2016-05-05

The files inside the dirs have correct time stamps.

  1. idea: purge the files with find -mtime and then (second round) purge the empty dirs. Most likely it is not possible to perform both in one round, since -empty would apply to files as well.

  2. idea: fix the time stamps of the directories in one round (according to their name) and then purge all by find -mtime in another round. But the regular rsync will corrupt that again. The cron jobs must be tuned against race conditions.

  3. idea: convert -mtime +150 to yyyy-mm-dd (using date -d "-150 days") and then compare this string with the folder name, as it was suggested in the answer by @xvan

I ask for help finding the best way.


Solution

  • You may use bash lexicographical compare:

    if [[ "2010-01-01" < "2011-02-02" ]]
      then echo "yes"
    fi
    

    EDIT: It's a bit hard to escape <, this worked for me.

    find . ! -path . -type d -exec bash -c "[[ {} < 2010-02-02 ]]"  \; -print