Linux Filesystem Hierarchy

By | 2019-04-14

Unlike Windows, Linux doesn’t use drive letters to distinguish storage devices. It uses a single hierarchy starting at /. Storage devices are associated with directories within this hierarchy through a process called mounting. I’ll cover mounting and mount points more later on. The term Linux filesystem hierarchy refers to this tree structure.

The Root Directory

Every Linux filesystem hierarchy begins at /, often called the root directory. As you will see later, there is also a /root. You will usually be able to tell by the context which one people are talking about. Other than it being at the top of the hierarchy, there is nothing special about it. You can create files and directories in it just like you can any other directory.

FHS

The Linux Foundation maintains a standard known as the Filesystem Hierarchy Standard (FHS). The FHS standard specifies the names and contents of directories on a Linux system.

The Linux Foundation maintains the FHS so Linux users and developers know where to find and put files. Since this is an introductory guide, I won’t cover it exhaustively. My goal is to help you find your way around a Linux filesystem.

The FHS Directories

The following table briefly describes some of the directories in the FHS. If you need more information or an exhaustive reference, consult the FHS specification. The Wikipedia page covering the FHS has table that is a good reference.

Directory Description
/bin /bin is short for binary. It used to store important programs for users and systems administrators. You will find programs used to view and edit text files and directories. Some distributions symbolically link /bin to /usr/bin
/boot Files used to boot the operating system. E.g. boot loader configuration files, the file storing the kernel image, and “archive” files used to store any drivers or programs needed to minimally boot the system.
/dev Linux programs use special files called device files to interact with hardware. The programs that format storage devices, such as USB flash drives, use them to read from and write to hardware.
/etc In /etc you will find OS and application configuration files. You will also find shell scripts used by the initialization system to start and stop various services.
/lib Shared libraries used by essential programs are found here. Shared libraries are files with code that is shared by multiple programs. Some systems symbolically link /lib to /usr/lib.
/home This is the standard place for users’ home directories. They are the equivalent of Windows profiles.
/opt The /opt directory is typically used for software that is not part of the operating system. This is typically closed-source software software.
/proc /proc is used for special files that can be used to retrieve information about running processes This is a virtual filesystem– the files and directories found here only exist in memory.
/root /root is usually the root user’s home directory. According to the FHS, this isn’t required. Most systems I have worked with follow this convention. I sometimes see root‘s home directory set to /. I don’t like doing this because it clutters / up.
/sbin In /sbin you will find programs used to maintain and troubleshoot filesystems, partitions, logical volumes, and the boot loader. You will also find programs used to configure network interfaces. Some systems symbolically link /sbin to /usr/sbin.
/tmp /tmp is short for temporary. Sometimes programs and users need to create files that are only needed for short amounts of time. The /tmp directory is usually cleared of its contents when the system is rebooted.
/usr The official FHS definition is shareable and read-only data. You will typically find several subdirectories containing things like program executables, documentation, and code libraries.
/usr/bin As with /bin, /usr/bin is short for binary. This is where most user programs are located. Here you will find everything from compilers to web browsers.
/usr/lib This is where shared libraries that aren’t necessary for booting or administering the system are found. For example, libraries with code for tasks such as resizing images or decoding video files are found here.
/usr/sbin Here you will find programs for system administration that aren’t required for repairing filesystem or boot loader problems. You will commonly find the executables for network servers in here.
/usr/share Machine independent data. Usually you will find things like documentation, locale definitions, desktop icon image files, etc.
/var This is where files that change a lot should go. In fact, /var is short for variable. In subdirectories of /var, you will typically find log files, cache files, and database files.
/var/db As you can imagine, db is short for database. Subdirectories of /var/db are the default locations that some database management systems use to store their data files. PostgreSQL and MariaDB are examples of database applications.
/var/log Linux based operating systems and their applications often keep log files for security and troubleshooting purposes. You will usually find them here.

Mounting and Mount Points

Any storage device, virtual filesystem, or network share can be associated with a directory anywhere on the Linux filesystem hierarchy. Whether doing so is a good idea is another matter. This proces is called mounting. When you have associated a filesystem with a directory, you say it is mounted on that directory. For example, if you wanted to access an external USB drive at the directory /backup, you would mount it to /backup. You would then say your USB drive is mounted on /backup. The /backup directory is now a mount point. While the drive is mounted on /backup, anything written to or read from /backup would happen on that USB drive. Consider the diagram:

Diagram Showing Mount Points

Diagram Showing Mount Points

Anything read from or written to / or one of its subdirectories will happen on the disk labeled internal disk 1. There is an exception. If one of the subdirectories is a mount point, the input or output will be applied to the device mounted on that directory.

A file create in /var/db/mariadb would be written to internal disk 2.

If you want to write a file to internal disk 3, you would have to put it in /var/db/postgres or one of its subdirectories.

The /opt directory is the mount point for a directory shared by another computer over a network. This means anything written to /opt will be written to the storage device or devices on that computer.

Mounting and Existing Data

Suppose you have the following directories and /stuff is not a mount point:

/stuff/documents
/stuff/pictures
/stuff/videos

If you were to mount a device on /stuff, the directories listed above would become invisible until you unmount the device. You would instead see the files and directories on that device.

Mount Options

How a filesystem behaves depends on its mount options. A common mount option is read-only. As you can imagine, a filesystem mounted read-only can be read from but not written to. Another common option is nosuid, which helps prevent a nefarious unprivileged user from gaining administrative access. The options available depend on the type of physical storage and filesystem type.

References and Other Sources