Two ways to name the same file
If a path string starts with a slash, the walk begins at the root directory, and the path is absolute. /etc/hosts always means the same file, no matter where you are. If the string does not start with a slash, the walk begins at the current working directory, and the path is relative. hosts means the entry called hosts in the directory where you are standing. The same file can be named both ways, and the kernel resolves both names to one inode.
Absolute paths are unambiguous, which makes them the right choice in scripts, in cron jobs, and in configuration files, where the working directory is not under your control. Relative paths are short and comfortable when you work interactively inside one project. Neither form is better in general. The question to ask is where the walk starts, and whether you know that place with certainty.
The dot entries are real directory entries
Every directory on Linux contains two entries that the filesystem maintains. The name . refers to the directory itself, and .. refers to its parent. They are ordinary entries that point at real inodes, which is why ls -a shows them and why the root directory's .. points back at the root. The same structure is why hard links to directories are forbidden in ordinary use: a loop of directory links would make the tree impossible to walk.
Using them, you can write a path that goes up and back down. /usr/bin/../lib names the same directory as /usr/lib, provided that /usr/bin is an ordinary directory. The kernel walks the path from left to right. Each .. means the parent of the directory the walk has just reached, which is not always the same as chopping the last component off the text. That difference matters once symbolic links are involved, and a later part of this unit shows why.
The shell's shortcuts
Bash expands a leading tilde before it runs the command. A bare ~ becomes the value of $HOME, and ~name becomes the home directory of another account. A single hyphen given to cd means the directory you were in before, which bash keeps in the variable OLDPWD. These expansions happen in the shell, so the program receives the expanded text. That is why echo ~ prints a path, while a program that receives the literal characters tilde would look for a file with that exact name.
echo ~
cd /etc
cd -
pwdThe last line prints the directory you returned to, and cd - also prints it, because it is useful to see where you landed. This is the same mechanism that makes a pair of cd commands act like a back button.
Normalizing a path with realpath
A path that contains ., .., or repeated slashes can be simplified to a canonical form. The GNU tool realpath resolves a path to an absolute name, and it follows symbolic links along the way. The -m option lets it accept names that do not exist yet, which is useful when you are planning a directory layout. Because realpath does the work against the real filesystem, the result is the physical location of the file.
mkdir -p /workspace/demo/a/b
realpath -m /workspace/demo/./a/../a/b//The output is /workspace/demo/a/b. Every dot segment was removed and the doubled slash was collapsed. Notice that the command name and the option both come before the path, which is the usual shape for GNU tools: options first, then operands.
Relative paths in scripts and cron jobs
A relative path is resolved against the working directory of the process that opens the file, not against the directory of the script that contains the path. A script that writes to output/result.txt works when you run it from the project root and creates a surprising directory somewhere else when a scheduler starts it from its own working directory. Cron, systemd units, and editors often start programs in a directory that you did not expect, so a script should either use absolute paths or change into a known location before it does anything else.
A practical pattern is to find the directory that holds the script and build every path from it. Bash can do this with a few lines at the top of the file, and the rest of the script then refers to $base/output/result.txt. The result is longer than a bare relative name, but it behaves the same wherever the script starts. Avoid the other extreme of hardcoding a home directory, because the script then fails on any other account or machine.
Relative paths are still the right tool at a prompt. When you type a command, the working directory is the one you can see in your prompt, and a short name is what you want. The discipline is to know which situation you are in. Interactive work can be relative. Anything that runs unattended should state its starting point.
When simplifying the text gives the wrong answer
Suppose a is a symbolic link to /srv/data/deep/dir. The text a/../b looks like it names b in the current directory. The kernel does not see it that way. It follows the link first, so a/.. is the parent of /srv/data/deep/dir, which is /srv/data/deep. The result is /srv/data/deep/b. Removing the a/.. from the text first would produce a different and wrong file.
This is why bash offers a logical and a physical view of the current directory, and why the next unit covers both. The rule of thumb is simple. When symbolic links are involved, trust realpath or the physical directory from pwd -P, not a string that you have simplified by hand.
Shell tab completion and many programs print paths relative to where you started. A relative path is only meaningful together with the current directory of the process that reads it. If you copy a relative path into a script that runs from somewhere else, it may name a different file.
Practice in the lab
The lab home directory is /workspace. Make a few nested directories, then name the same one three ways: with a relative path, with an absolute path, and with a path that goes up and back down. Use realpath to confirm they agree. The guided lab for this unit asks you to save the canonical result in a file so the checker can read it.