Hello the beautiful people.
And welcome to this video where we're going to be finishing off our discussion of how to delete files
and directories using the command line.
So in this video you're going to learn how to use the R, m and RMD commands to delete folders.
And by the end of this video you'll know pretty much everything you need to know about deleting files
and folders using the command line.
So let's go ahead and get right into it.
All right, so let's start off by creating a folder in our home directory called Dell Folder.
So if I use the mkdir command and type del folder, we'll see that we created a new folder inside our
home directory called del folder.
So let's remove it.
Okay.
Now all we have to do actually to remove it is use the r m command again.
So if I use r m and use del folder and press enter.
We actually get an error.
Now, this is because yes, we will use the RGM command, but in order for the command to remove folders,
we need to give the r m command the r option.
Now the R option stands for recursive or recursively.
Now, if you delete something recursively, that means that your delete that thing and everything else
inside of it.
So if we try that now our M slash R and then del folder and press enter, we'll see.
Now that del folder has indeed been deleted.
So let's make it again.
Make it again.
Let's make Del folder and let's make some more folders inside of it.
So we're going to use brace expansion for this.
Now we can do make make directory del folder and inside there we're going to make three folders called
delete me one, two and three.
Okay.
So if I press enter and take a look at Del folder, we can see we've got delete me one, delete me two
and delete me three.
So that's how this work we made first it made delete me one inside of Del folder, then it may delete
me too.
Then it made delete me three.
So that's brace expansion again.
So let's make some files inside each of those.
So if we touch del folder, delete me, delete me, one, two and three and then inside of there, make
file one, two and three.
And now if we take a look we can see and delete me one.
We've got far one, two and three in delete me two file one, two and three and then delete me three.
We've got file one, two and three.
So now if we, if we go ahead and if we just look, we've got Del folder here, we've got Del folder
in our home directory.
Now if we use our M with the R option to delete that del folder, remember to delete something recursively
means to delete it and everything else inside of it.
So if I use our M with the R option and give it del folder and press enter, we'll see.
That del folder has indeed been deleted and so is everything else inside of it.
Now one thing you might notice is that the r m command, when it did that, it actually didn't give
us any warning.
It just went ahead and deleted everything inside the del folder folder.
Okay.
Now because of this, our m dash r m with the r option is a very powerful command that should be dealt
with very carefully, especially when you start using the star wild card as well.
All things can go wrong and you can find yourself just deleting everything and not being able to get
your files back, which is sad, sad times.
So for example, if I do go ahead and do that now, if I just delete that on everything on our system
right now, I'm not going to do it.
I'm joking.
So one way to make the r m command safer is actually to give it the AI option.
So the AI option stands for interactive and this will make the r m command ask you interactively whether
to delete each file or not.
Now, at the moment we don't have we don't have the del folder anymore.
So to make del folder and delete me one, two and three all at once, I'm going to use the mkdir command.
Give it the p option which allows you to create an entire path and do delta and tell it.
Create delete me one, two and three.
So if I do that actually not del two sorry del folder then if I do that I get del folder inside there
delete me one, delete me two and delete me three.
Kind of call right now let's create the files inside there.
So del folder forward slash delete me one, two and three and we're going to make file one, two and
three.
And so each of those and inside each of those, we've got our files back.
So there we are.
Didn't take too long at all.
We've got our files back.
By the way, can you see just how much quicker this is than pointing and clicking?
We created all of those files without ever even needing to enter the folders.
We just created them right from our home directory.
So, so cool.
So now if we try to delete everything again, try and delete del folder by giving VRM Command the R
and I options.
It doesn't matter which order you do it.
So we give it del folder.
We'll see that now it's asking permission for us to actually delete things.
So it's saying Can I go into Del folder?
Let's tell it.
Yes, it will now say can I go into delete me one.
Let's say yes and can I delete a file too.
Yes, you can kind of delete file three.
Yes, you can.
Can I delete file one inside.
Delete me one.
Yes, you can.
Can I remove the directory.
Delete me one.
Yes you can.
Can I go into delete me three.
You see it's not particularly doing it in any order, but it's asking you before it does it.
Okay, so let's say yes, you can delete that.
You can delete that, you can delete that.
Now let's leave.
Let's leave file two inside delete me two.
So it's going to leave.
Delete me three.
Going to delete me two.
No, don't delete file.
Two.
Yes.
Delete file three.
Yes.
Delete file one.
No, don't delete the entire folder.
No, don't delete Dell folder and that's it.
So if we look inside Dell folder, which is C, delete me two and file two still left over.
Okay.
So that's quite cool.
Right.
You can control what the r command deletes, but this is kind of tedious, right?
Saying yes and no every time it's if you had a lot of files that would that would really be a massive
hassle.
So fortunately, there's another command that's specialized for only removing directories that are empty
now because this command only removes empty directories.
This means that you can tell it to delete everything.
It will only delete the folders that are actually empty, which means you'll never delete a file accidentally.
And that command is called the MDR, the remove directory command.
So to see the Remove Directory command in action first let's go ahead and delete everything inside Dell
folder.
So remove recursively Dell folder and there we are.
Dell folder is been deleted.
Now let's go ahead and create some folders.
So we're going to use mkdir with the P option to create a let's call it Dell folder and then folder
1 to 3.
That's another way of doing it.
By the way, you don't have to type one, comma two, comma three, you can just do 1.3.
And if we look in Dell folder, we've got folder one, folder two and folder three.
Okay, and let's put ten folders inside folders one and two, but not in folder one, not in folder
three.
So that's two touch dell folder folder one, two and then put inside there file 1 to 10 txt.
So if we look inside folder one, we've now got ten text files.
We'll look inside folder two, we've got ten text files, but in folder three we don't have any.
So now that we've got three folders inside Dell folder folders, want a folder to have ten files in
them, but folder three is completely empty.
Now if I try to do remove directory and I say remove directory dell folder slash everything.
Okay, what we're saying here is delete everything inside dell folder, delete the empty folders inside
Dell folder basically.
So if I do that, we'll see.
It failed to remove folder one because the directory is not empty.
It failed to remove folder two because the directory is not empty, but folder three has indeed been
deleted.
So just so you know, this wouldn't have worked if we just done remove directory of Dell folder without
putting the star.
So you see we've got the star asterisk wildcard here.
If we just had removed Dell folder, the shell will say but Dell folder is not empty.
How can I just delete it?
But what we did is we said remove everything that is inside Dell folder that is empty.
So we had to have the star there to make that work.
So that's just something to bear in mind.
Okay.
So as one final challenge, in the last video, we created a project folder that had a whole bunch of
other folders inside with like a bajillion files.
Okay, you must remember this from, like, the last few videos.
Now, what I want you to do is I want you to try and delete all of those folders and directories and
files and stuff in one command.
So that's your challenge?
You that's your challenge.
You've got to try and delete that entire project folder with everything inside of it.
All the thousands of files in one command.
So pause the video now and see if you can figure it out.
Figure out how to do it.
Then come back and see how you did.
Did you manage it?
If so, well done.
You learn super, super quick.
So here's how I do it.
Now, the folder was called Project.
It was on the desktop.
So let's shift there.
It's it's called Project.
So we want to delete everything.
So we don't want to use the RMD command because that only deletes empty directories.
We want to use the r m command instead because we want to delete directories that have stuff in them.
Now we want to delete the project folder and everything inside it.
So we need to delete recursively.
So to do that we're going to use the R option and putting that all together basically means we'll just
do remove recursively the project folder.
Or if I was in my home directory like this, I would do remove recursively desktop slash project just
giving the path to that folder.
And when I press enter as if by magic, however many folders there were, the thousands of files have
been deleted.
How absolutely epic is that?
So that'll just about wrap up our discussion of deleting files and folders using the command line.
So in this video you learn that the r m command needs the R option in order to delete folders, and
the R option stands for recursive deleting.
So to delete something recursively means to delete that thing and everything else inside it.
And this is why you need to be especially careful when using the R option because you can end up deleting
stuff that you didn't want to.
Now, one way to make that a bit safer is to use the AI option for the R command, which will allow
the r m command to be interactive when it does its deleting.
So this is where you can say yes or no to each file or folder being deleted.
But the best thing is there's not really much you can rely on.
Just be careful when doing your deleting.
Okay.
But the safest of all options is to use the RMD command which will only delete folders that are empty.
So if you just want to do a bit of tidying up and delete folders that don't have anything in them,
you can use the RMD command and rest assured that you won't delete any data.
So coming up next, we're going to discuss how you might copy and paste files and directories using
the command line.
So instead of cutting our file tree, we're going to make it grow.
So for all that goodness, I'll see you in the next video.