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
As we mentioned before, a path is a series of directory names, chained together with slash characters. There are two types of paths, "relative" and "absolute". In this video, we'll focus on relative paths, and we'll look at absolute paths in the next video.
- A relative path is a file path that's relative to your starting location in the file system.
- Suppose we want to print the contents of the
menu.txt
file in thestarbunks
directory we saw before.- Previously, we changed into the
starbunks
directory to access the file, but we don't always have to do that. We can do it from another directory. - Let's type our
cat
command name... - And we know the
starbunks
directory is within themall
directory. So let's type:mall
, and then a slash:mall/
... - Then we'll type the name of the
starbunks
directory, and another slash:starbunks/
... - And finally we'll type the name of the file within the
starbunks
directory:menu.txt
- Because you provided a path, the operating system knows how to go from folder to folder to find the file.
- Previously, we changed into the
treehouse:~/workspace$ cat mall/starbunks/menu.txt
Venti Iced Mocha Soy Latte (with Whip): $29.99
Grande Hot Americano: $34.99
Tall Hot Chocolate: $24.99
- By the way, a quick tip - you can use tab completion to do this as well.
- Now, let's suppose I was inside the
mall
directory instead of outside it:cd mall
- If I try the same path as before:
cat mall/starbunks/menu.txt
- ...it won't work, because there's no
mall
directory inside themall
directory. - Instead, I have to adjust the path to account for my starting location, and leave the
mall
directory off of the path. - If I type
starbunks/
, and thenmenu.txt
, the operating system will be able to find the file:cat starbunks/menu.txt
- If I try the same path as before:
- And of course, if I'm in the same directory as the file...
cd starbunks/
- ...Then I don't have to provide a path, I can provide just the file name:
cat menu.txt
- ...Then I don't have to provide a path, I can provide just the file name:
treehouse:~/workspace$ cd mall
treehouse:~/workspace/mall$ cat mall/starbunks/menu.txt
cat: mall/starbunks/menu.txt: No such file or directory
treehouse:~/workspace/mall$ cat starbunks/menu.txt
Venti Iced Mocha Soy Latte (with Whip): $29.99
Grande Hot Americano: $34.99
Tall Hot Chocolate: $24.99
treehouse:~/workspace/mall$ cd starbunks/
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
Paths work with directories, too.
- If you run
ls
without any arguments, it will list the contents of the current directory.- But if you give it a directory name as an argument, it will list the contents of that directory instead:
ls mall
- As always, if you want, you can put a slash at the end of a directory name, and it will work the same way:
ls mall/
- But if you give it a directory name as an argument, it will list the contents of that directory instead:
- I can join multiple directory names together with slashes:
ls mall/starbunks/
ls mall/dullards/jewelry/
treehouse:~/workspace$ ls mall
dullards map.txt starbunks
treehouse:~/workspace$ ls mall/
dullards map.txt starbunks
treehouse:~/workspace$ ls mall/starbunks/
menu.txt
treehouse:~/workspace$ ls mall/dullards/jewelry/
catalog.txt
As we mentioned before,
0:00
a path is a series of directory names
chained together with slash characters.
0:01
There are two types of paths,
relative and absolute.
0:06
In this video,
we'll focus on relative paths, and
0:10
we'll look at absolute
paths in the next video.
0:13
I'm gonna make sure I'm in my
workspace directory for this video.
0:16
A relative path is a file path that's
relative to your starting location
0:21
in the file system.
0:26
Suppose we wanna print
the contents of the menu.txt
0:27
file in the starbunks
directory we saw before.
0:30
Previously, we changed into the starbunks
directory to access the file, but
0:36
we don't always have to do that.
0:41
We can do it from another
directory instead.
0:43
Let's type our cat command name, and
0:46
we know the starbunks directory
is within the mall directory.
0:48
So let's type mall and then a slash.
0:51
Then we'll type the name of the starbunks
directory, and another slash.
0:56
And finally, we'll type the name of
the file within the starbunks directory,
1:01
menu.txt.
1:04
Because you provided a path,
1:07
the operating system knows how to go
from folder to folder to find the file.
1:08
By the way, a quick tip, you can use tab
completion to do this as well, star, tab.
1:13
M-E-N, tab, now let's suppose I was inside
the mall directory instead of outside it.
1:22
If I try the same path as before,
1:29
it won't work, because there's no mall
directory inside the mall directory.
1:31
Instead, I have to adjust the path for
my starting location and
1:36
link to the mall directory off the path.
1:39
If I type just starbunks/menu.txt,
1:42
the operating system will now
be able to find the file.
1:45
And of course,
if I'm in the same directory as the file,
1:49
then I don't have to provide a path,
I can provide just the file name.
1:53
Paths work with directories, too.
1:57
Let me change outside the mall
directory again, cd .., cd...
2:00
If you run ls without any arguments,
2:05
it will list the contents
of the current directory.
2:08
But if you give it a directory
name as an argument,
2:10
it will list the contents
of that directory instead.
2:13
As always, if you want, you can put
a slash at the end of a directory name and
2:16
it will work the same way.
2:20
I can join multiple directory
names together with slashes.
2:22
So I can say ls mall/starbunks, and
2:25
it'll list the contents of
the starbunks directory.
2:29
I can say ls mall/dullards/jewelry, and
it'll list the contents of that directory.
2:32
So that's how to work with relative paths.
2:40
In the next video,
we'll look at absolute paths.
2:43
You need to sign up for Treehouse in order to download course files.
Sign up