All of the paths so far started with /. These are absolute paths: they start at the root directory.
A path that does not start with / is a relative path. It starts at the current directory.
Same file, two paths
The current directory is /home/learner. Both lines read the same file:
learner@tidepool:~$ cat /home/learner/shop/prices.txt
learner@tidepool:~$ cat shop/prices.txt- /
- home
- learner← current directory: the relative path starts here
- shop
- prices.txt
| Kind | Starts with | Starts at | Example |
|---|---|---|---|
| Absolute | / | The root directory | /home/learner/shop/prices.txt |
| Relative | Any other character | The current directory | shop/prices.txt |
A path that starts with ~ is absolute. The shell changes ~ to /home/learner first.
A file name with no /, such as notes.txt, is also a relative path. That is why it is looked for in the current directory.
The meaning changes with the current directory
A relative path gives a different result in a different directory. An absolute path does not:
| Current directory | cat shop/prices.txt reads |
|---|---|
/home/learner | /home/learner/shop/prices.txt |
/home/learner/shop | /home/learner/shop/shop/prices.txt (it does not exist) |
/etc | /etc/shop/prices.txt (it does not exist) |
Use a relative path when the item is near the current directory. Use an absolute path when you write a path for someone else, or when you are not sure where you are.
. is the current directory
. is a name for the current directory. ./notes.txt and notes.txt are the same path. This is the . that tree writes on its first line.