[SOLVED] Deleting oldest named folder in a partition

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
bigal
Level 5
Level 5
Posts: 512
Joined: Mon Aug 03, 2009 3:26 am
Location: Pembrokeshire, South West Wales

[SOLVED] Deleting oldest named folder in a partition

Post by bigal »

64 bit Linux Mint 18.3 Cinnamon

I have a partition in which I store 7 daily backup files . Each file is named using the date & time of creation. For example a file name of 2018-12-22_18:02 ("%Y-%m-%d_%H-%M). Obviously there is a finite amount of room in the partition so each day I delete the oldest, create a new one then copy and paste the files I want backed up into the newly created folder. Not so difficult but unnecessary work so I thought it is about time I automated it. My 2019 learning curve for scripting and for cron jobs.

I have no previous knowledge of scripting but have worked out and written a script to create the new folder with the correctly formatted date/time name and to recursively copy and paste the required data into the new folder. In due course I will set up a cron job to run this script each day. So far so good but I cannot see how to completely delete the oldest folder in my backup partition. All my searching results in code to delete folders and files based on the file creation or amended time in the headers rather than the file name. Not necessarily accurate, or indeed, what I am attempting to achieve.

It takes about 30 minutes to delete the oldest folder so I am envisioning two separate cron jobs to complete the required task. The first to completely and recursively remove the oldest folder and all it contents; delete rather than move to the rubbish bin; and the second to run a suitable time after the first to run the script that creates the new folder, selects and copies the required data then pastes it into the newly created backup folder. Can anyone offer any suggestion please?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Lots of Minty machines but there again I like lamb and I do live in Wales!
bigal
Level 5
Level 5
Posts: 512
Joined: Mon Aug 03, 2009 3:26 am
Location: Pembrokeshire, South West Wales

Re: Deleting oldest named folder in a partition

Post by bigal »

If it of any help, here is my test script for inserting a new folder and copying data into it.

Code: Select all

#!/bin/bash
now=$(date +"%Y-%m-%d_%H-%M")
mkdir --m 755 /home/username/Desktop/"$now"
cp -r "/home/username/trial" /home/username/Desktop/"$now"
Lots of Minty machines but there again I like lamb and I do live in Wales!
gm10

Re: Deleting oldest named folder in a partition

Post by gm10 »

As long as you stick with that date format and have no other folders in that location, it's as simple as sorting by filename and removing the first one:

Code: Select all

rm -rf "$(find /home/username/Desktop/ -mindepth 1 -maxdepth 1 -type d|sort|head -n1)"
Note that if this is for backup purposes then there are more efficient solutions than a cp -r (should be at least cp -ra IMHO). Existing backup programs are readily available and also solve your timing issues.
bigal
Level 5
Level 5
Posts: 512
Joined: Mon Aug 03, 2009 3:26 am
Location: Pembrokeshire, South West Wales

Re: Deleting oldest named folder in a partition

Post by bigal »

Hi gm10,

I have set up a spare partition and copied folders and files over so I can run a trial without actually endangering my data. In the meantime I am trying to fully understand the line of code you have written. I will update you later on, possibly tomorrow and let you know how I am getting on.

Many years ago, in the middle days of XP, I used to use Acronis. I found it to be extremely flexible and very easy to use as you could put a blank HDD in a machine and copy over an entire system, partitions, o/s and data in an hour or so all with a couple of clicks from a bootable Acronis CD. On a daily level, it was just as easy to access a single system or data file, edit it or restore it, whichever you wanted. To me the sun shone out of its... I have been totally Linux based since the demise of XP and tried all sorts of backup programs. To my mind none compare, even slightly, to Acronis. I do run Timeshift on all my machines which gives the o/s security and coupling that with my current data backup system seems to work for me. Each to his own though.

Quite correctly, making backups it regularly recommended. Something I do not often see, however, is regularly crash testing you backups. I do, but I promise you it is VERY scary but it does give you peace of mind.

Bye the way, I had over looked the ‘r’ switch in copy. An excellent thought.

Thank you for the input. I will update this post later on.

Cheers
Lots of Minty machines but there again I like lamb and I do live in Wales!
gm10

Re: Deleting oldest named folder in a partition

Post by gm10 »

bigal wrote: Thu Jan 03, 2019 5:11 am I have set up a spare partition and copied folders and files over so I can run a trial without actually endangering my data. In the meantime I am trying to fully understand the line of code you have written. I will update you later on, possibly tomorrow and let you know how I am getting on.
Sure, let me know if you have any questions.
bigal wrote: Thu Jan 03, 2019 5:11 am I have been totally Linux based since the demise of XP and tried all sorts of backup programs. To my mind none compare, even slightly, to Acronis. I do run Timeshift on all my machines which gives the o/s security and coupling that with my current data backup system seems to work for me. Each to his own though.
To each their own indeed, but if you like Timeshift, there's backup programs that work based on the same principles for your data, e.g.
https://backintime.readthedocs.io/en/latest/
bigal
Level 5
Level 5
Posts: 512
Joined: Mon Aug 03, 2009 3:26 am
Location: Pembrokeshire, South West Wales

Re: Deleting oldest named folder in a partition

Post by bigal »

Well gm10, as you knew it would your line of script worked fine. Thank you. First I ran it on a test setup and just now on my live data and all was well – sigh of relief!

Looking at you line of code I had not heard of ‘mindepth’ or ‘maxdepth’ before and have read up on them so now understand how they work. The remainder of the line is a puzzle though. Firstly

Code: Select all

-type d
Can you elaborate please. From there can I assume that the results are piped to

Code: Select all

sort
which puts all folders in order then the sorted folder list is piped through to

Code: Select all

head -n1
I cannot seem to find out much about any of these commands. As I said at the outset, this is a leaning curve for me. This automating of my backing up is my first ever script.

Thank you for suggesting BackInTime. I have tried it in the past. On Mint 17.2, 18.1, and 18.2. All instances resulted in virtually continuous hard disk drive usage so I removed (purged) it off of each system.
Lots of Minty machines but there again I like lamb and I do live in Wales!
gm10

Re: Deleting oldest named folder in a partition

Post by gm10 »

bigal wrote: Fri Jan 04, 2019 1:42 pm I cannot seem to find out much about any of these commands. As I said at the outset, this is a leaning curve for me. This automating of my backing up is my first ever script.
It always starts with one script, soon you'll automate everything. At least I do. ;)

You can find information about nearly every command in its manual. The manual is accessed via the man command. So
man find tells you all you ever wanted to know about find, and more.
man sort, man head, etc.

But to quickly break it down for you this time, also the parts you didn't ask because I'm fairly sure you didn't catch them all if you're a newbie to the shell:

Code: Select all

rm -rf "$(find /home/username/Desktop/ -mindepth 1 -maxdepth 1 -type d|sort|head -n1)"
rm -rf - force remove recursively
$() - this is a command substitution, your shell interpreter will spawn a subshell to execute the contents of the bracket first and finally replace the $() with the output of the subshell. In our case the output will be a single path, determined by the following commands:
find - self-explanatory name. The -type d parameter means I only want to find file system objects of type directory, whereas the -mindepth 1 -maxdepth 1 parameters mean I only want to find them exactly one level below the base path /home/username/Desktop/. If it's not clear, run the find command separately and remove either one of the latter two parameters to see the difference it makes.
| - pipe, as you understood
sort - self-explanatory
head -n1 - from the top (hence head) of the input output only n lines, here a single one. the complementary command would be tail. ;)
bigal
Level 5
Level 5
Posts: 512
Joined: Mon Aug 03, 2009 3:26 am
Location: Pembrokeshire, South West Wales

Re: Deleting oldest named folder in a partition

Post by bigal »

Lol! :lol: :lol: The first thing I did was look at man type. No manual entry so I tried help but no entry there either. I went no further with man and turned to Mr G. Not much luck there either but, to be fair, I have had to do some other things as well so left off at that point.

Thank you again for your help, and now for the explanation as well. I will mark this post as solved and leave you in peace.

All the best.
Lots of Minty machines but there again I like lamb and I do live in Wales!
gm10

Re: Deleting oldest named folder in a partition

Post by gm10 »

bigal wrote: Fri Jan 04, 2019 5:02 pm Lol! :lol: :lol: The first thing I did was look at man type. No manual entry so I tried help but no entry there either. I went no further with man and turned to Mr G. Not much luck there either but, to be fair, I have had to do some other things as well so left off at that point.

Thank you again for your help, and now for the explanation as well. I will mark this post as solved and leave you in peace.
Happy to help.

And quickly re man: You can only look up commands, not parameters. The command is find, the parameters are explained in the man find output.
bigal
Level 5
Level 5
Posts: 512
Joined: Mon Aug 03, 2009 3:26 am
Location: Pembrokeshire, South West Wales

Re: [SOLVED] Deleting oldest named folder in a partition

Post by bigal »

I have just found this site.

https://www.shellscript.sh/

It seem that it is good a place as any to get started along a more structured path rather than the ad-hoc way I have been stumbling along.

Than you for the encouragement (and that explanation)
Lots of Minty machines but there again I like lamb and I do live in Wales!
Locked

Return to “Scripts & Bash”