Linux Directory Structure: What It Is And How It Works

The Linux Filesystem Has No Drive Letters... Here's Why

Root directory on Ubuntu Linux desktop

If you're trying out Linux for the first time, one of the areas in which it differs from Windows (and to a lesser extent macOS) is its directory (folder) structure. This refers to how the various directories are arranged as well as their purpose. In this article we'll explore some of the important directories and show their uses.

Note we're not talking here about the filesystem, which is the technical pattern used to store data on disk. The directory structure we'll review will apply to most Linux distributions regardless of what filesystem they use.

Differences Between Windows and Linux Filesystem Structures

To understand at a high level why these operating systems are structured the way they are, it's useful to understand their history. When Windows-based PCs grew in popularity, files were stored first on floppy drives. They were labeled A: and B: so it was easy to distinguish them. When hard disk drives received the C: drive label we know today, additional drive types, such as CD-ROMs or USB-based drives, were assigned subsequent letters.

In contrast, Linux descends from Unix, an operating system developed by AT&T in the 1970s. At this time, the "computers" people used were primarily just terminals. They displayed text-based information on screen and provided a keyboard for input. But the heavy lifting was performed by a "system" that could have been made up of multiple physical machines: one for processing, one for storage, and another that provided a specific application. These were all connected over the network, while to users they appeared as a single system.

With all this in mind, let's look at some of the directories in Linux systems. If you'd like to see this for yourself, you can easily create a live Linux system on a CD-ROM or USB drive that won't erase your computers existing operating system.

A rule of thumb for Linux filesystems is that the root, or top-most level of the filesystem, doesn't contain any stray files. In most systems it might contain the kernel (vmlinuz in the figures), or the heart of the OS, and possibly a ramdisk (initrd) containing data the system needs to get up and running. But aside from these, it should only contain the directories we'll discuss in the below sections (among others).

The /bin Directory

One of the first directories you'll encounter is /bin. This stands for binaries, and it contains "standard" operating system programs. Here, "standard" refers to basic utilities needed to use the OS in any meaningful way. Examples of programs you'll find here are as follows:

  • cd, or change directory, which operates much like its Windows counterpart
  • File management commands like cp and mv (copy and move files, respectively).
  • File permissions utilities such as chmod (which changes file permissions) and chown (which changes the owners of those files).
  • The programs that provide your computing environment, such as login (which provides your ability to sign into the machine) and bash, the main command prompt, also referred to as a shell.

When you first install a Linux distribution, these programs will be installed as part of the most basic installation, and in many cases can't be removed.

The /boot Directory

The Boot Directory Contains Boot-Related Files, Ramdisks, and Kernels

Next up alphabetically is /boot, which contains files the system uses when it starts up. It contains copies of the Linux kernel (the one you see in the root of the filesystem is probably a link to one of these), the initrd, or initial ramdisk for the system, and other related data.

The most important thing you should know about this directory is not to mess around in it. The stuff here is handled when your package manager installs and upgrades kernels. If you delete one of the files needed by your current kernel (or the kernel itself), you may end up with an unbootable system.

The /cdrom Directory

This is something of a legacy directory. It was introduced as a place where the CD could be mounted, or connected, and its contents accessed. At that time it was common to require root, or administrator, rights to mount the CD-ROM. But nowadays it's more common to find CD-ROMs auto-mounted in the /media directory, which we'll discuss a bit later.

In the same way, there used to be a /floppy directory. You can see how these came about to make users, who were likely used to Windows, a little more comfortable. But as floppy drives aren't standard components on modern PCs, you may not even see this directory any more. Ubuntu doesn't have it, for example. If your machine does have a floppy drive, you'll likely find it automatically mounted in /media.

The /dev Directory

Every Physical Device in a Linux System is Represented as a File in /dev

One unique aspect of Unix-based operating systems is that "everything is a file." Which is to say that there are files that represent everything in the system: running processes, RAM, and yes, storage devices. This means when you're copying some data to your hard drive, Linux is actually writing data to a file like /dev/nvme0n1p3, which represents the third partition of your SSD.

The /dev directory is where all of these special files are kept. If you browse through it, you'll see files for just about every device you can imagine, from the aforementioned NVMe-based solid state drives to hard disks on the SATA (e.g. /dev/sd*) or older IDE (/dev/hd*) connections, as well older ones like the PS/2 mouse port (/dev/psaux). Asterisks here are wildcards; for example, on a SATA-based set-up, your first hard disk will be "/dev/sda," and that disk's first partition will be "/dev/sda1." Its second partition is "/dev/sda2," while the second disk's first partition is "/dev/sdb1." While it's not important for them to be in any particular order, it's important to know which is which. This will prevent you from overwriting your Windows partition when you meant to install Linux to a new, blank drive you install, for example.

The /etc Directory

The /etc directory's name, as a place to put any "extra," or "et cetera" files, belies its importance. In practice, those tend to be the configuration files that control the entire system. There are two main types of files that reside here:

  • Global configuration files, e.g. the /dev/fstab file used to mount all those hefty drives you have described in the /dev directory above.
  • So-called "skeleton" files, which are used as defaults for user-level settings. An example of this is the /etc/profile file, which contains settings for the Bash shell. When a new user is created, this is copied to their account as .profile, and serves as the starting point until the user customizes it.

Many of the sub-directories in /etc are named for the applications they control, so it's easy to browse around here to try to find the file you need to fix an app when it's acting up.

The /home and /root Directories

Windows users by now have gotten used to the C:\Users directory, which is where the documents and settings for each user of the system reside. Since Unix was from the start a multi-user system, this concept has existed for many (many!) years in Linux as the /home directory. Each user will have a sub-directory here that contains all their data, in my case /home/aaron. In fact, a "normal," non-administrator user doesn't even have permission to store files elsewhere.

The /root directory is the equivalent user folder for the system administrator, or "root" user. It only differs from /home in its location (to keep potentially sensitive files far away from normal users) and ownership.

The /lib Directories

As you're probably using a 64-bit operation system, you probably have a couple of directories here: /lib, /lib32, and /lib64. These contain software libraries the kernel uses for its most basic operations. Those that don't contain code specific to the version the processor are in the /lib folder. Those that are version specific are in the /lib32 (32-bit) or /lib64 (64-bit) directories as appropriate.

The /media and /mnt Directories

Removable Media Get Mounted Under a User's Name in the /media Directory

Some years back, if you had removable media like a USB thumb drive, before you could use it you'd need to assign it a /dev entry and create a place for in the /mnt (mount) directory. Fortunately, in more modern versions, Linux distributions are pre-configured to configure removable media automatically, as you'd expect from working with macOS or Windows. In fact, depending on the user logged in when the device is connected, the system will create a user-specific directory, mount the device, and make sure that user has the correct permissions to access it. The difference nowadays is that the /media directory is generally used for this purpose. For example, inserting a thumb drive with the label "FREEDOS" on my Linux machine will mount it at /media/aaron/FREEDOS.

The /opt Directory

There was a time not so long ago that you couldn't just hop over to a site and download an Ubuntu package for your favorite application. You'd install many of these programs by running a custom installer, simply unpacking them from their archive, or even building them from their source code. These installation methods weren't unified, and sometimes it was difficult to track where the files had gone and how to uninstall them. The /opt directory was introduced for this "optional" software, to make sure the rest of your system was unaffected in the event your un/install went awry.

While more developers today are making their apps available in standard package formats (.RPM and .DEB), some of these will still install to /opt. Whether this directory still makes sense is a subject of some debate in the Linux community, but for now, it remains. And some software will still install there, even from standard .DEB packages. Notably, Google Chrome will install there on Ubuntu-based systems.

The /sbin Directory

The /sbin directory, like the aforementioned /bin, holds binary utilities. However, this directory is limited to system binaries, and as such only the root user can run its contents. It includes tools like adduser (used to create new user accounts), insmod (used to load hardware drivers to the kernel), and poweroff (which, as the name suggests, shuts the computer down).

If you're wondering at that last one, you've got a sharp eye. No, you don't need to be a "super-user" just to turn your Linux system off. On server systems this is the case. But on the desktop, your preferred environment (such as KDE, GNOME, or XFCE) is set up so it can execute that specific command as though it were a root user.

The /usr Directory

The /usr Directory Looks Simple Enough, Until You Start to Dive Into It

Wait, we already looked at the user directories, right? Let it never be said that Linux didn't have its own peculiarities. While user data is stored in the /home folders, /usr and its sub-directories hold the majority of installed software for the system. Here "installed" means whatever is available outside the basic utilities in /bin. For example, it will contain:

  • Applications such as desktop environments, the LibreOffice office suite, the GIMP image editor, and the Firefox web browser.
  • Source code for things like the Linux kernel and hardware drivers.
  • Ancillary files, such as artwork or documentation, for all the above.

The /usr directory stores these in its own hierarchy. It contains bin, lib, and sbin sub-directories, which operate just like their higher-up brethren, but contain only things related to software you installed. The /usr/share directory contains assets like icon sets (visible in the below figure), as well as documentation files.

The /usr/share Directory Is Huge, with Many Sub-Directories for Various Applications

Finally, the original intention of the /usr/local branch was similar to /opt — user-installed, "optional" software. Part of the debate is which of these two should remain. One point in favor of /usr/local is that most software installed from source code is configured to place itself here.

The /var Directory

The /var Directory Holds Working Data for Running Apps and Processes

The last of the "main" Linux directories, the /var directory contains system data. This means data generated by the system's operations, such as log files (/var/log), caches (/var/cache), and backups (/var/backup). And since Unix (and therefore Linux also) was originally a multi-user system, holding areas for server applications such as email (/var/mail for mailboxes and /var/spool/postfix for outgoing mail) and network printing (/var/spool/cups) are also here.

Other Directories

  • /lost+found: There are times when little bits of data get misplaced in the system. If you know you lost something you can check here, although the contents won't be easily identifiable. You'll need to manually inspect their contents to figure out what they are.
  • /proc: As previously mentioned, everything in Linux can be represented with a file. The /proc directory contains files associated with processes, and you can view them to examine what they're doing (but don't edit these).
  • /run: This is a runtime directory where processes store some temporary data they need, most often as the system is starting up. Like other system-level directories, don't muck about in here.
  • /snap: Ubuntu's Snap is a package format that wraps up an application, and everything that app needs, in a container that's isolated from the rest of your system. The /snap directory is where these containers are mounted to the system, in a similar way as storage devices.
  • /srv: This directory is where the files served by daemon programs are stored. For example, static web pages served by the Apache web server might be stored in the /srv/www sub-directory.
  • /sys: In keeping with the "everything is a file" mantra, /sys and its contents represent settings and information from the Linux kernel, in the same way /proc does for running processes.
  • /tmp: If you guessed by the name that this directory holds temporary data, you're correct. System-level programs will store info here temporarily.