Files and Directories

In this lesson, we briefly introduce files and directories.

We'll cover the following

Two key abstractions have developed over time in the virtualization of storage.

File

The first is the file. A file is simply a linear array of bytes, each of which you can read or write. Each file has some kind of low-level name, usually a number of some kind. Often, the user is not aware of this name (as we will see). For historical reasons, the low-level name of a file is often referred to as its inode number. We’ll be learning a lot more about inodes in future chapters. For now, just assume that each file has an inode number associated with it.

In most systems, the OS does not know much about the structure of the file, e.g., whether it is a picture, or a text file, or C code. Rather, the responsibility of the file system is simply to store such data persistently on disk and make sure that when you request the data again, you get what you put there in the first place. Doing so is not as simple as it seems!

Directory

The second abstraction is that of a directory. A directory, like a file, also has a low-level name (i.e., an inode number), but its contents are quite specific: it contains a list of (user-readable name, low-level name) pairs. For example, let’s say there is a file with the low-level name “10”, and it is referred to by the user-readable name of “foo”. The directory that “foo” resides in thus would have an entry (“foo”, “10”) that maps the user-readable name to the low-level name. Each entry in a directory refers to either files or other directories. By placing directories within other directories, users are able to build an arbitrary directory tree (or directory hierarchy), under which all files and directories are stored.

The directory hierarchy starts at a root directory (in UNIX-based systems, the root directory is simply referred to as /) and uses some kind of separator to name subsequent sub-directories until the desired file or directory is named. For example, if a user created a directory foo in the root directory /, and then created a file bar.txt in the directory foo, we could refer to the file by its absolute pathname, which in this case would be /foo/bar.txt. See the figure below for a more complex directory tree; valid directories in the example are /, /foo, /bar, /bar/bar, /bar/foo and valid files are /foo/bar.txt and /bar/foo/bar.txt.

Get hands-on with 1200+ tech skills courses.