This course will be retired on March 4, 2019. We recommend "Introduction to the Terminal" for up-to-date content.
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
In this video we will see how to move around in the filesystem using the cd
command. We will also explore the different ways to write the paths to the folders on our computer.
Commands
Further Reading
-
0:00
So, we saw how to run a basic command.
-
0:02
Now, let's learn a few more.
-
0:04
In the command line, you're always somewhere in the filesystem of the computer.
-
0:08
Much like how you navigate through folders using Windows Explorer or Mac OS Finder,
-
0:12
there are multiple levels of folders that you can navigate through in the console.
-
0:18
Folders contain other folders, often nested several levels deep.
-
0:22
You've probably seen this represented as the file tree
-
0:24
with each level of depth indented another level
-
0:27
representing the file system as a whole.
-
0:30
When using Linux or other POSIX operating systems,
-
0:33
what you call a folder is usually called a directory.
-
0:36
This is because they were called directories long before the desktop
-
0:39
was invented, and metaphors to real items like desktops, windows,
-
0:44
and folders were used.
-
0:46
Folders and directories are the same thing, but we'll be referring to them as directories.
-
0:51
So, let's find out where we are.
-
0:53
In order to find out the directory you are in, there's a command called "pwd."
-
0:59
Pwd stands for print working directory.
-
1:01
Again, like any other command, we can type in "pwd" and then hit enter.
-
1:07
The output of this command is the path to where we currently are in the file system.
-
1:12
Right now, we are in the default location that we were placed when we logged in to the computer.
-
1:17
This is /home/treehouse, and this is something called our home directory.
-
1:24
In most operating systems, you have a special directory called your home directory.
-
1:29
This is where directories like my documents and pictures exist.
-
1:33
There's a different home directory for each user on the computer.
-
1:37
On Linux, this directory is in /home/username.
-
1:40
In our case, our username is treehouse, so our home directory is in /home/treehouse.
-
1:47
On the Mac, the home directories are in /users/username.
-
1:52
This directory is so special that it has it's own shortcut name--tilde.
-
1:56
This means "my home directory."
-
1:59
In fact, that's why there's a tilde on your prompt.
-
2:02
Instead of showing you the full path, it's showing you the abbreviated path.
-
2:05
So, the tilde just means that you're at home.
-
2:09
Now, let's move around.
-
2:11
So, we see where we are--we're in our home directory--and we can move around.
-
2:16
Now, we saw LS before, so we can get our bearings a little bit more.
-
2:19
Pwd tells us where we are, and LS tells us what's available in the directory we're currently in.
-
2:25
We're currently in our home directory, which contains a documents directory that we could move into,
-
2:30
and a hello.txt file.
-
2:32
So, let's move into the documents file.
-
2:35
So, how do we do that? We use the "cd" command.
-
2:39
Cd stands for change directory, and like it would imply, it changes the current directory we are in.
-
2:47
So, the way we use that is type in "cd" and then wherever we want to go.
-
2:52
In our case, we want to move into the documents folder,
-
2:55
so we can type in "documents."
-
2:57
Now, here's a cool tip: In most environments, not all--again, this is a luxury not a necessity--
-
3:04
there is something called tab completion.
-
3:08
The console that we're interacting with or something we call the shell
-
3:12
that is actually handling our keystrokes, can actually do some pretty cool things
-
3:16
with tab completion. Since it knows all about the files and folders on our computer,
-
3:20
It can fill in things for us.
-
3:24
For instance, after we type in "cd" if we type in "do" and we could just hit tab,
-
3:30
it's going to fill in the rest for us.
-
3:34
If there's multiple options, we may be able to tab through it using multiple tab presses,
-
3:37
or it may tell us what our options are so we can type in a little bit more of our option.
-
3:41
This tab completion is really nice because it allows us to move around fairly quickly.
-
3:47
You'll notice the trailing slash.
-
3:49
It doesn't matter in this case. The slashes separate multiple levels of directories
-
3:54
that we want to move through.
-
3:55
So, if there was another directory inside of documents, we could type in another directory.
-
3:59
However, I only want to move into documents, so this is fine.
-
4:03
The trailing slash could be there or it could just do without.
-
4:06
So, now that we see, though, there's something a little bit different.
-
4:09
Our prompt has changed.
-
4:10
It still has our username, but now our current directory has changed a little bit.
-
4:15
Instead of being the shortened home path of just tilde, it's now tilde/documents,
-
4:21
which means it's our home directory,
-
4:22
and inside of that we're currently in our documents directory.
-
4:26
And this is where our prompt is very useful.
-
4:28
It's telling us where we are, which gives us some context as to what we can do.
-
4:33
So, now if I were to type in "pwd" or print working directory,
-
4:36
it's now /home/treehouse/documents.
-
4:40
And if I type in "ls," we can see that there's a file here called "How to go home.txt."
-
4:47
Now, we'll learn about how to actually read and edit that document in a little bit.
-
4:52
But, let's keep learning how to move around.
-
4:53
What we did in this command here is type in "cd documents,"
-
4:58
and this is what we call a relative path.
-
5:01
Because we were currently at the time in /home/treehouse which had a documents folder,
-
5:06
we're able to specify in relative terms where we wanted to go
-
5:10
because the documents existed in our current directory.
-
5:14
However, we can use an absolute path as well.
-
5:17
But, let me show you something.
-
5:19
If you type in "os," you see that there's only that one file.
-
5:23
So if I were to type in "cd documents" again, it wouldn't make sense
-
5:28
because there's not a documents folder in our current directory,
-
5:31
and so we can't go into documents through a relative command like this
-
5:35
because relative to where we are now, there is no documents folder
-
5:39
because we're in it and there's no extra folder called documents inside.
-
5:43
So, if we run into it, we're going to get a different output.
-
5:47
In this case, it says "-bash."
-
5:49
Now, bash is a program which we're actually interacting with right now.
-
5:53
It's called our shell.
-
5:56
Now, there are different shells, but bash is going to be the one you're going to probably
-
6:00
be interacting with the most.
-
6:02
Others are ZSH, CSH, TCSH, and pretty much anything that ends with "sh."
-
6:10
Now, most of them are similar; however, there are differences.
-
6:13
But when you see "bash," it just means the program that's taking our input
-
6:18
and handling things like coloring, as well as tab completion,
-
6:22
so it's something we can configure, but it's just the program that's really handling our input and output.
-
6:28
And because we typed in "cd documents," there's an error,
-
6:31
and so it's saying that cd had an error
-
6:34
that documents--there's no such file or directory,
-
6:37
so we can't just use the same relative path if the relative path doesn't exist.
-
6:43
However, we can move around with absolute paths.
-
6:46
Now, an absolute path differs in that it starts with one of a couple of characters.
-
6:51
One of those characters is a slash and the other is a tilde.
-
6:55
Now, there's some other ways to specify an absolute path,
-
6:58
but the idea is the absolute path starts with some point.
-
7:02
In the case of a slash, it means that we're specifying the full path of the directory
-
7:06
from the root or the top of our document tree.
-
7:10
You'll notice that in our pwd, the full path starts with a slash--
-
7:13
which means the top of our directory--and then there's a home folder
-
7:17
and then there's a treehouse folder, so we can use that exact same format
-
7:19
to move around with cd.
-
7:22
For instance, if we wanted to move home, we could do a cd/home/treehouse.
-
7:31
And you can see we're home.
-
7:32
Again, in our prompt we see the tilde which is the shortened version of our home directory,
-
7:36
but if we type in pwd, we can see we are exactly where we wanted to be.
-
7:41
And so that was an absolute path, because it began with a slash meaning our root.
-
7:46
There's also another one we can use, our tilde as our starting point
-
7:51
which means that we want to give a full path starting at our home directory.
-
7:56
For instance, if I was in the /home directory by going to cd/home,
-
8:02
we could type in "pwd," and we see our home directory.
-
8:06
Now, I'm going to clear it to bring our prompt a little bit higher,
-
8:09
so we can see everything, so I'm going to type in "clear."
-
8:12
And the only thing this does is it just moves our prompt to the top.
-
8:15
We could still scroll up to see our previous inputs.
-
8:19
So, if we type in "pwd" we can see we're at /home,
-
8:21
but if we wanted to go to the documents directory inside of our home directory,
-
8:25
we could use cd/home/treehouse/documents.
-
8:32
But remember, /home/treehouse is the same as our tilde.
-
8:36
So, instead we could use a different absolute path by starting with our tilde
-
8:40
and then typing in "/documents."
-
8:44
And this is really useful, because a lot of the files we want to get to are often
-
8:47
relative to our home directory, so the tilde is a nice shortcut.
-
8:50
No matter where we are in the file system, we can move to a directory
-
8:54
relative to our home directory by using the tilde.
-
8:58
So, if I go to cd ~ /documents, we can now see we are in ~/documents,
-
9:04
and typing in "pwd" prints out /home/treehouse which is our home directory,
-
9:08
and then we're in the documents directory.
-
9:11
Now, what if we want to go back to our home directory?
-
9:13
We saw a couple ways we could do that.
-
9:15
Another is to just type in "cd ~."
-
9:18
Because that's our home directory.
-
9:20
That should be pretty expected.
-
9:23
So, if I move back into documents,
-
9:26
we're going down into our documents directory from our home directory,
-
9:29
but what if we want to go back up?
-
9:32
We can use another type of relative URL,
-
9:34
and this is a special thing called the ".." which represents going up a directory.
-
9:39
So, right now we're currently in /home/treehouse/documents,
-
9:43
if we wanted to go back up a directory, and let's ignore the fact that's the home directory,
-
9:48
so there are a ton of different ways we could get there.
-
9:49
If we wanted to get there by going up, then we can use the ".."
-
9:54
So, if we type in "cd.." we've actually gone up a directory,
-
10:00
and now we're in /home/treehouse.
-
10:04
And this works just like any other path name, so we could go ".." going up a directory,
-
10:09
but we can also separate it with slashes and maybe use another ".."
-
10:13
to go up another directory.
-
10:16
And now we can see we're at the root directory,
-
10:17
and if we typed in "pwd," we're at the very top of our tree.
-
10:21
So, we actually can't go up any higher.
-
10:23
If we type that, we're just going to end up at the same place.
-
10:27
Now, it's interesting to note that the ".." can be used as part of any file path.
-
10:31
So, for instance, if we type in "cd/home/treehouse--our home directory--
-
10:38
if we were to type in ".." that means we want to go to the home/treehouse but then up a level.
-
10:44
So, we're actually saying "home."
-
10:47
And that's exactly where we end up.
-
10:49
So, if we were to do something like that again,
-
10:52
we could actually make a pretty complicated path by going up and down and around
-
10:56
and you can just use it as any other folder name.
-
11:00
I'm going to show you something cool. Another thing that bash provides us,
-
11:02
along with tab completion, is history.
-
11:05
So, if you use the up arrow key, in most situations it should give you the last command.
-
11:10
If you continue going up, you can scroll through your history,
-
11:13
and use down to go either direction.
-
11:16
So, if I want to recall the last cd command I did, I just scroll up until I find it,
-
11:21
So, this says we want to go to /home/treehouse, back up to home,
-
11:24
we can go to treehouse again, and that would have the effect of going to our home directory.
-
11:31
That's pretty convoluted, but if you're in a directory and you need to go up some levels
-
11:35
and then back down, you can use the ".." just like that.
-
11:39
So, now we know where we are.
-
11:40
Next, let's actually manipulate some of the files.
You need to sign up for Treehouse in order to download course files.
Sign up