How to use crontab to delete folders older than 7 days, by date.
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
- PaulyWalnuts
- 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.
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
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
Last edited by xenopeek on Thu Nov 28, 2024 2:54 pm, edited 1 time in total.
Reason: moved to scripting forum
Reason: moved to scripting forum
"Of all the things I've lost, I miss my mind the most."
Re: How to use crontab to delete folders older than 7 days, by date.
A bash script:
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.
Code: Select all
#!/bin/bash
find /path/to/base/dir/* -type d -ctime +7 -exec rm -rf {} \;
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
- PaulyWalnuts
- 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.
I'm one of the top 10 noobs.
How do 'ya make a bash script, and how do you make it executable?
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."
Re: How to use crontab to delete folders older than 7 days, by date.
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
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
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
Re: How to use crontab to delete folders older than 7 days, by date.
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:
I hope I didn't create a disaster for you.
Code: Select all
[code]#!/bin/bash
find /path/to/base/dir/* -maxdepth 0 -type d -ctime +7 -exec rm -rf {} \;
Linux Mint 21.1 Cinnamon on laptop and desktop
Re: How to use crontab to delete folders older than 7 days, by date.
Such a command or script is highly dangerous. It should have safeguards. Examples :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:
I hope I didn't create a disaster for you.Code: Select all
[code]#!/bin/bash find /path/to/base/dir/* -maxdepth 0 -type d -ctime +7 -exec rm -rf {} \;
- 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
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.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.
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.
- PaulyWalnuts
- 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.
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)
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."
Re: How to use crontab to delete folders older than 7 days, by date.
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
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
- PaulyWalnuts
- 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.
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.
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."
- PaulyWalnuts
- 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.
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.
Typed both at the terminal, and was told command not found.
"Of all the things I've lost, I miss my mind the most."
Re: How to use crontab to delete folders older than 7 days, by date.
At a command promptPaulyWalnuts wrote: ⤴Sat Nov 30, 2024 2:35 pmWould you explain the meaning of those commands, please? -ctime +7 seems self evident.
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
- PaulyWalnuts
- 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.
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.
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."
Re: How to use crontab to delete folders older than 7 days, by date.
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