[MUSIC PLAYING]
In this video, we're going to be discussing the delightfully
named topic of shell globbing, also known
as filename expansion.
Say you want to do something with a bunch of files
that all share a similar name--
perhaps you're deleting all of them
or you're moving them to another file.
Shell globbing lets you work with multiple files at once,
by recognising and expanding a set of wildcards.
These are asterisk,
which will match to zero or more characters.
You've also got square brackets,
where you can put characters inside here
and it'll match to listed characters.
And, finally, question mark,
which will match to exactly one character.
Note that, although this seems kind of
similar to regular expressions, globbing
and regular expressions are not the same.
The shell by itself doesn't recognise regular expressions.
And globbing is kind of its solution to doing
pattern matching with filenames.
It's bonus confusing because they share some wildcards
that actually work differently in each case.
If you want to learn about regular expressions,
you can check out our other videos on that.
OK.
Let's have a look at some of these
globbing wildcards in action.
I'm in a directory full of random files
that we're going to have a play with.
One of the most common uses of globbing
is to find files with a certain suffix.
Say I want to match all of my '.jpg' files.
I can do "echo *", for any number of characters, ".jpg".
Wonderful.
I've echoed all files that start with any number of characters
and end with lowercase '.jpg'.
But what if I also have '.jpeg', as you often have with JPEGs?
This is where the wildcard property
of matching zero or more characters comes in handy.
I can use the star in here as well.
And this will match the case where
there is no character there, and also the case
where there is a single 'e'.
Watch out, though.
It may not always match what you expect.
In my case, it was reasonably unlikely that I'd have a '.jpog'
or a '.jpig' in there.
However, say I'm looking for all files
with the word "cat" in them.
So I "echo *cat*", like this.
Cool.
I have a 'cat.jpg', 'my_cat.txt'.
However, I also have 'education.txt',
which does, strictly speaking, have the word "cat" in it,
but wasn't what I was looking for.
Globbing is not as powerful as regex.
And, often, you have to make assumptions about
what will exist in filenames in order to do a matching.
OK.
Getting back to my JPEGs.
Let's say, not only do I have JPEGs with or without an e,
I also have uppercase '.JPG'.
What can I do?
Well, I can use square brackets to match
from a set of characters.
I can list anything starting with any number characters,
then a dot, then choosing from uppercase or lowercase 'j',
uppercase or lowercase 'p', uppercase or lowercase 'g'.
Now this matches an uppercase '.JPG'.
And note that it also matches '.Jpg'.
Or, in this case, the 'j' has been matched
from the uppercase 'J' in the first set of square brackets,
and lowercase 'p' and 'g' have matched from
the lowercase characters in the second two square brackets.
I can throw in the extra asterisk as well,
to account for any 'e's that might be hovering around.
Note that I can't do a square bracket to account for this 'e'.
Exactly one character from each set of square brackets
will be matched.
There's no zero-characters case.
And this means, because we're using the asterisk,
it's not a precise match.
Another common thing I might want to do
is find all of my '.c' and '.h' files
and put them somewhere.
Let's make a directory, 'cfiles', to put them in.
And we're going to move them there by using "mv",
all files starting with any number of characters, a dot,
then, within square brackets, either 'c' or 'h',
and I'm going had to put them in 'cfiles'.
Cool.
Now if I list everything inside 'cfiles',
you'll see it's just my '.c' and '.h' files.
I can also use the square brackets to match our range.
I've got another set of files here,
just starting with all the letters of the alphabet.
Perhaps I wanted to only match with files
starting from 'g' to 'm'.
I could list 'ghijklm' within square brackets and then star,
but that's tedious.
And because you've got a continuous range of letters
here, I can make that a range by just doing 'g-m'.
Does the same thing.
The same thing works for single-character numbers.
And you can combine ranges.
So say you want 'g' to 'm' and also 't' to 'z'.
That will do that for me.
Another thing I can do here is use the not, or exclamation
mark character, to match anything not in the specified
set of letters or range.
Using this, I can select anything not starting
with a vowel--
'aeiou'.
Or anything not in the range 'm' to 't'--
or not starting with a character in the range 'm' to 't'.
Cool.
The last wildcard we're going to talk about is
the question mark, which matches to any one single character.
Before, back in my other folder, I used square brackets to match
the '.c' or '.h' files.
I could also have used the question mark
to match to any file that ends with one single character.
I'll have to look inside my 'cfiles' folder there.
And that'll do the same thing, provided that the only files
that end with a single character are '.c' and '.h' files.
A similar use that's less likely to come up with incorrect
results is listing all .mp3--
or .mp, any-number, files--
3, 4, or 5.
I can list any number of characters '.mp' and then
a single file--
or, a single character, rather.
I've got an '.mp3', an '.mp4', and an '.mp5'.
It's reasonably unlikely that I'd have an '.mpq' or an '.mp0',
or something weird.
So that's why I can sort of assume that.
One last thing I can do with this
is list all files that have a three-letter suffix with three
question marks.
And I see-- I think that's about all my files there.
They all have three-letter suffixes.
The last thing I'm going to cover is,
what if I want to match a character that is a wildcard?
To do this, I have to use the backslash character
to escape it.
Say if I wanted to look at all files that
have an actual asterisk in the name,
I can do "echo", unescaped asterisk for any number of characters,
backslash asterisk for an actual asterisk,
and then asterisk for another set of any characters.
And see here, I get all my files that have
an actual asterisk in the name.
So now you can go out and glob to your heart's content.
[MUSIC PLAYING]