How to use crontab to delete folders older than 7 days, by date.

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
User avatar
PaulyWalnuts
Level 2
Level 2
Posts: 54
Joined: Thu Apr 04, 2024 4:57 pm
Location: Oakdale, Ca.

How to use crontab to delete folders older than 7 days, by date.

Post by PaulyWalnuts »

I've setup Mint to be a NAS for my backups. I've been using an app since 2001. Inexpensive and even new versions are free. (Though I buy a new license every few years, to support them:)

Once a week, it creates a full backup of my Windows machines, and an incremental backup daily, to a new folder, with a name like 000000002, then the next folder will be 000000003...etc.

I want to delete the folder that's more than 7 days, old. NO idea, how to do that!

Note: I'm old, so keep it simple for the stupid :wink:
Last edited by xenopeek on Thu Nov 28, 2024 2:54 pm, edited 1 time in total.
Reason: moved to scripting forum
"Of all the things I've lost, I miss my mind the most."
MikeK
Level 3
Level 3
Posts: 103
Joined: Fri Jul 07, 2023 10:35 am

Re: How to use crontab to delete folders older than 7 days, by date.

Post by MikeK »

A bash script:

Code: Select all

#!/bin/bash
find /path/to/base/dir/* -type d -ctime +7 -exec rm -rf {} \;
Put it into a file, give it execute permissions, and schedule it.

EDIT: This script is recursive. See my later post for a non-recursive script.
Last edited by MikeK on Fri Nov 29, 2024 7:32 pm, edited 1 time in total.
Linux Mint 21.1 Cinnamon on laptop and desktop
User avatar
PaulyWalnuts
Level 2
Level 2
Posts: 54
Joined: Thu Apr 04, 2024 4:57 pm
Location: Oakdale, Ca.

Re: How to use crontab to delete folders older than 7 days, by date.

Post by PaulyWalnuts »

I'm one of the top 10 noobs.

How do 'ya make a bash script, and how do you make it executable?
"Of all the things I've lost, I miss my mind the most."
MikeK
Level 3
Level 3
Posts: 103
Joined: Fri Jul 07, 2023 10:35 am

Re: How to use crontab to delete folders older than 7 days, by date.

Post by MikeK »

Put the above script into a text file. You need to figure out the "/path/to/base/dir/" part. Name it "something.sh", or whatever you like. The ".sh" isn't necessary, but is helpful for identification as a script file. From a command prompt where the file lives give it execute permissions with chmod +x something.sh.

I don't use crontab so I can't advise on how to schedule something, but the "something.sh" is the thing you want to schedule. You can, alternatively, execute it from the command prompt with the command ./something.sh.
Linux Mint 21.1 Cinnamon on laptop and desktop
MikeK
Level 3
Level 3
Posts: 103
Joined: Fri Jul 07, 2023 10:35 am

Re: How to use crontab to delete folders older than 7 days, by date.

Post by MikeK »

I should have explicitly said that the script I posted is recursive. That is, it will travel down the tree for directories older than 7 days. If you only want to look at the age of the top level folders, then use this:

Code: Select all

[code]#!/bin/bash
find /path/to/base/dir/* -maxdepth 0 -type d -ctime +7 -exec rm -rf {} \;
I hope I didn't create a disaster for you.
Linux Mint 21.1 Cinnamon on laptop and desktop
Shiva
Level 4
Level 4
Posts: 270
Joined: Thu Jul 07, 2022 11:25 am

Re: How to use crontab to delete folders older than 7 days, by date.

Post by Shiva »

MikeK wrote: Fri Nov 29, 2024 7:30 pm I should have explicitly said that the script I posted is recursive. That is, it will travel down the tree for directories older than 7 days. If you only want to look at the age of the top level folders, then use this:

Code: Select all

[code]#!/bin/bash
find /path/to/base/dir/* -maxdepth 0 -type d -ctime +7 -exec rm -rf {} \;
I hope I didn't create a disaster for you.
Such a command or script is highly dangerous. It should have safeguards. Examples :
- use rm with the -i flag (-i is for interactive and will ask confirmation before deleting any folder). If it's OK after some executions, remove the flag.
- make a dry run (simulation) with the first part of the command first :

Code: Select all

find /path/to/base/dir/* -maxdepth 0 -type d -ctime +7
However, putting those together :
PaulyWalnuts wrote: Thu Nov 28, 2024 2:39 pm Once a week, it creates a full backup of my Windows machines, and an incremental backup daily, to a new folder, with a name like 000000002, then the next folder will be 000000003...etc.

I'm one of the top 10 noobs.
First of all, you should refer to your backup tool documentation or support : I don't know any serious backup tool which hasn't a feature to clean up or only keep N backups only.

If your tool is not "serious", why not use Nemo ?
If the folders of your NAS are listed in the right window, select all but last last folder (the only one that isn't > 7 days for weekly backups) and delete them. It would be a much safer procedure.
User avatar
PaulyWalnuts
Level 2
Level 2
Posts: 54
Joined: Thu Apr 04, 2024 4:57 pm
Location: Oakdale, Ca.

Re: How to use crontab to delete folders older than 7 days, by date.

Post by PaulyWalnuts »

MikeK:

LOL. Making a VirtualBox before I try any of this:)

Would you explain the meaning of those commands, please? -ctime +7 seems self evident.

This’ll be fun. The /path/to/base/dir/*, is:

/mnt/usb-TOSHIBA_EXTERNAL_USB_20240806018517F-0:0-part1/Backups/Junior/TeraByte_TBI_Backups/{9DC28570-936D-0000-0000-000000000000}/{D7C871F5-5FF6-40F4-A432-B2DA59AAB619}/0000000000000002

Is there a way to shorten this?

Then there will be another folder, of: /0000000000000003, /0000000000000004, /0000000000000005...etc. I manually deleted /0000000000000001.

Sorry, if things are a little incoherent. Early stages of oldtimerz. Dr. said to challenge the antiquated noodles. Linux certainly is challenging. (Hence the tag line)
"Of all the things I've lost, I miss my mind the most."
User avatar
majpooper
Level 8
Level 8
Posts: 2092
Joined: Thu May 09, 2013 1:56 pm
Location: North Carolina, USA

Re: How to use crontab to delete folders older than 7 days, by date.

Post by majpooper »

You mention crontab so I take you know how to edit crontab i.e. crontab -e from the terminal

add this line
0 2 * * * /usr/bin/find /path/to/directory -type d -mtime +7 -exec rm -rf {} +

/path/to/directory is the path to directory where the folders are located
User avatar
PaulyWalnuts
Level 2
Level 2
Posts: 54
Joined: Thu Apr 04, 2024 4:57 pm
Location: Oakdale, Ca.

Re: How to use crontab to delete folders older than 7 days, by date.

Post by PaulyWalnuts »

MikeK:

I think I mentioned the backups are of Windows. I googled, nemo and don't see a Windows version.

As for RTFM first, their documentation is written in stereo or something. Too hard for these fading brain cells to follow. I posted in their forums, but have received no response. Would that I could follow docs and man pages, 'ya know? Hell, it took me a week and a half to get tigervnc to work. Even tried to install tightvnc via wine:(

I bought EasUS Todo backup, 30 day return. 6 weeks later an update was released. Mind you, not an "upgrade". It removed the app, and reinstalled it. It then told me to "activate" it again. Went to my regkey.txt, copied the license, pasted it and was told it was already used. I had to go to their web site, login and found a way to update the key. Different key. 3 weeks later, there was an upgrade. Same thing.

Can't afford to keep buying that kinda garbage, on a fixed income.

majpooper:

Heard of crontab, via google. That doesn't seem too hard. Though, I'll definitely appreciate your example.
"Of all the things I've lost, I miss my mind the most."
User avatar
PaulyWalnuts
Level 2
Level 2
Posts: 54
Joined: Thu Apr 04, 2024 4:57 pm
Location: Oakdale, Ca.

Re: How to use crontab to delete folders older than 7 days, by date.

Post by PaulyWalnuts »

What is, and what's the difference between -ctime and -mtime? I asked google and she started talking about the difference between cd. and cd... and other useless info.

Typed both at the terminal, and was told command not found.
"Of all the things I've lost, I miss my mind the most."
MikeK
Level 3
Level 3
Posts: 103
Joined: Fri Jul 07, 2023 10:35 am

Re: How to use crontab to delete folders older than 7 days, by date.

Post by MikeK »

PaulyWalnuts wrote: Sat Nov 30, 2024 2:35 pmWould you explain the meaning of those commands, please? -ctime +7 seems self evident.
At a command prompt man find will pull up the manual for the find command.

* The find command, well...finds things. By default, it will search at the level invoked as well as deeper down the tree (i.e. subdirectories).
* -ctime is the last time the file's status was changed.
* -mtime is the last time the file's data was changed.
* -maxdepth 0 says to only find things at the root, so it doesn't look further down the tree.
* -type d says to only find directories.
* -exec says to execute the following command for each item found.
* rm is the remove command, so the remove command executes for each item found.
* -rf are two options for the remove command, saying to remove recursively and force removal.
* (man rm will pull up the manual.)
* {} allows the found item to be passed to the remove command.
Linux Mint 21.1 Cinnamon on laptop and desktop
User avatar
PaulyWalnuts
Level 2
Level 2
Posts: 54
Joined: Thu Apr 04, 2024 4:57 pm
Location: Oakdale, Ca.

Re: How to use crontab to delete folders older than 7 days, by date.

Post by PaulyWalnuts »

Now, THAT, I can follow!

Thanks.

I have a flash drive I can mount on the VirtualBox, so hopefully I'll get the same long winded convoluted path.
"Of all the things I've lost, I miss my mind the most."
MikeK
Level 3
Level 3
Posts: 103
Joined: Fri Jul 07, 2023 10:35 am

Re: How to use crontab to delete folders older than 7 days, by date.

Post by MikeK »

And, FWIW, the \; ends the whole thing. If you just use the semicolon it gets interpreted by the shell and won't behave like you'd expect. The backslash "escapes" the semicolon so that it's passed to the find command.
Linux Mint 21.1 Cinnamon on laptop and desktop
Post Reply

Return to “Scripts & Bash”