Video summary
The video introduces wildcard patterns as a powerful tool in Linux for performing operations on groups of files that share specific naming characteristics, rather than dealing with individual files one by one. Instead of typing out long lists of filenames, users can employ special symbols to define rules that match entire sets of files based on their structure. Common scenarios include copying all HTML files to a web directory, moving files starting with a specific number sequence, or deleting files with exactly two characters in their name. The primary wildcards discussed are the asterisk, which matches any number of characters including zero, and the question mark, which represents exactly one single character. By combining these symbols, users can create flexible patterns to filter and manipulate directories efficiently.
Beyond basic matching, the tutorial explains how to use square brackets to define character classes, allowing users to specify sets of related characters such as all lowercase letters, all digits, or alphanumeric combinations. These classes provide a more reliable method for filtering files compared to using character ranges like A through Z, which can yield unexpected results due to system-specific locale settings and accented characters. For instance, the video demonstrates that a range defined from A to Z might exclude a file with an accented letter even if it appears visually lowercase, whereas a dedicated lowercase character class captures all such variations correctly. This distinction is crucial for ensuring commands work consistently across different environments without missing files due to subtle encoding differences.
The presenter emphasizes practical application by walking through various command examples, such as listing files containing the digit four anywhere in their name or selecting files that do not start with a number. The video also highlights the importance of case sensitivity in Linux, noting that commands like `ls` will only match filenames with exact capitalization unless specified otherwise. Throughout the demonstration, the narrator periodically clears the screen to show how different patterns filter the file list, making it easy to visualize which files are selected and why others are excluded. This hands-on approach helps viewers understand the logic behind pattern matching and reinforces the concept that wildcards are essential for automating repetitive tasks.
In conclusion, while wildcard patterns may seem confusing at first glance, mastering them significantly improves workflow efficiency in Linux environments. The video advises against relying on character ranges due to their potential for inconsistency and strongly recommends using standard character classes for better reliability and completeness. Once users become familiar with these symbols and rules, they can quickly organize, search, and manage large numbers of files without manually listing each one. Ultimately, wildcards serve as an incredibly useful time-saving mechanism that transforms tedious manual operations into simple, single-line commands capable of handling complex file grouping tasks.
Read the full video transcript
Up to now, we've been using specific
file names for commands.
Each of these commands works with one
file or directory at a time.
But sometimes we want to be able to do
things with a group of files whose names
follow some pattern.
For example, we might want to copy all
files ending in .html to directory named
web files.
Or we might want to move all the files
starting with 19 followed by two digits
to directory named last century.
Or we might want to remove all the files
that have exactly two characters in
their name.
We use wildcards to specify a pattern, a
group of files, rather than a single
specific file.
The book gives several examples of
wildcards.
Let's see them in action.
Let's change to directory called
wildcard examples
and list the files in that directory.
We're going to use the ls command with
wildcards to list only the files that
fit the patterns.
As I go through these commands, I will
periodically clear the screen and reshow
the list of all the files so you can
more easily see which files are matched
by the patterns.
Our first pattern is ls star,
which shows all the files because star
matches any number of characters,
including zero.
What about all files beginning with the
letter G?
We'll type ls and then G followed by any
number of characters.
And only those two fit the pattern.
How about any file that begins with B
followed by any number of characters and
ending in .txt?
And we have beta.txt.
You'll notice that this file beta.go
does not show up because even though it
does begin with the letter B, it doesn't
end in .txt.
You can use the star pattern several
times in a pattern match.
For example, I will want to list all
files that have zero or more characters
followed by the digit four
followed by zero or more characters.
In short, these will be files with a
four anywhere in their name.
And you will see that we got the files
we want.
Let's clear the screen
and show again all the files.
Next, let's show all the files that
begin with the word data followed by
exactly three characters
where the question mark stands for any
single character.
These files data1066
and data123
don't match.
Data1066 has four letters after it and
data123 doesn't begin with a capital D.
Remember, Linux file names are case
sensitive.
Finally, let's talk about character
classes.
A character class is a group of
characters that are related.
We use them when specifying a set of
characters to match.
For example, here's a set that matches
any character in the range A through Z
lowercase.
We can also say, here is a set that
matches any character in the lowercase
character class.
The outer square brackets tell Linux
that we're creating a set.
The inner square brackets and colons
say, "This is a class of characters to
include in the set."
Here are the most commonly used
character classes.
Lowercase letters,
uppercase letters,
any alphabetic character,
any digit,
and alphanumeric, which is a combination
of alpha and digit.
Now, we can use a character class to
list all files that start with an
uppercase letter.
We'll say LS,
and then we're going to open a set,
and in that set, we were looking for
uppercase letters as a class.
And that's the end of our set.
Anything in that set followed by zero or
more characters, that's what we're
looking for.
And there are the files that match that
pattern.
How about any files that do not start
with a digit?
That means we're looking for a set
that includes everything except
the digit character class.
Followed by zero or more characters.
And those are the files that match.
We can also look for any file that ends
in lowercase or the digits 1, 2, or 3.
Let's look for LS,
any number of characters,
followed by a set of characters, and
that set of characters includes
all lowercase
and 1 2 3.
Anything in that set will match.
And again, these are the files that end
in either a lowercase letter or the
digits 1 2 and 3.
The book says you should not use
character ranges such as A through Z
because it could give unexpected
results.
Instead, you should use character
classes such as lower.
Let me show you why this is.
Let's create one more file
called San Jose
with an accented E.
If I run this command that I had before,
it shows San Jose.
But, the way my system is set up, if I
were to do this
and use any number of characters
followed by the set consisting of A
through Z
or 1 2 3,
we don't see San Jose because even
though that accented E is lowercase, it
isn't in the set A through Z as far as
my system is concerned.
The moral of the story,
use character classes. They're more
reliable and more complete.
Wildcard patterns may seem a bit odd at
first, but once you get familiar with
them, you will see that they are
incredibly useful time savers.