Linux File System Structure Explained | Chronicles of Everything #03
When learning how to switch to Linux, one of the first things you are confronted with is the way in which Linux structures its file system. Not talking about file system as in NTFS, FAT32 or Ext4, rather the way in which files and folders are presented to you.
Now, for this article to make sense to you, I am assuming you have at least used a Windows device already. Although annoyingly many younger people nowadays seem to only grow up using mobiles and tablets. Therefore never actually seeing where and how files are stored.
Windows
The basics of how Windows structures its files and folders are fairly simple.
Your connected drives or partitions are the starting off point, these drives just exist on the computer sitting next to each other and don’t have any relation to each other. Once you look into each drive you can see it contains folders and files. Folders basically being a container with a name, with other folders and files sitting inside. A file being stored data, like an image, text, video or just data.
This is a fairly simple model to understand. Since it follows a fairly easy model for something you would do in the real world as well. (Cabinet->Folder of vacation 2013->Printed Picture).
Linux
In Linux, and broadly speaking in the entire Unix-like world, the file system is more complex than this. The first thing to understand is, unlike in Windows, we only have “one structure”. Instead of each drive having its own structure. I think this is the biggest difference and the rest comes easily once you get this. Another small difference is a forward slash is used like / instead of a backwards slash.
Quick info before the big thing begins: Folders are also sometimes called Directories, another differentiator is that a file doesn’t need to have an ending. Like image.png could just be called image on a Unix-like system.
Every Linux file system starts of with a simple /
also known as the root of the file system. (Similar to how C:\ is the root of your C drive on Windows. But for everything. /
is the start of everything and it contains everything. If you look at /
in a file browser or in the terminal you will see various folders and files. Let us go through each folder and see what they do. Unlike Windows, most folders have a very specific usecase.
/home
is probably the most important one for users. If your username is Silas for example, the system will create a folder in /home/Silas
. This is where all the user files go. In Windows any program can and will put some files anywhere you want. Making it in my opinion hard to tell if you made a backup of all of your user files. Because unless you know where it is, you may have forgotten some video game save files, because they were in some special hidden folder. On Linux, everything will be in /home/Silas/.minecraft
for example. Which usually gets shown to you as ~/.minecraft
. ~ being the home folder of whoever is logged in. Instead of like on Windows where it stores game files in C:\Users\AppData\Roaming\.minecraft
Which, yes is in the Users folder. But in something not even related to you. So, I preferr knowing everything I care about is inside ~
/root
this is the home folder for the root user. Root is also what is called the Admin user in the Linux world.
/boot
Contains files needed for the bootloader. The program that actually starts up before your OS does, to let you choose between different systems or boot settings.
/sys
Contains files with information about drivers and devices.
/bin
traditionally this contains binary files, programs (provided by the system) meant for the user to run. For example commands you can run in the terminal like ls
or chmod
. Although this has been changing for a while to the /usr/bin folder. In fact, on my main computer running openSUSE Tumbleweed, /bin
is actually just a link to /usr/bin
. We will get to that in a bit.
/sbin
contains binary files and programs that the OS itself, or an Administrator needs. Also is in the process of being replaced by /usr/sbin
.
/lib
contains libraries. Or simply explained it containes files used by multiple programs. So you only have to install a library once, and every other program knows to look in /lib
to find a library it needs. Also now often replaced by /usr/lib
/etc
contains files relating to configuration of the system. Things like your screen resolution for example will be stored in a file here.
/tmp
is meant for temporary files. Everything you or a program only needs for a short time can be put here. After a shutdown, everything in /tmp
gets deleted. Some Linux distros don't do this though.
/usr
used to be for programs installed by the user only, but as mentioned before is now being used for programs and libraries in general. As well as other program related files. All of the files in here are supposed to be Read Only. This change of having moved the bin, sbin and lib folders into usr comes from a standard called the Filesystem Hierarchy Standard. Which had its goal to make the file system the same on most distributions. Because it used to be quite different for each Linux distro.
/opt
is for optional add ons for your system.
/var
contains variable files. Like for example program and system log files. Where usually one file is created and then changed all the time.
/srv
contains files used by services running on the system.
Up to now this has been fairly understandable. Just some files neatly organised. But now we get into the harder to get territory. Because in Linux/Unix, everything is a file.
So far we have been talking about files in terms of an image, text, a video, a program and things like that. But in a Unix-like system almost everything is a file. A file can also be other things.
/proc
contains folders and files. But the folders represent a process, essentially a program or parts of a program. So for example if you want to know if your video editor is crashed, and you know the ID of the process is 12. You could go into /proc/12/status
to see how it is doing. Proc also has other more general files in it like /proc/cpuinfo
to show you info about your CPU.
That was already weird I guess, let's continue.
/dev
contains devices. Everything is a file, remember? So for example if you have a sata disk installed it would create the file /dev/sdb
. sd meaning sata drive, b meaning the second one. If you have another sata drive installed before. If the drive has multiple partitions it will make a file for each one with a number like /dev/sdb2
. Each type of device has its own naming category. Webcam or a capture card might show up as /dev/video1
/mnt
is a folder for mounting file systems to. Usually manually. For example my NAS at home, I have configured to be mounted to /mnt/truenas
. So the NAS shows up as a folder in /mnt
/media
is where the system can automatically mount removable media. Like a CD or SD Card. Although this can vary depending on how your system is configured.
/run
is for runtime data. It's fairly similar to /tmp
as in it is for files the system needs until it shuts down. My example for this is on my system, When i plug in an SD card, it will automatically mount the SD card to /run/media/...
.
There are many really good parts of the ideas that Unix had and Linux now does. Everything being a file has been criticised quite a bit. Especially by people who develop drivers for new USB devices. Since this was meant for easier handling of hardware (reading and writing to hardware like a file). This has kind of become annoying and crap. But it is how it is. For the most part i think this is very well structured and once you get it, it does make sense. I encourage you to bookmark this article to come back and see if you need to look something up.