[solved] Bash to remove directories?

Commissario
Joined
16 Oct 2002
Posts
342,803
Location
In the radio shack
Morning all,

One of my Raspberry Pis does an rsync backup of a specific directory to my NAS once a day. I only want to keep the thirty most recent directories.

Using find like this is all very good except that some of the files copied over have a modified date older than 31 days so boom, they get deleted.

Code:
find /home/pi/spiderback/spider -type f -mtime +31 -exec rm -f {} \;

Can anyone help please, I simply want to keep the most recent thirty folders in the backup location, anything else should be blitzed.

Thanks.
 
You know how when you start a thread asking for some help, you hope that a specific person will reply?
:)

Thank you @Dj_Jestar, I will report back in a months time.

I saw the Ronseal reference btw and it made me chuckle.
 
@Dj_Jestar It doesn't work. I set mtime to +2 so I could test it earlier than a month and when it ran this evening, it didn't remove anything so I started fault finding.

Running it in the terminal without -delete, it shows the two folders that need removing but when I run it with -delete on the end, I get this.

Code:
find /home/pi/spiderback/spider -maxdepth 1 -type d -mtime +2 -delete
find: cannot delete ‘/home/pi/spiderback/spider/2023-02-08-21-30-02’: Directory not empty
find: cannot delete ‘/home/pi/spiderback/spider/2023-02-09-21-30-01’: Directory not empty

If I was using rm, I know I'd need -rf but what do I need to make delete delete the directory please?
 
@Dj_Jestar
That works, thanks.

With hindsight, it's not quite what I asked for though. I did say that I want to keep the most recent thirty directories, not ones that are specifically over thirty days old. If I were to run ten backups in a day, I still only want the most recent thirty backup directories to remain. So not delete by time but by number of directories.

Is that practical please?
 
List all directories (d) in date order, newest first (t), ensuring a new line per result (1). Starting from the 31st line (tail -n +31), delete recursively.
As you've given it, that lists them oldest first. If I substitute the d for an r though, it looks like it's working.

I think we're there, thanks.
 
Hmm, my directory filenames are made up of the date and time and they were definitely the other way round when I looked this morning after I forced a few backups. I've actually removed all of those now and only have one backup remaining so I'll wait a few days before double checking.
 
You may wish to confirm that, here's what it shows for me (adding -l and time-style to show dates):
Confirmed.

d definitely shows oldest first and r shows newest first.

Code:
$ ls -dd1 /home/pi/spiderback/spider/* | tail -n +1
/home/pi/spiderback/spider/2023-02-13-21-30-01
/home/pi/spiderback/spider/2023-02-14-21-30-02
/home/pi/spiderback/spider/2023-02-15-21-30-01
/home/pi/spiderback/spider/2023-02-16-21-30-01

$ ls -rd1 /home/pi/spiderback/spider/* | tail -n +1
/home/pi/spiderback/spider/2023-02-16-21-30-01
/home/pi/spiderback/spider/2023-02-15-21-30-01
/home/pi/spiderback/spider/2023-02-14-21-30-02
/home/pi/spiderback/spider/2023-02-13-21-30-01

This is now doing exactly what I wanted though, thank you :)
 
And just to confirm it works, this is the directory as it was yesterday after the backup and today after the backup. The oldest one has been removed so it's all good. Now I'll increase the number up to the required 31.

rkDbWJR.png
 
Back
Top Bottom