Hello the beautiful people.
And welcome to this video where you're going to be learning about wild cards.
Now, other than having an awesome name, wild cards are very useful tools that will make your commands
a lot more precise, a lot more expressive, and a lot more powerful.
Now, in this lecture, you're going to learn about the most common types of wild cards, and you'll
see how they can be very useful for manipulating files in Linux.
By the end of the video, you'll know how to use some of the most common types of wild cards, and you
will also have the ability to create more powerful commands.
So let's go ahead and get right into it.
Okay.
So first, let's take a look again at the LSE command.
Now, the LSE command will list the contents of every directory that we give it a valid path to.
So, for example, let's say we wanted to list the contents of our documents directory.
We could do LSE and then give it a path to our documents directory.
So we use the slash and then maybe we'd throw in a bit of tab completion and get it to list out our
documents, or if we're already in our home folder like we are now.
As you can see, we could just say list, list out documents, and that'll do fine.
But what if we wanted to have a look at what's inside our documents folder and downloads folder and
our pictures folder at the same time?
Well, let's clear the screen.
The way we do that is we just say LS then document, bit of cheeky tab, auto completion downloads,
bit of cheeky tab, auto completion and some pictures with a bit of cheeky tab or to completion.
And then if we go ahead and press enter now, you'll see that we can actually see the contents of each
directory are listed.
So far so good.
But you can see how manual of a process this was, right?
We had to type out documents, then type out downloads and then type out pictures even with even with
table to completion in order to pass them as command line arguments to the RLS command.
Now wild cards are basically special symbols that the shell interprets to have a special meaning.
The idea behind wildcards is to build up powerful patterns known as regular expressions, so that instead
of typing out every command line argument manually, you can instead say something like, Hey, Linux,
make this command act on anything that matches this pattern.
The patterns are known as regular expressions, and you can think of wildcards as being some of the
useful building blocks for creating regular expressions.
Now, using wildcards to build regular expressions gives you lots of flexibility and control over how
your commands work and can save you.
A whole bunch of typing and using regular expressions makes your commands a lot more expressive.
So for example, instead of expressing the LS command like list out the contents of this folder and
this folder and this folder, which is the way that we would usually be using it with command line arguments,
right?
We'd be saying, let's do documents and downloads and pictures, downloaders, download, downloads
and pictures.
That's how it works like that.
You've got to list them out manually.
Instead, you could just do something like, Hey, why don't you just list out the contents of every
file that starts with the letter D?
For example, if you're removing files instead of saying, Remove this file and this file and this file,
you can say, remove every file in this folder as long as its name ends in txt, for example.
Or if you're copying files instead of saying Copy this file and this file and this file, you can say
copy every file from this folder and to this other folder as long as its name ends with PDF.
Okay, so now that we understand that we're using wildcards to build these patterns, these regular
expressions, let's start building some regular expressions using wildcards.
Now, one of the most used wildcards is the asterisk or the star wildcard.
Now I find the star by pressing shift and eight on my keyboard.
If I press shift in eight, I get this star come up.
If you ever use Microsoft Excel or a, you know, a software spreadsheet package, it's the symbol you
use to do multiplication, but you find it using shift and eight on my keyboard it might be different
on yours but it's that's it's that symbol we're looking for now wildcards each match a specific pattern
and the asterisk or the star wildcard matches any piece of text regardless of the length of that text.
And it also matches numbers.
So let's say that we wanted to check the contents of every directory in the home folder.
Let me clear the screen if I clear the screen instead of listing them all out.
Okay, because there's quite a few folders in here, right?
Instead of saying, okay, LRS desktop and documents and downloads and so on and so on, all we have
to do is just do ls star.
So if I press, if I do that, you can see that it has just listed out all of the contents of the home
folder, the desktop documents, downloads, music pictures, public templates and videos all in one
go.
So let me clear the screen.
Okay.
So how did that happen?
The reason that happened is because the star matches anything.
So what you need to think of is when you give a command a regular expression to work with, imagine
it putting as command line arguments, anything that matches that pattern.
So because the star wildcard matches anything, it put everything as command line arguments to the command.
Does that make sense?
Let's take another example.
So let's say I just wanted to list out the contents of the folders, starting with D.
So the desktop downloads and documents folders.
What I could do is I could say, okay, let's do ls then a capital D and then a star.
Now the.
How often is this bit?
The pattern is the d them the star and the star is one of the building blocks, but so is the letter
D.
So what we're saying here is list out, then giving it as a command argument, anything that has a D,
a capital D, and then followed by anything.
So basically anything that starts with a capital D.
So if I do that, you can see that we just see the desktop documents and downloads and you can see that
they all match this pattern, this D star.
They start with a capital D and then A followed by anything.
They start with a capital D and then are followed by anything.
What if we just want documents and downloads?
Well, they start with D0 and then are followed by anything.
Right.
So if I do lhs doe then anything you can see we just get documents and downloads.
But desktop is ignored.
Now, something that is important to remember here is that these wild cards and these regular expressions
are case sensitive.
So if we did LHS DD and then a star notice how the D is lowercase.
You can see that we get nothing.
Nothing comes back because nothing starts with a lowercase d, but if we do capital D now, that will
work.
Okay, so that's really awesome.
Let me just clear the screen.
You've seen a few ways that the star wild card can be used to match any text, and you've seen some
ways that it can be used to build regular expressions.
Now there are a few other common wild cards that you'll likely use when working with regular expressions
and wild cards.
And the first is the question mark wild card.
Now the star wild card will match anything regardless of length.
So for example, if I do something like star text that could match anything as long as it ends in text.
So if I do ls star dot text, you'll see that we only see things that end in dot text.
Now it doesn't matter if what at the beginning of the of comes before text is one letter long, one
number long or 100 letters longer is full of numbers or whatever.
It doesn't matter if it's a thousand characters long, it'll still match because the star matches anything
but the question mark.
Wildcard will only match one place.
So if I do question mark text if I do LHS now, nothing will actually show up because the question mark
this this says basically what this says is show anything that ends in text but only has one thing before
it.
Okay.
So this would match things like a text or Q text or L text or one point text.
You notice how they've all just got one place just before the dot text.
The question mark is a way of saying is it's like a placeholder for just that place, whereas the star,
it doesn't matter how long it is.
So the question mark wildcard allows you to maintain the generality of the star wildcard because it
doesn't matter what it matches, it could match a number or it could match a letter.
But but it allows you to limit the length.
The star doesn't put a limit on length, whereas the question mark does, but it doesn't really matter
what fills that place.
Now you usually use the question mark wildcard when you want to build a general pattern but care about
the length of the file name.
So if we did something like question mark, question mark dot PDF, then that would match things like
a one dot pdf or B two dot pdf or a PDF or z seven dot pdf or something like 77. PDF.
You see how we're just taking up two spaces and then PDF.
That's what question mark question mark PDF would do.
We're saying we're going to fill it with two things, two spaces.
We're not sure what's going to go there, but there's got to be two spaces, two places and then dot
PDF.
Okay, so that's how that works.
Okay.
That's the question mark.
Now another type of wildcard is called is the square brackets, which are these ones here, these square
brackets here.
Now the square brackets wildcard is similar to the question mark in that it will only match one place.
But instead of matching anything like the question mark, will square brackets allow only a the pattern
to be they allow it to be more specific and only match the letters or numbers if they belong to a given
set of options.
Probably bit easier for me to show you than to explain.
So let's say we had six files.
We had file a txt, we had file b txt, we had file c dot txt and we had file one txt file to txt and
file three dot txt which hint hint.
If I do an LZ, we actually already have those in in our home directory.
Okay.
So let's say we wanted to match the files that just had numbers in the file name so far.
One txt file 2.2 and file three dot txt.
But we don't want the ones that have a, b and C to do this.
We could do less then file because the names got to start with file, then open the square brackets
and give it a bunch of options.
We could say 1234567890. txt.
Okay, so what's this?
Basically everything in these square brackets is an option that could fill that one place.
So anything that goes file and then either one, two, three, four, five, six, seven, eight, nine
or zero any one of those then dot txt.
So if we do that, you see that we get far one, file two and file three, but we don't get the A,
B and C because they're not part of the options, right?
From a given set of files that just matches file one, file two and file three.
Now, but what I want you to notice is how this wouldn't be possible using either the star wildcard
or the question mark wildcard.
If we do ls file.
Question mark.
TXT the question mark will match anything oc but only one place.
If we do the star you're going to get oop.
I need to ls it you're going to get basically the same thing as well because these don't restrict what
goes in that place.
The star will have anything regardless of length.
The question mark will need just one place, but it doesn't care what fills it.
The square brackets also only have one place, but they allow you to restrict what goes there.
So they're like the most restrictive.
It's like a very restrictive kind of kind of wildcard.
Now, you may have noticed that typing out all these numbers, 0123456789 is a bit of a hassle, right?
So the shell makes it a bit easier for us.
We've got a shortcut.
We can just do files 0 to 9 and that gives us exactly the same thing.
So instead of having to type out all the numbers, it's much easier, which is really useful for when
you want to work with the alphabet and you can go A to Z and that will work perfectly.
And what if you want to match two digits?
So what if you wanted something like file 11?
How would I do that?
Well, the way you do that is you'd say ls file then for the first digit 0 to 9, then just give it
another one, another digit 0 to 9, then dot txt.
So this one becomes the first digit, this one becomes the second digit.
What if I wanted to have a number and then a letter?
Well, that's easy.
Just change that to letters.
What if I wanted to have a number, then a letter, then a lowercase letter?
Well, that's simple.
Just change that like that.
And you can see now we're going to have file then one space which can have the number 0 to 9, then
another space which can have the number.
We can have the letters A to Z in uppercase, then another space which can have the letters A to Z in
lowercase and then ends in dot txt.
So you can see just how powerful these regular expressions can become just by adding a few simple wildcards.
We could also what if we wanted to have the numbers 0 to 9, but also the letters A, B, and C?
You can do that as well, just like that.
And that's going to show up.
So exactly what you'd expect.
Anything that has the word file, then one of the numbers from 0 to 9 or letters A, B and C and uppercase,
then dot txt.
If I reduce that to lowercase ABC now we'll just get four one, two and three because these wild cards
and these regular operations are case sensitive.
So you can see wildcards are really, really powerful stuff, guys.
And the only three that you really need to know are the star, which matches anything, the question
mark, which matches anything but only for one place and the square brackets which allow you to match
just one place.
But they also allow you to define what matches.
Okay.
So that's enough wild card.
Goodness.
For now, wild cards can make your commands a lot more expressive and give you much greater control
over how the commands work.
And using wild cards in this way to search for file names actually has a funny, funny name and it's
called Glomming.
So if you ever hear the word glomming floating around, it just means using wild cards to look for file
names, just like we've done in this video.
So before closing out, let's take a quick recap of what we've learned.
First, you learn that about the concept of regular expressions.
And regular expressions are basically patterns that a piece of text needs to match.
And anything that matches that pattern will be passed as a command line argument to the command.
Now, the star wild card matches anything regardless of length.
The question mark matches anything but just for one place, and the square brackets match just one place,
but allow you to specify options that will actually be matched.
So unlike the star in the question mark which will match anything the square brackets allows, you choose
what will be matched.
And of course you can just type letters and numbers in that you want specifically to appear as well.
So if I did Capital DX then Star, that means anything that starts with a D and then has anything afterwards.
So you can type letters and numbers in there as well.
You don't just have to only use the star question mark and square brackets.
So with wildcards, understood, you have a very solid foundation for learning about the different ways
to manipulate files in the file system.
Now nature the commands to create, delete, move, copy and paste files and so on.
It's actually rather simple, and when you learn them, you might wonder what's the point of actually
learning how to use them instead of just pointing and clicking?
But by knowing how to use these commands along with wild cards, you'll be able to manipulate hundreds
of files at once and with complete control, to the point that pointing and clicking will soon seem
like a very slow and sluggish way of doing things, believe me.
So up next, you can have a quick quiz to help you cement your understanding of wild cards because,
you know, practice makes perfect.
And then in the next video, you're going to learn how to create files and directories using some new
commands, and you're also going to combine those commands with wild cards to make them even more powerful.
You'll even learn about how something called brace expansion, which will see, which will show you
how you can create three folders with 100 files in each of them using just one or two commands.
Don't believe me.
Well, I guess I'll see you in the next video.