Hierarchical file systems as trees

Windows and Unix-based operating systems (including OS X, Linux, etc.) use hierarchical file systems, in which a file or directory is specified by its path—that is, its location with regard to some point of reference in the file system’s hierarchy. Usually, the point of reference is the current working directory.

In general, we can think of these hierarchical file systems as trees, where a number of branches (directories) spread out from the root (the root directory), with those branches splitting into additional branches (subdirectories), and so on. With this view of a file system, we can think of files as the leaves of the tree.

Path components and the separator character

A file or directory path consists of 1 or more components. Each component specifies the next branch of the tree to follow in locating the file or directory; the last component is the referred-to file or directory. Between the components we have a separator character—/ on Unix-based systems, and \ on Windows. If the first component in the path is the separator character, then the path begins at the root of the tree. So the path /opt/android/sdk refers to the sdk subdirectory of the android directory, which is itself a subdirectory of the opt directory, which is a subdirectory of the root directory. (In some cases, when specifying a directory path, we end the path with the separator character—e.g. /opt/android/sdk/.)

In Windows, we also have the possibility of multiple trees, representing separate logical disk drives; in that case, the path may begin with a drive letter, followed by a colon, followed by the rest of the path specification. For example, C:\android\sdk refers to the sdk directory, which is a subdirectory of the android directory, which is itself a subdirectory of the root directory of the C drive. Just as there is a current working directory, there is also a current drive in Windows; if the drive is not specified in the path, then the current drive is assumed.

Absolute and relative paths

In Unix-based systems, a path that begins with the / character is an absolute path; in other words, the path refers to a location that is independent of the current working directory. A path that doesn’t begin with / is a relative path, since it is specified relative to some possibly changing point of reference. If the first component is ~, the rest of the path is interpreted relative to the current user’s home directory; otherwise it is generally interpreted relative to the current working directory.

In Windows, a path that begins with a letter, followed by a colon and then \ (e.g. C:\) is an absolute path. A path that begins with \ is sometimes referred to as an absolute path, but it is actually a drive-relative path, since it is relative to the root directory of the current drive. A path that begins with a letter, then a colon, and something other than a \ is a relative path, since it is relative to the current working directory of the specified drive. Finally, a path that begins some other way is a relative path, interpreted relative to the current working directory of the current drive.

Special component identifiers

There are two special identifiers which can be used as path components in Windows as well as Unix-based systems, which have specific meanings:

  • . represents the current directory. If it appears at the start of the path specification, it refers to the current working directory. If it appears as a component in the path, but not at the start, then it simply refers to the current directory at that point in the path specification. For example, in C:\Windows\.\System32, the single period represents the Windows subdirectory of the root directory of the C drive.

  • .. represents the parent of the current directory. If it appears at the start of the path specification, it refers to the parent directory of the current working directory. If it appears as a component in the path, but not at the start, then it refers to the parent directory of the current directory at that point in the path specification. For example, in /usr/local/bin/../lib, the .. component refers to the parent directory of /usr/local/bin—i.e. the /usr/local directory.

(It might seem that there isn’t much point in including the . or .. component in a path specification—especially if it’s not at the start of the path. However, these can be very useful when combining multiple partial paths.)