5

My requirements are pretty much the same as this question: Shell script to delete directories older than n days I've got directories that look like this:

Jul 24 05:46 2013_07_24

Jul 31 22:30 2013_08_01

Sep 18 05:43 2013_09_18

Oct 07 08:41 2013_10_07

I want to remove anything older than 90 days. Based on the solution given in the aforementioned thread, I used the following in my script:

find $BASE_DIR  -type d -ctime +90 -exec rm -rf  {} \;

The script is successfully deleting the directories, but it is also failing with this error:

find: 0652-081 cannot change directory to <actual_path>:
  : A file or directory in the path name does not exist.

The only thing here that $BASE_DIR points to a location that's virtual location and the actual_path in the error message points to the actual location. There are soft links in the environment.

Community
  • 1
  • 1
citsym
  • 75
  • 8
  • Can you please post output of `find $BASE_DIR -type d -ctime +90` ?? It seems the dir it's trying to delete has already been deleted and hence can not reference symbolic link to it. – jkshah Nov 09 '13 at 05:55
  • perhaps also using the `-depth` helps? – shx2 Nov 09 '13 at 06:33
  • I need to do a bit more testing, but `-depth` seems to get rid of the error. Thanks! – citsym Nov 11 '13 at 06:01

2 Answers2

2

Try

find $BASE_DIR -mindepth 1 -maxdepth 1 -type d -ctime +90 -exec rm -rf  {} \;

This will only cover directories directly under $BASE_DIR, but it should avoid generating that error message.

Robin Green
  • 32,079
  • 16
  • 104
  • 187
  • I'm sorry. I forgot to mention that I'm using Korn shell. This is the output I get: `find: 0652-017 -mindepth is not a valid option.` – citsym Nov 11 '13 at 05:46
  • 1
    I'm a little concerned that if you don't use mindepth, the entire directory might get deleted!!!! – Robin Green Nov 11 '13 at 11:35
0
find .$BASE_DIR -type d -ctime +90 | sort -r | xargs rm -rf

sort -r will sort our directories in reverse order, so we won't try do delete external directories then internal ones.

asm0dey
  • 2,841
  • 2
  • 20
  • 33