Hello the beautiful people.
And welcome to this video where we're going to be kickstarting 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 HM command to delete files, and you're also going
to learn how to use the r m command along with wild cards to make the r m command even more powerful.
By the end of this video, you'll know how to delete files using the command line, and in the next
video we'll move our discussion onto directories.
So let's go ahead and get right into it.
All right.
So let's kick off by creating a file called Delete Me in our home directory so we can see that we're
already in our home directory.
By looking at our shell prompt, we see the tilde, the squiggle up there, which means we're in the
shell prompt.
But to confirm we could just type the command print working directory and see that we are indeed in
our home folder.
So let's create a file called Delete Me.
We're going to use the touch command and type delete me and press enter.
And now if we use the LS command, we'll see that we do indeed have a file called Delete Me been created.
We can confirm this of course by opening up our graphical file browser and we'll see a file called Delete
Me.
Okay, so so far so good clearing the screen.
Now that we've created Delete Me, how can we actually delete it?
Okay.
Well, the way to actually delete or remove that file is using something called the r m command.
Now the r m command is r m stands for remove and all you've got to do is give the m command a set of
paths to files that you actually want to delete.
And these could be absolute paths or relative paths.
So if we open up the main page for the R m command, it says that you can give it more than one file.
You see, you can give it a file and then it's got three dots after it.
And we know from our discussion of command line syntax or command line layout that these three dots
mean you can have more than one.
And if you need a refresher on that, check the cheat sheet in the previous section.
It's got all that information in there for you.
So let's go ahead and remove delete me so I could because I'm already in the same folder.
I could just use the relative path like that or delete me.
And when I press enter we'll see that delete me has indeed been deleted.
I could if I create delete me if I create, delete me in our documents folder this time.
Okay.
So if we're looking at our documents folder, we see delete me has been created at the minute in the
command line.
I'm just in the home folder so if I try to do RMM delete me, that won't work because there is no such
thing as delete me in this in this directory.
So delete it.
I have to do our m documents slash delete me.
So I have to give it the path relative to where we are now, which is the home folder.
So here I am in the home folder to get to delete me I have to do documents then delete me.
Okay.
So document slash, delete me and I press enter and then it gets deleted and you can have more than
one.
So if I go ahead and create a file called let's say file one dot txt that's in our home directory,
I create a file called file two in our documents.
So a file two point text in our documents and I create a file called file three dot txt in our downloads.
So I've got one in the home folder, one in the documents and one in downloads.
So delete all those.
I can just enter them one after another.
So I do m file one txt.
I don't have to do anything because that will do because I'm already in the home directory.
The same folder as the document slash file two and downloads slash file three.
And if I press enter, you see the file one disappeared.
File two has disappeared from documents and file three has disappeared from downloads.
So the R command is a very simple command to use for files.
Just think of it as the opposite of the touch command.
So if you want to create a file, you do touch file one.
If you want to delete it, our M to our M file one and it'll work just like that.
So now that we've seen how to use the r m command in a basic way, let's take it up a notch and use
it along with wild cards.
Now, wild cards can really level up the power that your commands have.
So let's just see.
See how we do this using the r m command.
Let's make a few new files.
Let's make a file one txt let's make file two dot pmg and file three jpg.
So when I press enter, you see that all three of those have been created in our home folder.
Okay, now how would we delete every single one of them that ended in dot txt?
So that's actually just for now.
Let's make another one file for txt.
So we've got to the end in txt and we've got one that ends in PNG and one that ends in jpg.
How would we delete them?
They're the ones that end in txt.
Well, first of all, we could do it manually.
We could say ah rm file one txt and file for txt.
That would work.
But I want a bit more of a clever solution than that.
I want to say delete every file that ends in dot txt.
Now the way we do that is by giving the command a pattern and anything that matches this pattern will
be added as a command line argument to it.
So if I the pattern that I'm going to use is this star dot txt.
Let us break this down.
So the star means anything.
Anything could go there.
So this whole thing is our pattern.
The star means anything.
And then dot txt.
So what this means is for anything to match that pattern.
All it has to do is end in dot txt.
So basically what this is saying is remove everything that ends in dot txt and we press enter on that.
You see that file one and file four have been deleted because they ended in dot text.
So let's let's try and see if we could make some other files.
Let's make, let's make file one dot txt again and file for dot dot txt.
Let's make those back and then then let's let's create some of the files.
Okay, let's create some called qibla dot txt and sublime one and then qibla two dot png and qibla three
dot jpg.
Okay.
So we've got some that start with qibla and some that start with the word file.
Okay.
How would we delete the ones that just start with the word file?
Well, the way that we do this is we would use the ram command again.
Of course, because we need to delete.
Then we need to give it another pattern.
So let's think how how would we create a pattern that would match anything as long as it started with
the word file?
Well, the way we could do that is we could type the word file and then put a star, because this means
as long as it starts with file and then has anything after it, it matches.
So all it has to do is start with the word file.
And if I press enter, we see that file one, two, three and four.
Doesn't matter about the file extension or anything.
As long as they start with file, they've been deleted.
But all of the problems have been kept there.
So let's let's create file one, two, three and four again.
So we're going to create touch file one txt file two dot png, file three dot jpg and file for txt will
make them back again.
Okay.
Now we want to delete all of the files that have the number two in them.
Okay.
So how would we do that?
Well, what we might do is we might say, okay, let's remove it.
Doesn't matter what it starts with, but it's got to have the two in there and it doesn't matter what
it ends in.
So here we are.
We're saying it could have anything, then the number two and then anything afterwards.
So if we do that, we see that file two has been deleted and problem two has been deleted, but all
the others are okay.
What about getting rid of the ones that end in jpg?
Well, to do that you just do our m ends in jpg.
I press enter file three dot jpg and three have been deleted, but all the others that end in txt are
still there.
What about deleting those?
Ah m everything that ends in dot txt press enter and they've all been deleted as well.
So you can see here how wildcards can really make the your commands a whole lot more powerful and a
lot more specific.
There's one more that I want to show you.
Let's just go up here.
We've made back our files and let's make back ash blam ash files sublime two dot png, i think.
Yep.
And problem three dot jpg.
Now what if we wanted to get rid of the ones that had two or three in them?
Well, to do that, remember how we got rid of the one that had two in it?
We said it could start with anything, then have a two, then ending anything.
Well, now we want to give it some options.
And this is where the square brackets come in.
So if I said, okay, something like that, that's saying it could start with anything, then have either
a two or three and then ending anything.
So this should get rid of file two, file three and Sjoblom two and Sjoblom three.
So if I press enter, there are file two and file three and problem two and should blam three have been
deleted?
So wildcards are incredible tools that really allow you to control what's going on when you use your
commands, and anything that matches this pattern will be passed as a command line argument to the command
that you use the wildcards for.
So that's a bit of wildcards in action as well.
So there you have it.
In this video you learnt about the Rim Command.
Now the Rim Command is the command that's used to remove stuff from your file system.
And in this video we focus specifically on deleting files.
You also built upon what you learned in the last video about wild cards and saw wild cards being used
in action to delete files.
Now, what you saw about wild cards in this video, it doesn't only apply to the M command.
What you learned about wild cards and how you might do specific tasks works for any command you build
patterns called regular expressions with wild cards and whatever matches those patterns is passed as
a command line argument to the command that you're using.
So there you are.
You've seen some wild cards in practice as well.
Up next, what we're going to be doing is we're going to be developing this discussion a bit more and
we're going to move on to looking at how you might delete directories, how can you delete folders?
So that's a that's a completely next level.
It's going to be very exciting stuff.
So I'll see you in the next video.