0

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.

SkyRaT
  • 285
  • 3
  • 6
  • 1
    Please show us what you have tried so far. Stack Overflow is not a code writing service, but people are willing to help you if you at least try to solve the problem at your own. Please see also [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask). — I would just read the folder names, split it using `${name#-}` into day, month, year and then compare the dates. – Martin Nyolt Aug 29 '16 at 12:47

1 Answers1

3

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
xvan
  • 4,554
  • 1
  • 22
  • 37
  • Yes, this is the right way, combining with my nr.3 idea it does the job. The final command is: `find . ! -path . -type d -exec bash -c "[[ {} < $(date -d "-30 days" +%Y-%m-%d) ]]" \; -print`. Thank you. – SkyRaT Nov 01 '17 at 13:21