Command to delete oldest directory

OptimalDuck

Dabbler
Joined
Jan 14, 2022
Messages
15
Hi,

Lightroom creates a backup of my catalog every time I close the program. The problem with this is it doesn't automatically delete any of them. I only need to keep the most recent ones so I'm looking for a command that will look at the directory these backups are in, skip the 8 most recent directories inside of it and then delete the rest.

I've been working on this and have so far come up with the following, but it doesn't work.

Code:
find "/mnt/Photos/Lightroom Backup" -type f | sort -r | tail -n +8 | rm


I've also tried the following, and I think it is closer to working, but since my lightroom files have a ' in them, I get an unterminated quote error.

Code:
find "/mnt/Photos/Lightroom Backup" -type f | sort -r tail -n +8 | xargs rm


I'm pretty new to all of this, so I'm no doubt going at this wrong, but I'm feeling pretty stumped so I'm hoping someone can help point me in the direction.
 
Last edited:
Top