Linux File System Structure (with FHS) for Beginners
In this short article I will explain the FHS aka FileSystem Hierarchy Standard in Linux Systems

By Omkaram - Mar 03rd, 2024


Now before you try to understand the Linux FHS, you must understand what a filesystem is.


What is a File System

In the world of Linux, a file system can be many things depending upon the context in which it is being used. There is no standard definition to it.

In broad usage the words File System can be two things

  1. The Root / Directory and its sub-directories
  2. The data storage and management format such as ZFS, BTRFS, EXT3, EXT4

On a high level, a FileSystem (FS) is a logical way of organising files in directories using metadata such as file length, file type, inodes, block and sectors the files belongs to etc.

Imagine it this way. If you click on some directory say /etc in your machine, how does the system know what files and folders /etc contain and where in the HDD the /etc begin? Such information is neatly gathered as metadata and that metadata is held in the harddrive in the form of inodes which are spread across the drive and the inodes uses specific storage format such as EXT4, BTRFS etc. So EXT4 is not a physical place on the drive. Its a storage format. You can compare it to file extensions like .mp4, .jpeg etc

There are several concepts like Sectors, Super Block, Blocks, Block Groups, Inode Tables, Inodes and Data Blocks which form the complete FileSystem and that's lesson for some other day. Today, we will focus on the File System Directory Structure.

When it comes to the Linux / root directory structure in the beginning days, several Unix and Unix like operation system created their own way of structuring the Linux File System. Over the years this has become a real problem and one day the Linux Foundation has come up with a Global Standard which is meant to be followed. This Standard is what we call the Linux Filesystem Hierarchy Standard. Without having such a standard, people were organising files in directories where there aren't supposed to. Now let's see what the FHS entails.

See the below image. This is the directory structure proposed by FHS and most Linux Distros are now maintaining their FS this way

na

Here is a simplified image

Linux Filesystem Hierarchy Standard

And here is how the root directory of my system looks

Linux Filesystem Hierarchy Standard Linux Filesystem Hierarchy Standard

You can see in the above, the layout is pretty clear to understand. What we as a Linux user need to know the most is, what each of directories corresponds to.


What each of these Directories meant to hold?

/bin: This directory now-a-days is symlink (referring to) the /usr/bin directory. This has all the list of essential user executables/binary files such as ls, cat, grep, awk etc. The user do not need Superuser previleges to execute these commands as this is supposed to be read-only according to FHS, but both normal user and system admin user will be using them, and it requires R-W-X permission. App installation into this directory is managed by App Package Managers like Apt, Yum, Pacman etc. There is another place where users can install their own programs and not have conflicts with the programs in /usr/bin directory, that location is /usr/local/bin.


/boot: This directory contains all the necessary files (except configuration files sometimes) required for the system to boot. This is the place where the firmware looks for the bootloader. This is usually placed in the starting sectors of the HDD and often times you will find the InitRamfs along with the Linux kernel file here.


/dev: This directory contains all the devices attached to the machine represented in the form of files.


/etc: The word 'etc' standards for etcetera. This is read-only directory contains all of the user and system configuration files famously, systemd, initd, network mangaer, apt, dpkg, apache2, gnome etc. By far in all the linux directories this is the most important one from a user standpoint.


/home: This directory is the home for all system users. Each user has his/her own home sub-directory with their name in it. For example my home directory is /home/omkaram. This directory is important too because, when you are planning to switch distros but would like to retain the existing setup such as your look and feel of the desktop and anything you ever customised, then having a backup of this directory is must. All the other popular directories like Dotfiles, Pictures, Videos, Music etc are found here.


/lib, /lib32, /lib64: These are the directories which contains the most essential files such as libraries, shared libraies for glibc, gnu core lib etc are found. This is the heart of your system and the kernel utilises these files to operate. Without these files, your system won't work. Also, most users (even power users) would never need to directly interact with these ones.


/media and /mnt: Media and Mnt are two places where you can find your external storages attached. /media in particular is the place where the system automatically attaches the drives and mounts the partitions without any user intervention. Whereas /mnt is a location to mount things manually. When you are doing LinuxFromScratch, then the /mnt is the place where you put your files and compile first.


/opt: This is an important directory from any users perspective. This is the place where you as a user need to manually install any unbundled or packaged non-self, independent third party software. Examples could be /opt/MuleSoft, or /opt/Chrome etc. And their respective configuration files are stored in /etc/opt/app-name, and logs in /var/opt/app-name

Note: The word 'bundled' means - any software which comes with the operating system.


/proc Anything and everything which is running (apps) on your system are maintained as files. All the apps are running as processes and each process gets a PID (process id). The /proc is the place where all of your processes live as files. This folder will be cleared once the system shuts down. Delete those files and your processes die. Try not to delete /proc/1/.


/root: If you understood what /home is meant for a user, then /root is like the /home for root user. Unlike /home which can host many different users, /root do not host a sub-directory for each root user. Instead /root is the home for all root users. Do not get confused /root with / aka "the root". People use this interchangebly, but that is wrong as hell.


/run: There is nothing special about this directory, but this is a place where all the runtime volatile (temporary) files for system daemons are stored. For example systemd, udev, cups, dbus, mysqld. This folder will be cleared once the system shuts down.


/sbin: Like /bin, the /sbin is used for activities to be performed by the system admin. You need Superuser previleges to run any binary file under this. This directory holds the commands needed to boot the system. Now-a-days this directory is a symlink to /usr/sbin/


/usr/local: Firstly, in the /usr, the word 'usr' does not mean unix-system-resources, user-system-resources or whatever. 'usr' stands for 'user', that it. Nothing special. Now, anything which does not come with the operating system, that which is manually installed, but requires self-compilation instead of a packaged pre-compiled application will go in here. Say you have to perform operation like ./configure, make and make install, then this is the location to do that. All the essential user binaries go into /usr/local/bin and all the locally installed programs for system administration goes into /usr/local/sbin.

In the past, /bin, /sbin were not symlinked to /usr/bin and /usr/sbin. For the system to successfully boot the programs in /bin and /sbin are needed before /usr was mounted. This means the /usr/bin and /usr/sbin were non-essential programs for the boot process. But now things have changed. These days instead of relying on /sbin and /bin for boot process, the same programs are mainted as copies in the InitRamfs. Once the bootloader loads the Kernel file and the InitRamdfs files into the memory (RAM), the kernel uses the /bin /sbin /etc /lib programs needed before the / aka root is mounted for use. This way of doing things has led to unification of /bin with /usr/bin, and /sbin with /usr/sbin, and finally one day they became symlinks instead.


/sys and /srv: The /srv is relatively a new directory which has been introduced to the linux systems. It is meant to be a place for data files of a particular service like ftp, www etc. Whereas the /sys is actually a virtual file system that contains virtual files (drivers etc) which the Linux kernel can interface with. Think it like /dev, /proc and /run but only dedicated for the Linux Kernel. The /sys is relatively old, now-a-days it is slowly being replaced by /dev I suppose (but not sure). A bit of advice to you as a linux user is to never touch the files in these directories.


/tmp:If you need a temporary folder to do stuff, then this is provided for such use.


/var: This is an important directory which goes synonymous with "log files". Its a place where the system application stores the log files. The applications can be system related or user related. Famous file are /var/log/wtmp, /var/log/btmp, /var/www/html, /var/cache/apt, /var/backups/dpkg.status.0


Thanks for reading till the end. The FHS is just a standard and there is nothing much to explain about it than what the FHS proposed file Hierarchy is and the directories intended use. If you are using the directories like they are meant to be used, then your system is FHS complaint. But, by the end of the day, as the owner of your machines, you can do whatever you want to do with it and no one can stop you from doing anything different.

If you liked what you read so far, then there is a good article I wrote on the Linux Boot Process aimed at the beginners. Here is the Link