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 paths we saw in the previous video were relative paths. A relative path that works in one directory may not work if you change to a different directory. In this video, we're going to look at absolute paths, which stay the same no matter what directory you're in.
- If I change up to the parent directory enough times, eventually I'll reach a directory where I can't go any further.
- This is the root directory. If you imagine the file system like a tree, with directories as branches, the root directory would be the base that all the other branches spring out of.
- The root directory doesn't have a name.
- Its path is just a single slash (
/
).
treehouse:~/workspace$ pwd
/home/treehouse/workspace
treehouse:~/workspace$ cd ..
treehouse:~$ pwd
/home/treehouse
treehouse:~$ cd ..
treehouse:/home$ pwd
/home
treehouse:/home$ cd ..
treehouse:/$ pwd
/
treehouse:/$ cd ..
treehouse:/$ pwd
/
- Now let me show you a simpler way to get to the root directory.
- We started out in the
home/treehouse/workspace
directory. Let me change back there:cd home/treehouse/workspace/
- Instead of typing
cd ..
a bunch of times, I can typecd /
to jump to the root directory:cd /
- This works no matter what directory I'm in.
- We started out in the
treehouse:/$ cd home/treehouse/workspace/
treehouse:~/workspace$ cd /
treehouse:/$ pwd
/
treehouse:/$ cd home/treehouse/
treehouse:~$ cd /
treehouse:/$ pwd
/
- An absolute path is one that starts at the root directory.
- A slash by itself is the simplest absolute path; it takes you straight to the root directory, no matter where you are on the file system.
- Let's find a different directory to go to:
ls
- I'll change to this
bin
directory:cd bin/
- And now I'll change back to the root directory:
cd /
- Let's find a different directory to go to:
- Absolute paths start at the root directory, but like other paths, you can then join other directory names onto them.
- Suppose I was in the
bin
directory:cd bin/
- And I wanted to change to the
dev
directory. - I couldn't do it with a relative path, because it assumes I mean a
dev
directory that exists within thebin
directory:cd dev
- But there is a
dev
directory within the root directory. So if I start my path with the root directory, it will work:cd /dev
- Suppose I now want to change to the
etc
directory within the root directory. I don't have to change back to the root directory and then change toetc
, I can justcd /etc
to jump directly there.
- Suppose I was in the
treehouse:/$ cd /
treehouse:/$ cd bin/
treehouse:/bin$ cd dev
bash: cd: dev: No such file or directory
treehouse:/bin$ cd /dev
treehouse:/dev$ cd /etc
treehouse:/etc$
- Now let's try a longer absolute path.
- We started out in the
home/treehouse/workspace
directory. Let's see if we can use an absolute path to get back there, starting from theetc
directory. - The
home
directory is within the root directory, so we start with the root:/home/treehouse/workspace
- We're taken straight there:
pwd
- I can also jump straight back to the
/etc
directory, or any other directory, by using its absolute path:cd /etc
- We started out in the
treehouse:/etc$ cd /home/treehouse/workspace
treehouse:~/workspace$ pwd
/home/treehouse/workspace
treehouse:~/workspace$ cd /etc
treehouse:/etc$
- Absolute paths work with files, too.
- Let's suppose I want to print that Starbunks menu again, but I don't want to leave the
/etc
directory. - I can type the
cat
command name:cat
... - Then I can type the absolute path of the
menu.txt
file. - It's a long path, so I'm going to use tab completion to help me remember all the directory names.
- [
hom
, Tab,tr
, Tab,wo
, Tab,mal
, Tab,st
, Tab,men
, Tab.] - There it is, the complete absolute path of the
menu.txt
file, starting at the root directory. - And when I run the command, the
cat
program finds the file and prints its contents. - All without leaving the
/etc
directory:pwd
- Let's suppose I want to print that Starbunks menu again, but I don't want to leave the
treehouse:/etc$ cat /home/treehouse/workspace/mall/starbunks/menu.txt
Venti Iced Mocha Soy Latte (with Whip): $29.99
Grande Hot Americano: $34.99
Tall Hot Chocolate: $24.99
treehouse:/etc$ pwd
/etc
- Absolute paths are a bit less convenient than relative paths.
- If you're currently in or near the same directory as a file, you can use the file's absolute path to access it.
- But it would probably be more convenient to use a relative path.
- But when you're nowhere near the file or directory you want to access, or if you don't even know where you are relative to your target, you should use its absolute path. Absolute paths eliminate all confusion about where a file or directory is within the file system.
The paths we saw in the previous
video were relative paths.
0:00
A relative path that works in one
directory may not work if you change to
0:03
a different directory.
0:07
In this video, we're going to look at
absolute paths which stay the same no
0:09
matter what directory you're in.
0:12
If I change up to the parent
directory enough times,
0:15
eventually I'll reach a directory
where I can't go any further.
0:17
So I'll say cd dot dot.
0:20
That will change me to my home directory.
0:22
CD dot, dot again.
0:25
That will change me to
the slash home directory.
0:28
And CD dot dot one more time.
0:31
And this changes me to yet
another directory.
0:37
If I try typing CD dot dot in this
directory, it won't go anywhere.
0:40
If you imagine the file system like
a tree with directories as branches,
0:45
the root directory would be the base that
all the other branches spring out of.
0:49
The root directory doesn't have a name.
0:53
Its path is just a single slash.
0:56
Now, let me show you a simpler
way to get to the root directory.
0:58
We started out in the home slash
treehouse slash workspace directory.
1:03
Let me change back there.
1:07
CD, and I'll use tab completion here,
home slash treehouse slash workspace.
1:08
Instead of typing CD dot dot,
a bunch of times,
1:16
I can type CD slash to jump
straight to the root directory.
1:19
This works no matter
what directory I'm in.
1:23
Let's change to the home
slash treehouse directory.
1:27
CD home slash treehouse.
1:29
Without the workspace
directory on the end.
1:31
Once again, cd slash will take
me back to the root directory.
1:36
An absolute path is one that
starts at the root directory.
1:42
A slash by itself is
the simplest absolute path.
1:47
It takes you straight
to the root directory,
1:50
no matter where you
are on the file system.
1:52
Let's find a different directory to go to.
1:55
I'll run ls to list the contents
of this root directory.
1:58
And I'll change to this bin directory,
cd bin slash.
2:01
Again, the slash is optional.
2:06
And now, I'll change back to
the brief directory, cd slash.
2:09
Absolute paths start at
the route directory.
2:11
But like other paths, you can then
join other directory names onto them.
2:15
So suppose I was in the bin directory.
2:20
Cd bin.
2:22
And I want to change to the dev directory.
2:23
I couldn't do it with a relative path
because it assumes I mean a dev directory
2:26
that exists within the bin directory.
2:30
But there is a dev directory
within the root directory.
2:34
So if I start my path with
the root directory, it will work.
2:37
Cd slash dev.
2:40
Suppose I now want to change to the etc
directory within the root directory.
2:43
I don't have to change back to the root
directory and then change to etc.
2:47
I can just cd slash etc
to jump directly there.
2:51
Now, let's try a longer absolute path.
2:56
We started out in the home slash
treehouse slash workspace directory.
2:58
Let's see if we can use an absolute
path to get back there,
3:03
starting from the etc directory.
3:05
The home directory is
within the root directory.
3:08
So we start with the root, slash,
then type home slash treehouse.
3:10
And I'll start using tab completion now.
3:16
And workspace.
3:18
We're taken straight there.
3:21
I can also jump straight back to the etc
directory, or any other directory,
3:24
by using it's absolute path, cd slash etc.
3:28
Absolute paths work with files, too.
3:32
Let's suppose I wanna print
that starbunks menu again, but
3:35
I don't wanna leave
the slash etc directory.
3:38
I can type the cat command name, and
3:40
then I can type the absolute
path of the menu dot txt file.
3:43
It's a long path, so
3:46
I'm going to use tab completion to help
me remember all the directory names.
3:47
I'm gonna start with slash h-o-m tab.
3:51
completes the home directory name.
3:55
T-r, that completes treehouse.
3:57
W-o completes workspace.
4:00
M-a-l completes mall.
4:04
S-t-tab completes starbunks.
4:06
And m-e-n-tab completes menu dot text.
4:09
There it is, the complete absolute
path of the menu dot text files,
4:14
starting at the root directory.
4:17
And when I run the command, the program
finds the file and prints its contents,
4:19
all without leaving the etc directory.
4:24
Absolute paths are bit less
convenient than relative paths.
4:27
If you're currently in or
near the same directory as a file,
4:31
you can use the file's
absolute path to access it.
4:34
But it would probably be more
convenient to use a relative path.
4:37
But when you're nowhere near the file or
directory you want to access,
4:41
or if you don't even know where
you are relative to your target,
4:44
you should use its absolute path.
4:48
Absolute paths eliminate all
confusion about where a file or
4:50
a directory is within the file system.
4:53
You need to sign up for Treehouse in order to download course files.
Sign up