Delete directories older than X days
Asked Answered
P

2

6

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 {} \;
Polonaise answered 18/12, 2014 at 19:29 Comment(0)
T
12

To delete directories in /TBD older than 1 day:

find /TBD -mtime +1 -type d | xargs rm -f -r
Tolle answered 18/12, 2014 at 20:36 Comment(12)
I also want it to delete files but if necessary I will add another line to the script specifying files. I will attempt this and see if it works.Polonaise
Nope. The two directories are still there.Polonaise
Are you sure that the user that is running the find command has the necessary privileges to delete these directories?Tolle
This is a PXE server and there are no users other than our one administrative account. Ls -lct shows that the admin account has rwx privileges on everything in the TBD directory. The script was made under the admin account using sudo so it should.Polonaise
In that case, to isolate the problem, try running just the find part of the command: find /TBD -mtime +1 -type d. Does the output of the command show the directories that should be deleted? If so, what happens if you run the rm part of the command, with the directory name, i.e. rm -f -r /path/to/directory?Tolle
I already tested doing it rm -f -r and it did remove one of the other directories so I know that works. I will try the find command.Polonaise
How would I use the find command by itself to yield a result?Polonaise
In whatever shell you are using, you should be able to run the find right from the command line, as is.Tolle
Oh, well I did the find command and specified my parameters but there is no result. So does that narrow down the solution?Polonaise
Do ls -l /TBD It should show you the dates on the directories that you are trying to delete under /TBD. Are they more than 1 day old?Tolle
Yeah as I stated in my original post I did ls -lct which gives all modification and access time stamps. They are all 3 days old through all trees of each sub directory.Polonaise
Thanks, perfect for my database backup scriptUredo
R
4

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
Royceroyd answered 18/12, 2014 at 20:9 Comment(6)
Hey should have mentioned that in the original post. I need it to delete both files AND directories so that won't work. BUT just to be curious, I tried it to get the directories to delete and it still didn't work.Polonaise
You forgot the -exec in your find command, should work if you change it.Royceroyd
Well it has to be in a specific location, dosen't the . just mark all directories? I don't want all directories deleted.Polonaise
Added the -exec, still not deleting them when I run the script.Polonaise
try adding -exec rm -rf {} \; ... if that doesn't work, then I'm not quite sure what will... see my editRoyceroyd
I am not changing the time because I'm testing these directories that are only 3 days old before finalizing time. And I already have the switches you include in your edited text. Look at my original post. find /TBD/* -mtime +1 rm -r {} \; was my first attempt at this script. I have aldo tried iterations including -rf -Rf and -r -f. None work.Polonaise

© 2022 - 2024 — McMap. All rights reserved.