User avatar
TommyG007
Posts: 63
Joined: Fri Jun 26, 2020 4:30 pm
Location: South Wales, UK

Automatically delete files older than 30 days?

Wed Aug 05, 2020 6:47 pm

Hi all,

I'm looking to set up a cron job to automatically delete files from my NAS folder older than 30 days.

After some Googling I found this old thread from 2013: viewtopic.php?t=63326

Does anyone know if the arguments are still valid in modern Raspberry OS?

I've edited it for my use, shown below.

0 1 * * * /home/pi/OMVNAS/Backups/'Raspberry Pi/Den Top Raspberry Pi4 Backup Images'/* -mtime +30 -exec rm {} \;

As I understand it, the cron job will run every day at 1.00am, and delete any files within the 'Den Top Raspberry Pi4 Backup Images' folder older than 30 days.

Will that work as expected?

Thanks.
Last edited by TommyG007 on Wed Aug 05, 2020 7:27 pm, edited 1 time in total.

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: Automatically delete files older than 30 days

Wed Aug 05, 2020 7:15 pm

You seem to have lost the "find" command !
I would expect trouble from all those spaces in the directory names as well.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
TommyG007
Posts: 63
Joined: Fri Jun 26, 2020 4:30 pm
Location: South Wales, UK

Re: Automatically delete files older than 30 days

Wed Aug 05, 2020 7:20 pm

I thought maybe the find command wasn't needed as it wasn't shown in the last working example on the old thread?

I've also put comments around the folder names that have spaces, which works for my other script. :D

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: Automatically delete files older than 30 days

Wed Aug 05, 2020 7:43 pm

TommyG007 wrote:
Wed Aug 05, 2020 7:20 pm
I thought maybe the find command wasn't needed as it wasn't shown in the last working example on the old thread?
I suggest you go back and check again :roll: It is the find command that actually does all the work !
I've also put comments around the folder names that have spaces, which works for my other script. :D
The ' (called quotes not comments) are in unusual places. It's normally to quote the whole string and not have the * on the end as
find assumes you want it to look at the files in the directory !

Disclaimer since this has an "rm" command in that could delete more than you expect: This is untested and if it goes wrong on your machine it's not my fault.

Code: Select all

0 1 * * * /usr/bin/find "/home/pi/OMVNAS/Backups/Raspberry Pi/Den Top Raspberry Pi4 Backup Images/"  -mtime +30 -exec rm {} \;
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
TommyG007
Posts: 63
Joined: Fri Jun 26, 2020 4:30 pm
Location: South Wales, UK

Re: Automatically delete files older than 30 days?

Wed Aug 05, 2020 7:50 pm

I don't know how I missed the find command?! lol, thanks for that.

Thanks for fixing the string, I'll give it a go. It's only for deleting older bakup images so it's not the end of the world if it deletes the whole folder (I have a master backup image on a spare SD card).

Thanks again! :D

Return to “Beginners”