[solved] Bash to remove directories?

Commissario
Joined
16 Oct 2002
Posts
2,754
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.
 
Caporegime
Joined
18 Oct 2002
Posts
29,491
Location
Back in East London
Code:
find /your/path -maxdepth 1 -type d -mtime +31 -delete

A condescending summary of the options:
  • `-maxdepth 1` filters to nodes at the 1st level only
  • `-type d` filters those nodes to directories only.
  • `-mtime +31` filters thos directories to those with a last modified time of 31 days or more.
  • `-delete` deletes 'em.
It's important to note that "find" applies its options in an order of precedence/aggregate! e.g.
Code:
find ./ -delete -type d -mtime +31 -maxdepth 1
will delete everything from the current directory!
 
Last edited:
Commissario
OP
Joined
16 Oct 2002
Posts
2,754
Location
In the radio shack
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.
 
Commissario
OP
Joined
16 Oct 2002
Posts
2,754
Location
In the radio shack
@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?
 
Caporegime
Joined
18 Oct 2002
Posts
29,491
Location
Back in East London
Yeah that's a pain. Just noticed in the man page that despite -delete implying -depth, it will not delete non-empty dirs. I guess when combined with -type d it honours the same rules as rmdir.

Go for this one which will limit the search to first level only, but recursively delete the matches:

Code:
find ./ -maxdepth 1 -type d -mtime +31 -exec rm -rf {} \;

Test it by running this first (with whichever number you wish for the mtime :)):

Code:
find ./ -maxdepth 1 -type d -mtime +31 -ls
 
Last edited:
Commissario
OP
Joined
16 Oct 2002
Posts
2,754
Location
In the radio shack
@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?
 
Caporegime
Joined
18 Oct 2002
Posts
29,491
Location
Back in East London
Doh! I just focussed on the code sample(s) :)

Code:
ls -dt1 ./* | tail -n +31 | xargs rm -rf

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.
 
Last edited:
Commissario
OP
Joined
16 Oct 2002
Posts
2,754
Location
In the radio shack
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.
 
Commissario
OP
Joined
16 Oct 2002
Posts
2,754
Location
In the radio shack
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.
 
Commissario
OP
Joined
16 Oct 2002
Posts
2,754
Location
In the radio shack
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 :)
 
Commissario
OP
Joined
16 Oct 2002
Posts
2,754
Location
In the radio shack
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