Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Graphical programs like File Explorer or the Finder allow you to create, copy, move, and delete files and folders. But you can also carry out all these operations in the terminal.
- Most operating systems have a concept of "folders", which group a collection of files together.
- Folders can also contain other folders, and those folders can contain still more folders, nested as deeply as you want.
- In the shell, folders are usually referred to as "directories". Folders and directories are the same thing, but the term directory dates from before modern operating systems and their "folders" metaphor.
- The system we're on in this workspace is set up to show us what directory we're in as part of the shell prompt. But if you're on a system that's not set up that way, there's a command you can run to find out what directory you're in. It's called
pwd
, which stands for "print working directory":pwd
- When we run it,
pwd
prints out/home/treehouse/workspace
. - This doesn't exactly match what's shown in the prompt. The prompt directory includes this little
~
character, which is called a tilde. We'll talk more about what the tilde means later in this stage. - In the output of
pwd
, the tilde is replaced with/home/treehouse
. We'll talk about what this means later in the stage too. - Both the prompt and the
pwd
output contain slashes. The current directory's name appears after the final slash.
- When we run it,
- You can change between directories using the
cd
command.- Let's list the files and directories in this current directory with the
ls
command:ls
. - It looks like there are
library
,mall
, andoffices
directories here. - Let's change into the
mall
directory. We run thecd
command, and give it the name of the directory we want to change into as an argument:cd mall
. - Notice that the prompt changes to show that we're now in the
mall
directory, which is inside theworkspace
directory. - If we print the working directory with
pwd
, that will also now that we're now in themall
directory:pwd
- Now, if we run the
ls
command again, we'll see the output has changed:ls
- It lists only the files and directories contained in this
mall
directory. It looks like there's amap.txt
file here, anddullards
andstarbunks
directories. - If we try to run commands that work on files from other directories, they won't work, because those files aren't in this directory.
- So for example, if we try to print the contents of
statue.txt
withcat
, it won't work, becausestatue.txt
is in another directory:cat statue.txt
- But we can now easily run commands on files in this directory:
cat map.txt
- Let's list the files and directories in this current directory with the
treehouse:~/workspace$ pwd
/home/treehouse/workspace
treehouse:~/workspace$ ls
bird.txt cart.txt library mall offices statue.txt
treehouse:~/workspace$ cd mall
treehouse:~/workspace/mall$ pwd
/home/treehouse/workspace/mall
treehouse:~/workspace/mall$ ls
dullards map.txt starbunks
treehouse:~/workspace/mall$ cat statue.txt
cat: statue.txt: No such file or directory
treehouse:~/workspace/mall$ cat map.txt
Suite 101: Starbunks
Suite 102: Dullards
- Now let's try changing to one of the directories contained within the
mall
directory:cd starbunks
- Again, the prompt updates to show that we're now in the
starbunks
directory. - And if we run
pwd
, it will also show the new directory name:pwd
- Let's see what this new directory contains:
ls
- Looks like there's just a
menu.txt
file here. - Let's print out its contents:
cat menu.txt
- Again, changing to the
starbunks
directory makes it easy to work with the files contained in that directory. We can't easily operate on files in themall
directory anymore, because we're no longer in that directory. - For example, we can't easily print the contents of the
map.txt
file, because that's in themall
directory:cat map.txt
- Again, the prompt updates to show that we're now in the
treehouse:~/workspace/mall$ cd starbunks
treehouse:~/workspace/mall/starbunks$ pwd
/home/treehouse/workspace/mall/starbunks
treehouse:~/workspace/mall/starbunks$ ls
menu.txt
treehouse:~/workspace/mall/starbunks$ cat menu.txt
Venti Iced Mocha Soy Latte (with Whip): $29.99
Grande Hot Americano: $34.99
Tall Hot Chocolate: $24.99
treehouse:~/workspace/mall/starbunks$ cat map.txt
cat: map.txt: No such file or directory
- If we want to easily look at the contents of the
map.txt
file, we need to get back to themall
directory.- This
starbunks
directory is contained within themall
directory. - When one directory contains another, it is said to be the parent directory.
- A directory that's inside another is said to be a child directory or subdirectory.
- So we're in a
starbunks
directory that's inside amall
directory. -
mall
is the parent directory, andstarbunks
is the child directory or subdirectory. - You can see that in the output of
pwd
. It shows/home/treehouse/workspace/mall/starbunks
. - See the slash characters? Each slash separates one directory name from another.
-
starbunks
is the directory we're currently in. -
mall
is the parent of thestarbunks
directory. -
workspace
is the parent of that. And so on. - When you see a list of nested directories separated by slashes like this, it's called a file system path, or just "path" for short.
- Just as you might follow a path through a forest, a path through a file system indicates the route you need to follow through directories to get to a particular directory or file.
- Unix-like operating systems use forward slashes like you see here.
- Windows paths use backslashes instead of forward slashes, but they work the same way.
- We'll look at paths more in an upcoming video.
- This
treehouse:~/workspace/mall/starbunks$ pwd
/home/treehouse/workspace/mall/starbunks
- So we want to get back to the
mall
directory and print the contents ofmap.txt
again. But how do we do that?- If we type
cd mall
, it won't work. That just tries to change to another directory namedmall
inside themall
directory, which doesn't exist! - We'll see a solution if we run
ls -a
, to list all files. - That listing includes two strange-looking names,
.
and..
. The names use period characters, but they're read aloud as "dot". -
.
refers to whatever directory we're currently in. So we can typecd .
, but that will just change us to the same directory, so it doesn't seem to do anything:cd .
- But
..
refers to whatever directory is the parent of the current directory. Socd ..
will take us from thestarbunks
directory, back to its parent, themall
directory:cd ..
- The prompt shows that we're back in the
mall
directory. - And running
pwd
will confirm it:pwd
- If we run
ls
, we'll see that themap.txt
file is here. - And now we can re-print its contents:
cat map.txt
- If we type
treehouse:~/workspace/mall/starbunks$ ls -a
. .. menu.txt
treehouse:~/workspace/mall/starbunks$ cd ..
treehouse:~/workspace/mall$ pwd
/home/treehouse/workspace/mall
treehouse:~/workspace/mall$ ls
dullards map.txt starbunks
- If we run
ls -a
in this directory, we'll see.
and..
again. They have the same meaning:.
refers to the current directory, and..
refers to the parent.-
cd .
just changes to the same directory, as before:cd .
- And
cd ..
changes to the parent directory,workspaces
:cd ..
-
treehouse:~/workspace/mall$ pwd
/home/treehouse/workspace/mall
treehouse:~/workspace/mall$ ls -a
. .. dullards map.txt starbunks
treehouse:~/workspace/mall$ cd .
treehouse:~/workspace/mall$ cd ..
treehouse:~/workspace$ pwd
/home/treehouse/workspace
You can use tab completion with directory names, too, and it's very helpful when changing directories. Any time you're referring to a directory, you can put a slash at the end of the directory name, and it means the same thing as if you'd just put the directory name by itself.
treehouse:~/workspace$ cd mall
treehouse:~/workspace/mall$ cd ..
treehouse:~/workspace$ cd mall/
treehouse:~/workspace/mall$
-
0:00
[MUSIC]
-
0:04
Graphical programs like File Explorer or the Finder allow you to create, copy,
-
0:09
move, and delete files and folders.
-
0:12
But you can also carry out all these operations in the terminal.
-
0:16
Most operating systems have a concept of folders,
-
0:19
which group a collection of files together.
-
0:23
Folders can contain other folders, and
-
0:25
these folders can contain still more folders, nested as deeply as you want.
-
0:31
In the shell, folders are usually referred to as directories.
-
0:35
Folders and directories are the same thing, but the term directory dates
-
0:38
from before modern operating systems and their folders metaphor.
-
0:42
The system we're on in this workspace is set up to show us what directory we're in
-
0:46
as part of the shell prompt.
-
0:48
But if you're on a system that's not set up that way,
-
0:50
there's a command you can run to find out what directory you're in.
-
0:54
It's called pwd, which stands for print working directory.
-
0:59
When we run it, pwd prints out /home/treehouse/workspace.
-
1:05
This doesn't exactly match what's shown in the prompt.
-
1:08
Prompt directory includes this little squiggle character,
-
1:11
which is called a tilde.
-
1:13
We'll talk more about what the tilde means later in this stage.
-
1:17
In the output of pwd, the tilde is replaced with /home/treehouse.
-
1:22
We'll talk about what this means later in this stage too.
-
1:26
Both the prompt and the pwd output contain slashes.
-
1:30
The current directory's name appears after the final slash.
-
1:34
You can change between directories using the cd command.
-
1:38
Let's list the files and
-
1:39
directories in this current directory with the ls command.
-
1:43
It looks like there are library, mall, and offices directories here.
-
1:48
We run the cd command and
-
1:49
give it the name of the directory we want to change into as an argument, cd mall.
-
1:56
Notice that the prompt changes to show that we're now in the mall directory,
-
1:59
which is inside the workspace directory.
-
2:02
If we print the working directory with pwd,
-
2:05
that will also now show that we're in the mall directory.
-
2:08
Now, if we run the ls command again, we'll see the output has changed.
-
2:13
It lists only the files and directories contained in this mall directory.
-
2:17
It looks like there's a map.txt file here and dullards and starbunks directories.
-
2:23
If we try to run commands that work on files from other directories,
-
2:26
they won't work because those files aren't in this directory.
-
2:30
So for example, if we try to print the contents of statue.txt with cat,
-
2:34
it won't work because statue.txt is in another directory.
-
2:39
But we can now easily run commands on files in this directory.
-
2:42
For example, we can print the contents of the map.txt file.
-
2:46
Now let's try changing to one of the directories contained within the mall
-
2:51
directory, cd starbunks.
-
2:55
Again, the prompt updates to show that we're now in the starbunks directory.
-
3:00
And if we run pwd, it will also show the new directory name.
-
3:05
Let's see what this new directory contains,
-
3:09
it looks like there's just a menu.txt file here.
-
3:13
Let's print out its contents, cat menu.txt.
-
3:18
Again, changing to the starbunks directory makes it easy to work with files contained
-
3:22
in that directory.
-
3:23
We can't easily operate on files in the mall directory anymore because we're no
-
3:27
longer in that directory.
-
3:29
For example, we can't easily print out the contents of the map.txt
-
3:32
file because that's in the mall directory.
-
3:36
If we want to easily look at the contents of the map.txt file,
-
3:39
we need to get back to the mall directory.
-
3:42
This starbunks directory is contained within the mall directory.
-
3:46
When one directory contains another, it is said to be the parent directory.
-
3:51
A directory that's inside another is said to be a child directory or a subdirectory.
-
3:57
So we're in a starbunks directory that's inside a mall directory.
-
4:01
Mall is the parent directory and starbunks is the child directory or subdirectory.
-
4:07
You can see that in the output of pwd,
-
4:09
it shows /home/treehouse/workspace/mall/starbunks.
-
4:16
See the slash characters?
-
4:18
Each slash separates one directory name from another.
-
4:22
Starbunks is the directory we're currently in, mall is the parent
-
4:27
of the Starbunks directory, workspace is the parent of that, and so on.
-
4:34
When you see a list of nested directories separated by slashes like this,
-
4:38
it's called a file system path, or just path for short.
-
4:42
Just as you might follow a path through a forest,
-
4:44
a path through a file system indicates the route you need to follow through
-
4:48
directories to get to a particular directory or file.
-
4:52
Unix-like operating systems separate directories with forward slashes,
-
4:56
like you see here.
-
4:58
Windows paths use back slashes instead of forward slashes, but
-
5:01
they work the same way.
-
5:03
We'll look at paths more in an upcoming video.
-
5:07
So we want to get back to the mall directory and
-
5:09
print the contents of map.txt again, but how do we do that?
-
5:13
If we type cd mall, it won't work.
-
5:16
That just tries to change to another directory named mall inside the mall
-
5:20
directory, which doesn't exist.
-
5:23
We'll see a solution if we run ls -a to list all files.
-
5:27
That listing includes two strange-looking names, dot and dot dot.
-
5:32
The names use period characters, but they're read aloud as dot.
-
5:37
Dot refers to whatever directory we're currently in.
-
5:40
So we can type cd dot, but that will just change us to the same directory.
-
5:44
So it doesn't seem to do anything.
-
5:47
So cd dot dot will take us from the starbunks directory back to its parent,
-
5:52
the mall directory.
-
5:54
The prompt shows that we're back in the mall directory, and
-
5:57
running pwd will confirm it.
-
6:01
If we run ls, we'll see that the map.txt file is here.
-
6:05
And now we can easily reprint its contents with cat map.txt.
-
6:11
If we run ls -a in this directory, we'll see dot and dot dot listings again.
-
6:17
They have the same meaning, dot refers to the current directory,
-
6:21
the mall directory, and dot dot refers to its parent, the workspace directory.
-
6:26
cd dot just changes to the same directory as before, and
-
6:30
cd dot dot changes to the parent directory, workspace.
-
6:35
You can use tab completion with directory names too, and
-
6:38
it's very helpful when changing directories.
-
6:41
Notice that if I type cd, a space, ma, and
-
6:45
the tab key, it completes the name of the mall directory.
-
6:48
But it also adds a slash after the directory name.
-
6:52
Anytime you're referring to a directory,
-
6:54
you can put a slash at the end of the directory name.
-
6:57
And it means the same thing as if you just put the directory name by itself.
-
7:02
So we can do this both ways.
-
7:03
Let me change back to the parent directory,
-
7:05
I can type cd mall without a slash and that will work.
-
7:08
I can cd back to the parent directory, and
-
7:11
I can type cd mall/ with a slash and it works just the same.
-
7:17
Let me use tab completion to move around the file system a little bit.
-
7:21
cd S-T-A-R tab, that completes the name of the starbunks directory.
-
7:26
I'll change back to the parent.
-
7:29
cd D-U-L-L tab, that completes the name of the dullards directory, and
-
7:33
I can change back to the parent again.
You need to sign up for Treehouse in order to download course files.
Sign up