Now let's talk about another script to delete old files.
Now this is a definite script that everyone might have experienced or will be experiencing.
If you are becoming a system administrator or becoming a Linux or taking over a Linux job so you have
a system and as you know that and you build a system so many people log in so many program runs so many
Laux get created as a system ages or time it creates a lot of files and a lot of files are created.
There is no mechanism inside unless you enable a couple of other services to delete all the files.
So what happens if you don't delete those files.
Well it will fill up your disk space.
And once the disk space is full your system will become unresponsive.
So it is very important for all the system engineers or administrators to actually keep an eye on all
the old files and that are no longer in use.
So in this lesson where are you going to do is it going to create a script from scratch that will delete
all the files for a specified number of days or years or which ever that is being specified in the script.
And what are you going to do.
Actually first we're going to create files with all the time step because in my system my system is
fairly new and most likely the system you are working on is also new.
So maybe you don't have all the files you would have to create files with the whole the time stamps
so you could try out our new script then we're going to find and delete those files that are older than
90 days.
Why pick 90 days it doesn't really matter you can pick hundred days 80 days whichever days that you
like but in a Jones standard is that most the time people want to get rid of all the files all within
three months and then begin going to find another way or another script which will find and rename old
files.
So if instead of deleting it what you could do is you could find all the files that's older than 90
days and instead of deleting it how can we just rename it and rename it like Dot old or dot archive
or whatever that you want and rename.
So you need a script for that too.
So these are these are the type of scripts that you work on in this lesson.
I will go straight into my Linux machine.
OK.
I have it open to my precession.
I am logged in as myself and I am in home.
I have all p s directory Pierres directory is something that I previously created stands for professional
scripts any way you can pick any directory.
As I mentioned earlier.
So now we are in this directory and when you do ls minus l tr you'll notice that I do not have any file
older than August 27 which is not 3 months old.
So for me to test my script on all the files I would have to create some files that will fit all the
time stamps.
Well can you do it.
Yes you could do it.
You could do it with a touch command and the format for it that is touch with minus D option and you
specify the time.
Let's say those day is day.
Comma first march.
2018.
12.
30 am and create a file call let's call it a.
Ok so now the file is created.
That's verified if it is.
If it is created all the time stamp.
Yes you see I did LS mine l tr and this option LS myself.
It will give me all my files in the time order so that the most recent one is created at the bottom
and the oldest one is at the top.
So if you see the top one is this one is created on March 1st so March 1st is a lot older.
I created that.
So now I wanted to create just like that.
I wanted to create other files as well so create b create C. create d create for files of this wave
and I run the script it will find all these commands.
All these files and execute it.
Now let's create our script to delete it.
So first thing is to find it.
First of all every time you need to create a script it's better that you run that command so it will
work in the script.
So do find the path to where you finding it so I'm going to find it in this path which is right here
so find this path
with time modify time plus 90 and then bonce it finds them.
Then what do you want.
Do you want to execute a command.
And I just want to see the listing or if it's all the LS minus L off what returns and what is returned
is specified with curly braces.
Open curly braces close space backslash and colon.
This is just a standard way to find files.
It's not something fancy that is only written script is just a simple command when you search online
you will find exactly commands.
So once I run this command you will see fine all the files that are created three months ago.
And you see it found four files right.
Perfect.
So now I will create a script that will do exactly the same thing and I don't have to actually run it
manually.
I'll just cry and tap that job and it will run on a
schedule basis.
So if I do VI and I will put delete old files this is the name of my script I will hit enter.
And you know here what you need to do is bin bash and Autor Emraan outsole date 2 8 8 20 20
description very important this script will delete all either then or delete files older than 90 days.
Perfect modified We need to worry about it all really this time because you're creating first time.
OK perfect.
Now this is the description and the shelf is defined.
When I right click.
I copied that same exact file earlier and now just save it.
Give escapable permission to my script which is delete all files.
Let's check the file there.
Now when I run it delete all the file and you'll see it as giving me all the files.
Now I want to delete them.
So to delete them you rely upon the script again and instead of execute LS You just do or delete those
files that are returned.
So now when I run it or files and it gave me my profit back it means it ran.
So when you do this myself here you'll see all those flaws that I had earlier.
A B C D E.
All of them are gone.
Right.
So that's how you create a script that will delete or open.
What if you just want to rename it.
Just try the same script and this time I'm just going to say move delete or pause to rename all files.
So now it has renamed I'm going to change it to rename old files and this time what I'm going to do
is I want once that those files are found I want to move whatever is returned to
return name dot old.
So this is going to fine and move whatever you find to the name of the find that dot old extension you
copy and paste.
Now first before I run it I want to create those files that are created earlier so I'm going to hit
up arrow key because up arrow keys remembers all of the commands that I ran previously.
OK.
So this is the one d see
B and A.
That's where a fight.
Let's do this myself.
TR and you'll see all the Pozzo there.
Now when I run this updated script it should update the name of these files from D to D.
All the old beat out old an 8 has to rename all files and it ran let's verify it.
You see a b c d d old.
So this is about the script.
Again it's the everyday script that you're going to be using it.
It's a real life example script you need to know that you need to know how to delete all players or
rename it or whatever that you're trying to do with the files.
It will do it for you.
So try it out yourself.
I am going to include the instructions that you just followed in the lectures text and the same lesson.
You'll find it.
You could copy paste or you could start from scratch.