The Linux file system is very specific and can be hard to understand for newbies on this system.
Where are my files? Why is there so many folders and subfolders? etc.
The goal of this post today, is to help you to get an overview of the file system on a Raspberry Pi (and on any Linux device)
How does the Raspberry Pi file system works?
The Linux file system can be seen like a tree.
The / location is the root, where the trunk begins.
Each sub-folder is a big branch (/home, /var, /etc, …), and these branches also have smaller branches (/home/pi, /var/www, /var/lib, …)
This post idea comes from a survey I made in my emails, so I hope it will help at least the person who asked for it 🙂
We will start by a general overview about the Linux file system (the tree), then list the most useful folders to know, and finally see some common mistakes in the file system usage.
The Raspberry Pi File System
The Linux File system
The first thing to understand is that the Raspberry Pi doesn’t use a specific file system tree, it’s a standard from the Linux Foundation named “Filesystem Hierarchy Standard” or FHS.
So, basically, you’ll find the same hierarchy on any Linux distribution.
For those interested in this, you can find all the information about this standard on the official documentation here
The Linux tree
I already introduced this concept in the introduction, but I want to illustrate it here because it’s important. Most common mistakes comes from a misunderstanding at this point (we’ll see them later).
So, as I told you, you can see the files hierarchy as a tree, with / as the root folder, and each subfolder as a new branch
In this picture, I presented it more like an organizational chart, but you can absolutely revert it if you didn’t understand the tree analogy 🙂
Colored cells are folders, and white are files
At the top there is the root folder: /
Then home and etc are subfolders, two big branches (there are other ones)
pi and user2 are subfolders from home, passwd and hosts are two files in etc
The file location is identified as /etc/passwd for example
So, / and etc because they are the folders where you can find the file, and then you add another / for each subfolder and the file name at the end
Other example: /home/pi/Documents/MyDoc.doc. You have to go through /, home, pi and Documents to find it from the root folder
I hope that this notation is clearer now 🙂
File system format
I also often see this question about the file format used by Raspberry Pi OS, but I don’t think it’s relevant.
In fact, the Raspberry Pi Foundation use a boot partition in a FAT format (good news for people on Windows because you can read and write on it), and a Linux format for the main partition (probably EXT4)
That’s it, you have the information, but you can’t do anything with it, as the format is included in the image file and you can’t control it.
Most useful folders to know
Home is the short version of “home directories”
It contains one subfolder per user (for example /home/pi)
Each user directory contains personal data, like preferences, documents, downloaded files etc.
On Raspberry Pi OS Desktop, you’ll get many subfolders by default in /home/pi:
It’s really close from Windows on this point.
/root is the same thing as /home for the administrator user
Basically, we don’t use it on Raspberry Pi
But, if you log in as root, you’ll go to this folder as your home folder
Etc contains all the configurations files
There are several explanations given for the name, but the one I prefer is “Editable Text Configuration”
There are a few files directly available in /etc, for example:
- /etc/resolv.conf: If you want to change your DNS server
- /etc/crontab: For tasks scheduling (more detail here)
- /etc/sudoers: To manage users with the sudo permission
And for more complex configurations where there is several configuration files, you’ll more frequently find a specific subfolder for them:
- /etc/apache2: If you use an apache as a webserver, this folder contains all the configuration files
- /etc/ssh: same thing for SSH client and server
- /etc/php: same thing for PHP
You understand the concept, if you are looking for a configuration file, it’s probably somewhere under /etc
Var is the abbreviation of “Variable files”
It means files that are modified during a program execution (log files for example)
Here are the most common subfolders you need to know:
- /var/log: contains all the system log files
- /var/www: will be used as soon as you install a web server, it’s the root folder for the web server (public files need to be in /var/www/html)
- /var/lib/mysql: same thing for a MySQL server, it contains all database files. Generally, other database engines also have their files somewhere in /var/lib
- /var/mail: mailboxes files
/usr contains mainly applications files you don’t need to touch (like binaries and libraries)
I generally use /usr/local/bin as the main folder for my scripts because I learned like this, but I don’t know if it’s really a good practice.
Saving your scripts in a folder like /home/pi/scripts is probably a better idea.
/bin is something similar for essential command binaries
There is no subfolder in this one, just binaries like ping, cp/mv/rm or nano
In fact, it’s the only available binaries when you boot in single user mode; any user can use them
I didn’t find opt in the standard description, but I often use it, so I add it here
/opt is the abbreviation of optional, and it’s mainly used for optional application packages
When I use it, it’s generally for applications that I download on GitHub, not from the Raspberry Pi OS repositories
For example, if you install AlexaPi on your Raspberry Pi, the documentation tells you to download the files here, and then you use the binary file from /opt/AlexaPi
/media and /mnt
/media and /mnt are not mandatory, but you’ll probably use them when you use USB drives
In theory, /mnt is for temporary mounted filesystems and /media for removable media
So if you mount a USB drive, you’ll create a folder like /media/usb and mount it in this folder
Tips & common mistakes
This part is a list of tips coming from the mistakes I often see with the Linux file system
In my job, I train new employees on Linux usage, and I always see the same errors 🙂
Know where you are
Most tips can be avoided if you have a clear idea of where you are in the Linux files tree.
In general, the current path is written in the Terminal prompt:
In this screenshot, you have the current user, the device name and the current path: /var/log/apt
If you don’t have it, you can use the following command to find it:
No, it’s not the abbreviation of “password” but “print working directory”
It’s critical to know where you are when you use a command.
For example, if you use
sudo rm -rf * to remove all files at your location, it’s better to be sure where you are because a wrong location will probably destroy your system 🙂
So, let’s say you are in /var/log/apt and want to go to your home folder
You have several options, that all works:
Obviously, the last one is better, but all of them work.
The most common mistake is to not know when to use /home/pi or home/pi for example.
You can omit the first slash only if you already are in the top folder.
If you are in /home, you can use
If you are in /, you can use
But most of the time, it’s difficult to understand
In general, I recommend to always use the first /, even if there is a shorter version possible, it’s easier to understand in the beginning.
For example, if you are in /home/pi,
cd Downloads and
cd /home/pi/Downloads work, but
cd /Downloads doesn’t work, there is no folder Downloads in the root directory (/).
If you understand directly, perfect, if not, keep the long version, it’s better than trying things during 10 minutes each time you use
Execute a script
This one is almost the same problem.
When you execute a script, you need to indicate the path.
In general, something like:
But if you don’t want to use the first command, and run the script directly (in a crontab for example), which path do you use if you are in /home/pi?
Yes, 1, 2 and 4 work, but not the other one.
It’s the same thing for anything you want to do with files.
You need to use either the relative path or the absolute path (full path from the root), but not a mix of both.
That’s it, you now know a bit more on how the Linux file system works
I hope you liked this post, and will remember a few things after that 🙂
As usual, this kind of learning takes practice to be memorized in the long term.
So, don’t stop here, use Raspberry Pi OS Lite or a terminal more often, understand your mistakes or why you are doing these commands. Is there a shorter method to do the same thing? (with the absolute path for example)
If you need help with the terminal usage, here are a few posts that should help: