Advanced Shell Scripting Tutorial | Delete More Than X Days Directories | Tech ArkIT

Advanced Shell Scripting Tutorial | Delete More Than X Days Directories | Tech ArkIT

for more updates click on subscribe and
click on bell button for latest notifications hi friends welcome to Tech Arkit youtube channel my name is Ravi in this video session we are going to
see that little bit of the scripting so I have a scenario there Tech Arkit -High Quality Tech Videos like share
comment and subscribe I’m just going to give you the scenario
on how you are going to write this script so the scenario is every day from
Monday to Friday one directory will be created in the sense the directory name
will be created using this year month and the date under /fullbackup/dailybackup and year-month and date and it will be moved back to its parent
directory every night every night in the sence every midnight to /fullbackup/archive however Saturday and Sunday sorry Saturday Sunday and Monday
directories will move to slash backup slash archive path every Monday evening
in the sense Saturday Sunday and Monday the directories will be there in slash
full backup slash daily backup until Monday evening so Monday evening
saturdays folder and sundays folder and mondays folders will move back to the
archive path on monday evening okay so the pre names will be created
the example is over here you can see this this is the example this is how the
directives name will be created fine I would like to delete the directories
older than two days from slash full backup slash anti path how to do it is
the question you can use any script or any method to you can use to delete
those directories holder then one of them two days from slash full full
backup / r-ky path so the problem statement is that I found whenever I use
this actually the find command will do the job for us like find the path and
way that type is their face and the modified time
that directory is more than two days then remove us the print all these
directory names using the print zero and pass it to the remove command RM command
over here and delete them okay you can do this way but what is the problem
statement over here it is that whenever this command does not work as expected
due to directly modified date for Saturday Sunday and Monday our Monday
State example Monday date is 28 and modified date for these three folders
examples as these three folders when this dealer is whenever its moves to the
archive the modified date of this dark place is 28 then whenever you use this
find command you cannot able to find example say that
modified time is more than two days so if you see these three directories so
this three-dollar pieces that modified time is 28 so you cannot delete if I run
on 29th still I cannot able to delete these three folders so when I am going
to do it if I run on 31st of the December then I can able to delete these
three folders so I don’t want to do that always I want to keep only the two
directories on slash full backup slash archive so that is the question so here
I have found two methods where I can do this
scripting example say that in to simulate this question what I’m going to
do is that I am just going to create this but example this does not work of
course but just for showing this prep how it works so I am just going to
create this directories slash full backup slash daily vitamins just going
to create it over here under these directories
and I’m just going to create this directory is whatever you would like to
so these are the example lab please you can see this these are they
Larry’s has been preheated over here so this is the narrow place called 24 26 27
28 okay and maybe I can create few more like 29 30 31st maybe fast over here okay so this is the folder structure
over here now I want to date the directories based on its modified pack
right so but now modified time would be like not the straight forward or here so
I have to create one modality or here slash full backup and archive Ida move
this varies from full backup daily full backup to archive okay I’m just moving
this to slash full backup archive and go back to the full slash full backup and
archive and see this the directories are out here now I would like to do this
deleting this that please and how we can do that so maybe the find command may
not work because the M time of that modified time of card directories
example if I go to archive and start the directory stat and see this er 2018 1224
so the modified time of this directory is not something different right so this
is 31st something like that okay this does not work for us because if I based
on this time if I would like to see this example say that the 25th folder okay
this is modified date is latest one so hard
I cannot delete this one right so now I wanted to delete that folders based on
using this script so here I have written the one script so where it can validate
the directory date kind of convert that date into the epoch time then – this
starting date and ending date and validate that time to the more than two
days then delete that folder so how does it work so let’s go ahead and create the
script so example say that I am just going to create a script therapy or here
and go to slash crate and create the script name more than example X date dot
Sh so here I am just giving the bash script and say description is that
delete the directories folder than two days based on directory name validation
ok I’m just taking this Derby names and validating them and deleting them ok so
what I am doing is that fast LS – LT R and I am just doing this directory is
listing out so the like free listing out over here is not full backup slash
archive and Here I am just cutting data
directory names using this a wk print statement okay
print dollar nine so it is in the ninth position then I am crapping that grape
okay because there is some other database in that actually but in this
case it’s not there okay maybe you can eliminate this one / script / this that
output I am just capturing over there so what does this command do actually so if
you can go ahead and type into the terminal okay
and you can see this these are the directory names I am dripping from this
folder okay now what I’m doing is that I’m
using the Farlow for I in I’m just going to cat this file this file finder do so
what I’m going to do is that I’m going to calculate this directories date time
stamp Cal it as a start time equal to so I’m just using this date command over
here to convert that directory date into the epoch time – D I say
dollar I find zero zero zero zero zero what is this this is hours minutes and
seconds so using this date and hours and minutes on time using this date
percentage is and I Fundy will convert that actual time into the hip hop time
now I am just grabbing this end time so where is the end time is coming from end
of time is that our system fine so in no time
place percentage is okay and I am just doing this command calculation the time
calculation over here so using this double process and say that in the time
– start time okay so if this start time is in – time – in the same current –
time and the start time in the sense before time so the directories name
based on the name I’m just calculating that ok if the start time and in my end
of time – start time if it is more than two days okay I am just converting
converting the time into the we’re into the hours of the minute for the print
and do the entities in the sensor its calculate the value by 60 the value can
be converted into the 60 then I’m storing this value into the this scripts
directory and the name is well maybe a number I can do anything and I say that
I am just counting that value so assigning that value to the count say
that a cat and scripts value so what is the value the value will be stored into
the count variable now if so I am writing the if condition or
here and say if the count of the value is more than two eight eight zero
minutes which means that more than two days then examples either an gesture
printing some statement over here directories are older than two days
dollar I so I just would like to print that directories to output file called
example say that job log something like that now I run out of my phone RF and
delete from full backup slash archive the dollar I and close this if condition
and closely follow this is how the script is rated and you can see this
colorful script so what I am doing here it is that I am just gripping that
directories name using this LS – ltr’ and
printing this ninth column from that output then after that I am storing that
output into the one of the file called a priest and I am carrying that using the
for loop and say for loop so one by one one by three we wonder if I am just
converting the directory using the directory name and adding thumbs
timestamp to eat and it converts the directories so what this exactly this
will do here this command will go over here Utica sense and I will show you or
example over here example is if I do 2099 from 0 1 1 0 1 it will convert this
into the time so and now I am just getting this current epoch time using
the percentage s so this is the current epoch time the one the one is the older
epoch time okay this one and I am doing this this minus this minus this so that
I will get some values or there the value I am converting into the minute
and the minutes I am calculating over here so example say that so here so what
I am doing this is a start date and it and n to date – started I’m get some
value over here and the value will be converted divided by 60 you know since
hours the minutes of the minutes will be calculated in the sense the values will
be calculated into this mean this many minutes and the value will be stored
over here and if the count of that I am assigning the value of to the count and
the if the count is more than this many minutes in the sense two days then it
should print that they’re pleased to this file and delete them so how this
script is going to work or here let’s example say that X and we have to give
the permission before that and giving the execute permissions and
I’m going to execute this so before this I’m just going to show you over here is
that LSI Ellis I fun just address and you can see this
these many directories are there within this archive therapy now what I’m going
to do is that I’m just going to run this script and see now you can see go ahead
and see this how many directories are they’re all
here there are only three directories over here which are matching more than
today’s time because today is 31st as for the date today is as per the system
date today’s 31st so whenever I run again this command over here which can
delete this folder date times time data ok now I can just go ahead and see this
job log ok what it says so job log directory says older then ok so I did
not added the double quotation over there that’s why it’s printed love the
last battery path over here ok like this you can see this maybe can go higher in
the seat of value what is the value over here the value is this one maybe you can
see this one of this their fries I fetched from there so these are the
dirty names I fetch from there so this is the script how it works it fetches
these directories value but every name in the date and it converts that date
into the epoch time and – the epoch time value eye on the calendar is the value
it to be two eight eight zero which is more than today’s then deduce this
directories so this is how I solved and there is an another method called you
can also use LS I have an LTR output and based on this modified like based on
this on code you can delete the upper batteries which using the account
of these lines the diesel Tron in the solution you can do that okay if you
have any other script which can solve this problem or if you have thinking of
maybe a unique way to do this you can do and post in the comment thanks for
watching stay tuned please subscribe to the channel for more upcoming videos
find the causes

5 thoughts on “Advanced Shell Scripting Tutorial | Delete More Than X Days Directories | Tech ArkIT

  1. Advanced Shell Scripting Scenario Based solution. Comment your answer over here.
    Shell Scripting Playlist:

  2. and my another question is what is hip-hop time?i cannot understand the exact word and meaning can u please tell me

Leave a Reply

Your email address will not be published. Required fields are marked *