Instructor: Wildcards, what are wildcards?
Just as his name says they are wildcards,
meaning if you play cards.
And you know in poker or blackjack,
they do pick a card, like a 10 or nine, whatever the card,
and you could match it to any of the card
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 with 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 question mark, represents a single character.
So if you have a filename, text,
and you are looking for any files
that has the word text.
And you could use TE, instead of X,
you could just put question mark in there and then T.
That means whichever files that had TE,
doesn't matter what's inside of it,
the X and the last letter is T, then it'll work.
The the third one we have brackets,
which represents a range of characters.
So it's better we look at it in the Linux machine
to understand them more deeply.
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 exist in my Home Directory.
I want to create nine files that has a name,
let's say, ABCD1 and XYZ.
And then same thing instead of one,
it just increments the number 2, 3, 4, 5, 6.
I could do this, touch A, B, C, D, one, dash,
X, Y, Z 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 gonna create nine files.
So it's just a practice that we are doing for nine files.
Trust me, in corporate environment,
you're probably gonna have to create
like thousands of files.
What you gonna do?
You're gonna just hit up arrow key
or type it a thousand times?
No, that's not productive. So then what do you do?
We use wildcards.
So first of all I'm gonna do is I'm gonna remove these
files one by one.
Again, I could remove it, wildcard as well.
But let's create it first.
Then I'll show you how I could remove it.
So I have two left.
Now I could use a wildcard to remove both in one shot.
Remove A, B, C, wildcard.
So what is telling it?
Remove everything that has a file name that starts with A,
B, C, and whatever that comes afterward,
you don't care, just remove it.
I hit enter.
That's my LTR, and you'll see all those two files are gone.
Now I'm gonna create nine files,
and to create nine files, I'll use the command touch.
As you know, the touch is the command to use.
And then there's a wildcard, another wildcard
that's called curly braces.
So I was creating file A, B, C, D, curly braces, one,
dot dot nine because I'm gonna create nine files.
So this curly brace is one dot dot nine
so I need to create nine files
that has digit one through nine and dash X, Y, Z.
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'll see
I have nine files.
Awesome, right? This is what you want it to do.
It is much more faster to do in the wildcard as opposed
to typing one by one.
So let's use the asterisk key.
What I just want to list every file name that starts
with A, B, C.
You do A, B, C, asterisk
and you'll have all the files that starts with A, B, C.
I want to remove every file that starts with A, B, C.
You could remove one by one, A, B, C, D, one,
and so on and so forth.
Again, very cumbersome, don't do it.
You are a system administrator. You are smart folks.
You don't wanna use the commands one by one.
What you would use, you'll use a wildcard.
So you'll do A, B, C, D or A, B
and do asterisk.
The reason I picked the four letters is
because if I just pick A
and hit asterisk, it will also remove any other files in
my home directory
that starts with A, in 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 wanna 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.
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 gonna 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 X, Y, Z?
Same way, remove.
But this time we'll put asterisk in front of X, Y, Z.
Hit enter. Boom, all gone.
Awesome.
I'm gonna 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 minus L,
question mark, B, C, D, asterisk.
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 B, C, D,
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 wanna make sure I will get the listing of the files
that has B, C, D in it and after the A.
So that's how you use the question mark.
And the last one we have brackets.
Bracket is gonna tell me
that, give me anything that has
C or D letter in a file.
It's just that simple, either C, either D, either F
or this and that.
So you put all that stuff that you're looking for inside
of the bracket and you'll have all those files you see here.
And it went so fast. So I'm gonna put it in more the 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 has either has A
or B, either has A or B.
So that's how it works. That's how the wildcard works.
Again, now I want to get rid of all the files
that has Y, X, and Y in it.
Awesome. I have my wildcard.
I'm not gonna go and delete one by one.
So this command is telling the shell
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 I, we do have these matching files, X, Y,
and it will go ahead and delete it
and all of them are gone.
So while card 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 examples, try them.
Just get familiar with them.
This will help you a lot in your job. I promise you.