Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
The asterisk is used for wildcard expansion, where a pattern is used to complete one or more file or directory names.
- Now for a special symbol that works totally differently than the others, the asterisk:
*
- The asterisk is used for wildcard expansion, where a pattern is used to complete one or more file or directory names.
- It's great for working with several files at once.
- I'm going to change to a subdirectory within this workspace's
library
directory devoted to novels by author Kim Stanley Robinson:cd ~/workspace/library/fiction/kim_stanley_robinson/
- There's files for the books in his "Mars Trilogy": "Red Mars", "Green Mars", and "Blue Mars". There's also a file for the new "Red Moon" novel. And finally there's some kind of executable script here.
- When you type part of a file name with an asterisk, the shell will treat the asterisk as a "wild card".
- It will find all files whose name matches the partial name you typed. The asterisk, the wild card, can represent a group of any characters.
- So suppose I wanted to run a command on all the files with a
.txt
extension. - I could type
*.txt
to find all of those files, and pass each of them as an argument to the command. - I'll do it using the
echo
command first, so you can see what the shell converts it to:echo *.txt
- You can see that the
*.txt
gets expanded out to four arguments, one for each file it matched. There's an argument here for each text file in this directory. - Notice that the shell script was not included, because its file name ends in
.sh
, not.txt
. - Now, instead of the
echo
command, let's use thecat
command with*.txt
, to print the contents of all the text files:cat *.txt
-
*.txt
gets expanded out to the same four arguments, but this time they're passed to thecat
command, which prints the file contents.
treehouse:~/workspace$ cd ~/workspace/library/fiction/kim_stanley_robinson/
treehouse:~/workspace/library/fiction/kim_stanley_robinson$ ls
blue_mars.txt green_mars.txt red_mars.txt red_moon.txt reserve.sh
treehouse:~/workspace/library/fiction/kim_stanley_robinson$ echo *.txt
blue_mars.txt green_mars.txt red_mars.txt red_moon.txt
treehouse:~/workspace/library/fiction/kim_stanley_robinson$ cat *.txt
A novel about the civilization of Mars.
A novel about the terraforming of Mars.
A novel about the colonization of Mars.
A novel about the colonization of the moon.
- Wildcard expansion can find all file names that end a certain way, but it can also find all file names that start a certain way.
- Let's try running
echo re*
:echo re*
- It will match
red_mars.txt
,red_moon.txt
, and thisreserve.sh
script.
- Let's try running
- We'd rather not include the shell script, so let's match files with a certain start and ending:
echo re*.txt
- That's better; now we just have the two text files.
- Let's try using this pattern with the
cat
command:cat re*.txt
- It prints the contents of just the "Red Mars" and "Red Moon" files.
treehouse:~/workspace/library/fiction/kim_stanley_robinson$ echo re*
red_mars.txt red_moon.txt reserve.sh
treehouse:~/workspace/library/fiction/kim_stanley_robinson$ echo re*.txt
red_mars.txt red_moon.txt
treehouse:~/workspace/library/fiction/kim_stanley_robinson$ cat re*.txt
A novel about the colonization of Mars.
A novel about the colonization of the moon.
- To match all files, regardless of how they start or end, use just a star:
echo *
-
cat *
will print the contents of all files:cat *
- But be warned, sometimes a wild card will match files you don't want.
- For example, in this case, we just printed the contents of the
reserve.sh
script. - If you accidentally print a file that doesn't contain text, you'll wind up with gibberish on your terminal.
- And that's just printing files. Wildcards can be used with other commands to edit or even delete files. So when using wildcards, take care to ensure you're acting on the right files.
-
treehouse:~/workspace/library/fiction/kim_stanley_robinson$ echo *
blue_mars.txt green_mars.txt red_mars.txt red_moon.txt reserve.sh
treehouse:~/workspace/library/fiction/kim_stanley_robinson$ cat *
A novel about the civilization of Mars.
A novel about the terraforming of Mars.
A novel about the colonization of Mars.
A novel about the colonization of the moon.
#!/bin/sh
echo "Not implemented"
- Wildcards can match directories, too.
- Let me change to a directory that contains both files and subdirectories:
cd ~/workspace
- And list its contents:
ls
- If I try running
echo *a*
, it will all the files and subdirectories whose names contain the letter "a". -
bird.txt
and theoffices/
directory are excluded, because their names don't contain "a". - Now let's try
cat *a*
:cat *a*
- It prints the contents of two text files.
- Two directories get passed to
cat
as well, but they get ignored. - We can also try
ls *a*
:ls *a*
- The two text files are listed out.
- It will also list the contents of the two directories.
- Let me change to a directory that contains both files and subdirectories:
treehouse:~/workspace/library/fiction/kim_stanley_robinson$ cd ~/workspace
treehouse:~/workspace$ ls
bird.txt cart.txt library mall offices statue.txt
treehouse:~/workspace$ echo *a*
cart.txt library mall statue.txt
treehouse:~/workspace$ cat *a*
A stand selling hot dogs and bottles of diet cola.
cat: library: Is a directory
cat: mall: Is a directory
A statue of a hunter standing over a dead bear. Creepy.
treehouse:~/workspace$ ls *a*
cart.txt statue.txt
library:
fiction non-fiction periodicals
mall:
dullards map.txt starbunks
- We can match all directories, but none of the regular files, with
*/
:echo */
-
ls */
will list the contents of all the directories.
-
treehouse:~/workspace$ echo */
library/ mall/ offices/
treehouse:~/workspace$ ls */
library/:
fiction non-fiction periodicals
mall/:
dullards map.txt starbunks
offices/:
dentist lawyer web_agency
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Now for special symbol that works totally
differently than the others, the asterisk.
0:00
The asterisk is used for
wild card expansion,
0:06
where a pattern is used to complete one or
more file or directory names.
0:09
It's great for
working with several files at once.
0:13
Let me show you what I mean.
0:16
I'm going to change to a subdirectory
within this workspace's library directory
0:18
devoted to novels by author
Kim Stanley Robinson.
0:22
Let me list the file it contains,
there's files for
0:28
the books in his Mars trilogy,
Red Mars, Green Mars and Blue Mars.
0:31
There's also a file for
the new Red Moon novel, and
0:36
finally there's some kind
of executable script here.
0:39
When you type part of a file
name with an asterisk,
0:42
the Shell will treat
the asterisk as a wildcard.
0:45
It will find all the files whose names
match the partial name you typed.
0:48
The asterisk, the wild card can
represent a group of any characters.
0:52
So, suppose I wanted to run a command
on all the files with a .txt extension.
0:57
I could type *.txt to find
all of those files and
1:03
pass each of them as
an argument to the command.
1:06
I'll do it using the echo command first,
so
1:09
you can see what the Shell converts it to,
echo *.txt.
1:12
You can see that the *.txt gets
expanded out to four arguments, one for
1:17
each file it matched.
1:22
There's an argument here for
each txt file in this directory.
1:23
Notice that the shell
script was not included,
1:28
because its file name
ends in .sh not .txt.
1:30
Now, instead of the echo command,
let's use the cat
1:35
command with *.txt to print
the contents of all the text files.
1:38
*.txt gets expanded out to the same for
arguments.
1:42
But this time they're passed to the cat
command which prints the file contents.
1:46
Wild card expansion can find all file
names that end a certain way, but
1:51
it can also find all file names
that start a certain way.
1:55
Let's try finding echo re*.
1:59
It'll match red_mars.txt, red_moon.txt,
and this reserve.sh script.
2:01
We'd rather not include the shell script,
so
2:07
let's match files with a certain start and
ending, echo re*.txt.
2:10
That's better,
now we just have the two text files.
2:15
Let's try using this pattern
with the cat command.
2:19
It prints the contents of just
the red Mars and red moon files.
2:22
To match all files, regardless of how
they start or end, use just an asterisk.
2:26
Echo *,
cat * will print the content of all files.
2:31
But be warned, sometimes the wild
card will match files you don't want.
2:38
For example, in this case we just printed
the contents of the reserve.sh script.
2:42
If you accidentally print a file
that doesn't contain text,
2:48
you'll wind up with gibberish on your
terminal and that's just printing files.
2:50
Wild cards can be used with other
commands to edit or even delete files.
2:54
So when using wild cards take care to
ensure you're acting on the right files.
2:59
Wild cards can match directories too.
3:04
Let me change to a directory
that contains both file and
3:06
subdirectories cd~/workspace and
I'll list its contents.
3:09
If I try running echo *a*,
it will list all the files and
3:17
subdirectories whose names
contain the letter a.
3:21
bird.text and the offices directory
are excluded because their names
3:24
don't contain a.
3:29
Now, let's try cat *a*, it prints
the contents of the two text files.
3:31
Two directories get passed the cat
as well but they get ignored.
3:36
We can also try ls *a*.
3:41
The two text files are listed out and
3:44
will also list the contents
of the two directories.
3:46
We can match all directories, but none
of the regular files with */, echo */.
3:50
ls */ will list the contents
of all the directories.
3:57
In the first stage, we learned how to
run commands and pass arguments to them.
4:03
In this second stage, we learned how
to move around the file system and
4:07
how to specify files for
your commands to work on.
4:10
The third and final stage will focus on
new commands, but the hard part's over.
4:13
We've already learned everything
we need in order to run them.
4:18
Now you get to put all you've learned
to work, see you in the final stage.
4:21
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up