0

This is slightly different question/variation to this question

  1. Remove all directories whose created on time is older than N days.
  2. Don't consider the sub-directories/files within the directory.

Ex:

drwxrwxr-x 6 test test 4096 Aug 26 14:42 2.1.6-SNAPSHOT_201408261440_1
drwxrwxr-x 6 test test 4096 Sep  1 05:13 2.1.6-SNAPSHOT_201408281233_1
drwxrwxr-x 6 test test 4096 Sep  1 10:06 2.1.6-SNAPSHOT_201409011001_1
drwxrwxr-x 6 test test 4096 Sep  1 15:58 2.1.6-SNAPSHOT_201409011554_1
drwxrwxr-x 6 test test 4096 Sep  2 15:11 2.2.0-SNAPSHOT_201409021508_1
drwxrwxr-x 6 test test 4096 Sep  2 15:18 2.2.0-SNAPSHOT_201409021515_1
drwxrwxr-x 6 test test 4096 Sep  5 13:05 2.2.0-SNAPSHOT_201409051303_1
drwxrwxr-x 6 test test 4096 Sep  5 15:32 2.1.6-SNAPSHOT_201409051528_1
drwxrwxr-x 6 test test 4096 Sep  8 11:54 2.1.6-SNAPSHOT_201409081152_1

I should be able to delete all folders in this path whose created on is older than N days. The inside folder might have updated files/sub-directories which are new, it doesn't matter.

Community
  • 1
  • 1
RaceBase
  • 18,428
  • 47
  • 141
  • 202

1 Answers1

3

Assuming you want to delete old directories:

N=4
find . -type d -mtime +$N -exec rm -fr {} +

A depth-first search would ensure that sub-directories are removed before the directories that contain them, but might end up altering the modify time on the directory before find looks at it, which would mean that directories that were old are no longer counted as old. However, conversely, the rm may end up trying to remove directories it has already removed, but the -f option ensures this does not end up with error reports.

You might want to consult Explaining find … -mtime command for information about the meaning of +$N vs -$N vs $N (where N is assumed to hold a number).

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278