Delete directories older than X days

24,615

Solution 1

To delete directories in /TBD older than 1 day:

find /TBD -mtime +1 -type d | xargs rm -f -r

Solution 2

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
Share:
24,615
stobiewankenobi
Author by

stobiewankenobi

Updated on December 19, 2020

Comments

  • stobiewankenobi
    stobiewankenobi over 3 years

    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
    stobiewankenobi over 9 years
    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.
  • buydadip
    buydadip over 9 years
    You forgot the -exec in your find command, should work if you change it.
  • stobiewankenobi
    stobiewankenobi over 9 years
    Well it has to be in a specific location, dosen't the . just mark all directories? I don't want all directories deleted.
  • stobiewankenobi
    stobiewankenobi over 9 years
    Added the -exec, still not deleting them when I run the script.
  • buydadip
    buydadip over 9 years
    try adding -exec rm -rf {} \; ... if that doesn't work, then I'm not quite sure what will... see my edit
  • stobiewankenobi
    stobiewankenobi over 9 years
    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
    stobiewankenobi over 9 years
    Nope. The two directories are still there.
  • mti2935
    mti2935 over 9 years
    Are you sure that the user that is running the find command has the necessary privileges to delete these directories?
  • stobiewankenobi
    stobiewankenobi over 9 years
    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.
  • mti2935
    mti2935 over 9 years
    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?
  • stobiewankenobi
    stobiewankenobi over 9 years
    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
    stobiewankenobi over 9 years
    How would I use the find command by itself to yield a result?
  • mti2935
    mti2935 over 9 years
    In whatever shell you are using, you should be able to run the find right from the command line, as is.
  • stobiewankenobi
    stobiewankenobi over 9 years
    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
    stobiewankenobi over 9 years
    Oh, well I did the find command and specified my parameters but there is no result. So does that narrow down the solution?
  • mti2935
    mti2935 over 9 years
    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?
  • stobiewankenobi
    stobiewankenobi over 9 years
    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.
  • Alejandro Aranda
    Alejandro Aranda about 5 years
    Thanks, perfect for my database backup script