- Okay, the next thing that we should talk about,
which is related to files is using wildcards,
also known as globbing.
So, most file-oriented commands support using wildcards,
as in ls a*, which is showing all files
that have a name starting with an a, or ls a?*.
The question mark stands for one single character
one single character, any character,
and the star is an unlimited number
of unspecified characters.
So, this will show you all files that have
a name of at least two characters.
Then, we have ls a and then [nm].
The nm between square bracket means either or,
so either an m or an n.
And what do you think of ls a[a-e],
for anything between the a and the e.
Also useful, is that many commands
also support working with groups and ranges,
as in touch file{1..100}.
What is that doing?
That is creating 100 files in one single command.
Or what do you think of mkdir /data/{sales,account}
which creates two directories in one.
Let me demonstrate this.
In order to demonstrate, I'm getting into the etc directory
and in the etc directory I'm using ls b*.
So what do we see?
We see that it's showing files
that have a name starting with a b,
but then something weird is happening.
It's also showing matching directory names
and for matching directory names,
it shows all the files it found in the directory.
Now, that is exactly this thing that I just talked about.
If you use ls -d b*,
then we see directory names instead of directory contents.
So that's probably so much better.
Now let's use ls -d b*.?.
Now, what is that?
Well, let's try to understand ls b*.
The star is for any character, but then there is a dot.
That means that we need a dot in the name of the file.
And then there is a question mark,
which means that behind the dot
we need one single character.
And that is why we see bash_completion.d and binfnt.d
because these are the only ones that have one character
after the dot.
So let's have a look at another example,
ls [ab]???, and what is that showing, huh?
It is showing files.
And do these files match the pattern?
Hey, everybody,
take a second to think about what is happening here.
You know what is happening?
This is happening. Let's do ls -d.
You remember what ls is doing
when it finds the directory that matches the pattern?
It doesn't show the name of the directory.
It shows the contents of the directory.
And if we put a -d in front of it,
then we can see that it actually did what we asked it to do.
ls -d [ab] plus ??? is looking for
items that have a name consisting of four letters
starting with an a or a b.
The only matching item is alsa and without a -d,
we see the contents of this alsa thing,
and not really what we were looking for.
As you can see, the output of ls
sometimes is pretty confusing.
Now, let me use ls [a-e]*,
which is an easy one,
as long as we don't forget to use the -d.
Hey, that's also something we haven't seen before.
I'm using the -d to the end.
Can we do that? Yes, we can.
Linux is a pretty flexible operating system
and command line options can often be specified
on the end of the command line as well,
as we can see right here.
Now, let's also talk about these ranges,
and in order to do so, I'm going back to my home directory.
I am using touch file one up to many.
Should I do that many?
No, Let me just make the point and let me use 100.
And there we can see that it easily created 100 empty files.
Use any number that you want, but be careful.
And maybe you wanna use rm after it to removed that.
I'm not sure if I should be talking about it
because we haven't talked about rm yet, but rm is remove.
And in this case, I think it's useful to do that.
And what do you think of mkdir?
Mkdir /data/{sales,account}.
Now, this is an interesting one
and you should know, everybody, that every now and then
I like to show you things that go wrong.
And as you can see, this is going wrong.
Why is it going wrong?
Well, not for one, but even for two reasons.
Reason number one is that it is trying
to create a complete directory path and we cannot do that.
The error message that I'm getting here
is that it is trying to create directory sales
in the directory data but directory data does not exist.
So, let me modify the command.
Notice again that my cursor
is at the end of the command line.
I'm using control a to move my cursor
to the start of the command line
so that I can put a -p behind.
Why am I doing that?
I can do -p at the end of the command line.
Yeah, I know, but for some reason I like it
if my options come directly after the command,
and there we go and uh-oh,
different error message, permission denied.
Of course we get a permission denied.
We need sudo if you want to write
into the root of the file system
but with this command, it should be working
after we enter, of course, the sudo password.
There we go.
And if I use ls /data, then we can see,
and this time I wanted to see that
we can see the content of the /data directly
containing account and sales,
and that's exactly what I wanted.