Absolute vs Relative Path
A path is the address of a file. An absolute path is the full address, starting from the very top of the system, like a full street address with city and country. A relative path is directions from where you are standing right now, like “two doors down.” Both get you to the same file; which one works depends on where you start.
Absolute paths begin with / and resolve the same way from anywhere: /etc/ssh/sshd_config, /home/george/notes/install-log.md.
Relative paths do not begin with / and resolve against the current working directory (pwd): notes/install-log.md, ../shared, ./script.sh.
Special entries:
.: the current directory..: the parent directory~: your home directory (expanded by the shell, so it is really absolute)-: withcd, the previous directory
Why ./script.sh? The shell looks for bare command names only in $PATH directories, not the current one. ./ says “this directory” explicitly.
Where each is the right choice:
- Absolute: cron jobs, systemd units, fstab, config files, and scripts that may run from any directory
- Relative: interactive work and project files inside a repo, so the project still works when moved
Paths are case-sensitive on Linux. Notes and notes are different directories. Spaces must be quoted or escaped: "My Files" or My\ Files.
Moving around with both kinds of path
$ pwd
/home/george
$ cd notes # relative
$ pwd
/home/george/notes
$ cd ../Downloads # up one, then into Downloads
$ cd /var/log # absolute
$ cd - # back to the previous directory
/home/george/Downloads
# Turn a relative path into an absolute one
$ realpath ../notes/install-log.md
/home/george/notes/install-log.md Many “it works when I run it, but not from cron” bugs are relative paths. Cron and systemd start scripts with a different working directory, so a script that reads config.yml fails. Use absolute paths in anything automated, or have the script cd to its own directory first. In interactive work, tab completion makes typing either kind quick and catches typos before you press Enter.