File Systems: The Interface

a path name

A path name is the address of a file, written as a route through the directory tree, just as a postal address routes from country down to street and house number. It is a string of directory names separated by a delimiter (a slash on Unix-like systems, a backslash on Windows), ending in the file you actually want. Reading a path tells you exactly how to walk the tree to reach the file.

There are two kinds, and the difference is where the route starts. An absolute path starts from the root and spells out every step, so it begins with the root marker, for example /home/alice/notes/today.txt. It means the same file no matter who runs it or where they are, because it is anchored to the top of the tree. A relative path instead starts from your current working directory and gives only the remaining steps from there, for example notes/today.txt, which means "today.txt inside notes inside wherever I am right now." Relative paths use the special names dot (the current directory) and double-dot (the parent) to move around, so ../shared/data.txt means "go up one level, then into shared, then to data.txt." The OS resolves a path by reading each directory in turn, following the matching entry to the next level until it reaches the target.

Why it matters: paths are the everyday language for naming files in commands, scripts, and programs, and choosing absolute versus relative has real consequences. Absolute paths are robust but rigid; relative paths are short and portable but depend on where you currently are, the same relative path can point at different files depending on your current working directory. A frequent bug is a script using a relative path that works when run from one folder and mysteriously fails from another.

Standing in /home/alice, the relative path notes/today.txt and the absolute path /home/alice/notes/today.txt name the same file. But move to /tmp and notes/today.txt now refers to something else (or nothing), while the absolute path still points at Alice's file.

Absolute paths start at the root; relative paths start at your current directory.

A relative path's meaning depends entirely on the current working directory, the same string can name different files from different places. Absolute paths avoid this but are tied to the exact tree layout.

Also called
pathpathname路徑