Relative Paths-sass
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
- 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:
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/
Comments
Post a Comment