[No audio]
Wildcards. What are wildcards? Just as the name
says they are wildcards, meaning if you play cards,
[No audio]
and you know in Poker, or Blackjack,
they do pick a card, like 10 or 9, whatever the
card and you could match it to any of the cards to
make your flash, trail, or whatever that you like.
So that is kind of a wildcard. So just like that,
in Linux, we use wildcards to get the output that
we are looking for. A wildcard is a character, now
this is a definition right from the Linux
dictionary, that can be used as a substitute from
any of a class of characters in a search. So there
are many wildcards, but the most basic ones I have
listed here, there are three. The first one is the
one I use most of the time is asterisk, which is
also referred to as a star. A star represents
zero or more characters, anything that matches
that star placeholder. We'll look into that in a
moment when we actually use the star wildcard in
our Linux machine. Then we have a question mark,
represents a single character. So if you have a
file name, text, and you are looking for any files
that has the word text, and you could use T E,
instead of x, you could just put question mark in
there, and then t, that means what ever falls that
had T E doesn't matter what's inside of the x and
the last letter is x, the t, that didn't work. The
third one we have brackets, which represents a
range of characters. So it's better we look at
done Linux machine to understand them more deeply.
I'm going to bring up my machine real quick.
Where'd it go? Right here. I'm logged in always as
myself. And I'm in my home directory, of course.
Now, right now, these are the files and
directories in exist in my home directory, I want
to create nine files that has a name, let's say
abcd, one, and xyz. And then same thing instead of
one, it just increments the number 23456. I could
do this touch abcd1-xyz, one
time, then hit up arrow key again, change one to
two, change two to three, and keep going. But don't
you think it's cumbersome and tedious? It is right.
So we use wildcard to perform that task because
you're going to create nine files. So it's just a
practice that we are doing for nine files. Trust
me, in corporate environment, you probably going
to have to create like 1000s of files, what you're
going to do, you're going to just hit up arrow key
and type it 1000 times. No, that's not
productive. So then what do you do? We use
wildcards. So first of all, I'm going to do is I'm
going to remove these files one by one. Again, I
can remove it wildcard, as well. But let's create
it first and I'll show you how I can remove it. So
I have two left. Now I could use a wildcard to
remove both in one shot, remove abc wildcard. So
what is telling it, remove everything that has a
file name that starts with abc and whatever that
comes afterwards, you don't care, just remove it.
I hit enter, ls -ltr, and you'll see all
those two files are gone. Now I'm going to create
nine files. And to create nine files, I'll use the
command touch. As you know the touch is a command
to use, and then there's a wildcard another
wildcard that's called curly braces. So I was
creating file abcd curly braces 1 dot dot 9,
because I'm going to create nine files. So this
curly braces 1 dot dot 9 starting to create nine
files that has the digit one through nine and dash
xyz. When I run this command, what do you think
that I will have listed in my home directory? I
will have nine files right here at the bottom, you
will see I have nine files. Awesome, right? This
is what you wanted to do.
[No audio]
It is much more faster to do in the wildcard as
opposed to typing one by one. So let's use
the asterisk, I just want to list every
file name that starts with abc. You do abc
asterisk, and you will have all the files that
starts with abc. I want to remove every file that
starts with abc. You could remove one by one, abcd1
and so on, and so forth, again, very
cumbersome, don't do it. You're a system
administrator, you are smart, folks, you don't
want to use the commands one by one, what do you
would use, you'll use a wildcard. So you will do
abcd, or ab, and do asterisk. The reason I
picked the four letters, because if I just pick a
and hit asterisk, it will also remove any other
files in my home directory that starts with a and
which I don't want it to do it. Right now, I don't
have any files. So let's say if I create a file
touch a. So now I have all the files and a. So I
just want to confirm I don't have a so I don't
delete any file accidentally. So yes, I don't have
any file that begins with a other than these. So
if I just do remove a asterisk, it will remove all
the files that starts with a. Let's check it.
[No audio]
See, all those files are gone. Isn't that cool?
Yes, it is. I love this asterisk because I use it
all the time. I'm going to recreate those files
again, by hitting up arrow key that will return my
last ran command. Just confirm they are there. Now
what if I want to remove everything that ends with
xyz, same way remove, but this time we'll put
asterisk in front of xyz, hit enter, boom, all
gone. Also, I'm going to recreate it just for the
exercise of this lesson. It created nine files.
Now we'll move on to the next wildcard which is
the question mark. The next question mark is
telling it ls -l ?bcd*.
Okay, what am I asking here, I'm asking is
give me all the files that has whatever the
character is the first, as long as the other
characters has bcd, and whatever comes
afterwards. These are the matching files because
whatever the first character is, whether it's a or
not, I don't care. I just want to make sure I will
get the listing of the files that has bcd in it
after the a. So that's how you use question mark.
And the last one we have brackets. Bracket is going
to tell me that give me anything that has c or d
letter in a file. It's just that simple. Either c
or the d, either f or this and that. So you put
all that stuff that you're looking for inside of
the bracket. And you will have all those files you
see here. And it went so fast. So I'm going to put
it in more output, I want to see one page at a
time. And you'll see it gave me and it has a, it
has b, every file that I'm getting as either has a
or b either has a or b. So that's how it works.
That's how wildcard works. Again, now I want to
get rid of all the files that has x and y in it.
Awesome, I have my wildcard I'm not going to go
and delete one by one. So this command is telling
the show that go ahead and please delete all the
files that has x and y in it. And x and y has to
be together of course. So if we do have these
matching files xy and it will go ahead and delete
it. And all of them are gone. So wildcard not
necessarily is used just with ls or remove. You
could use it with any of the commands we have. You
could use it with all the commands we have learned
so far. Try it. Try it with different commands. If
it doesn't work, that's okay. Go online, google it
and try wildcard, wildcard examples. Try them,
just get familiar with them. This will help you a
lot in your job, I promise you.
[No audio]