Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete directories older than X days

so I have looked at every single script on here regarding deleting directories older than 14 days. The Script I wrote works with files but for some reason it is not deleting the directories. So here is my scripts.

#!/bin/bash
find /TBD/* -mtim +1 | xargs rm -rf

So this code successfully deleted the FILES inside TBD but it left two directories. I checked the timestamp on them and they are atleast 2 days since last modification according to the timestamp. Specifically Dec 16 16:10 So I can't figure this out. My crontab I have running this runs every minute and logs and in the log it only shows.

+ /scripts/deletebackups.sh: :2:BASH_XTRACEFD=3xargs rm -rf
+ /scripts/deletebackups.sh: :2: BASH_XTRACEFD=3find /TBD/contents TBD/contents -mtime +1

I used contents since the contents are actually peoples name in our pxe server. I checked every file and folder INSIDE these two directories and their timestamps are the same as the parent directory as they should be but it's still not deleting.

Could it be a permissions thing? I wrote the script using sudo nano deletebackups.sh When I type ls under TBD in the far left it shows drwxr-xr-x 3 hscadministrator root 4096 DEC 16 16:10 for each of the two directories that won't delete. I'm not overly familiar with what all those letters mean.

Other iterations of this code I have already attempted are

find /TBD/* -mtime +1 rm -r {} \;
like image 213
stobiewankenobi Avatar asked Dec 18 '14 19:12

stobiewankenobi


2 Answers

To delete directories in /TBD older than 1 day:

find /TBD -mtime +1 -type d | xargs rm -f -r
like image 131
mti2935 Avatar answered Nov 15 '22 08:11

mti2935


Add -exec and -f to your find:

find /TBD/* -mtime +1 -exec rm -rf {} \;

Note, if you're looking to delete files older than 14 days, you need to change mtime:

-mtime +14
like image 33
buydadip Avatar answered Nov 15 '22 08:11

buydadip