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 {} \;
stobiewankenobi
  • 694
  • 1
  • 10
  • 16

2 Answers2

12

To delete directories in /TBD older than 1 day:

find /TBD -mtime +1 -type d | xargs rm -f -r
mti2935
  • 11,465
  • 3
  • 29
  • 33
  • 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. – stobiewankenobi Dec 18 '14 at 20:48
  • Nope. The two directories are still there. – stobiewankenobi Dec 18 '14 at 20:49
  • Are you sure that the user that is running the find command has the necessary privileges to delete these directories? – mti2935 Dec 18 '14 at 20:56
  • 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. – stobiewankenobi Dec 18 '14 at 21:00
  • 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`? – mti2935 Dec 18 '14 at 21:02
  • 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. – stobiewankenobi Dec 18 '14 at 21:07
  • How would I use the find command by itself to yield a result? – stobiewankenobi Dec 18 '14 at 21:08
  • In whatever shell you are using, you should be able to run the find right from the command line, as is. – mti2935 Dec 18 '14 at 21:13
  • Oh, well I did the find command and specified my parameters but there is no result. So does that narrow down the solution? – stobiewankenobi Dec 18 '14 at 21:32
  • 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? – mti2935 Dec 18 '14 at 21:45
  • 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. – stobiewankenobi Dec 18 '14 at 21:48
  • Thanks, perfect for my database backup script – Alejandro Aranda Apr 25 '19 at 15:30
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
buydadip
  • 8,890
  • 22
  • 79
  • 154
  • 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. – stobiewankenobi Dec 18 '14 at 20:12
  • You forgot the -exec in your find command, should work if you change it. – buydadip Dec 18 '14 at 20:27
  • Well it has to be in a specific location, dosen't the . just mark all directories? I don't want all directories deleted. – stobiewankenobi Dec 18 '14 at 20:27
  • Added the -exec, still not deleting them when I run the script. – stobiewankenobi Dec 18 '14 at 20:28
  • try adding -exec rm -rf {} \; ... if that doesn't work, then I'm not quite sure what will... see my edit – buydadip Dec 18 '14 at 20:38
  • 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. – stobiewankenobi Dec 18 '14 at 21:31