How Does the Raspberry Pi File System Works?

The Raspberry Pi file system is based on the Linux operating system, which uses a hierarchical file system. This means that files and folders are organized in a tree-like structure, with the root directory at the top. All other files and folders are stored in the root directory or in subdirectories below it. The Raspberry Pi file system is organized into three main directories: /bin, /usr, and /home.

The /bin directory contains the most important system commands, such as ls, cp, and mv. The /usr directory contains user-installed programs and libraries. The /home directory is where users store their personal files and folders.

The Raspberry Pi also has a virtual file system, which is used to access files stored on external devices such as USB drives and SD cards. This virtual file system is mounted at the /media directory.

Finally, the Raspberry Pi also has a boot partition, which is used to store the operating system and other important files. This partition is mounted at the /boot directory.

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)

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.

If you’re looking to quickly progress on Raspberry Pi, you can check out my e-book here. It’s a 30-day challenge where you learn one new thing every day until you become a Raspberry Pi expert. The first third of the book teaches you the basics, but the following chapters include projects you can try on your own.

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

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:

  • Videos
  • Downloads
  • Music
  • Pictures
  • Desktop
  • Etc.

It’s really close from Windows on this point.

/root

/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

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
  • Etc.

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 apache as a web server, this folder contains all the configuration files
  • /etc/ssh: same thing for SSH client and server
  • /etc/php: same thing for PHP
  • Etc.

You understand the concept, if you are looking for a configuration file, it’s probably somewhere under /etc.

/var

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

/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

/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.

/opt

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 file systems 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:
linux prompt path

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:
pwd
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 ?

cd

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:

cd ..
cd ..
cd ..
cd home
cd pi

cd ../../..
cd home/pi

cd /
cd home/pi

cd /home/pi

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 cd pi/Downloads
If you are in /, you can use cd var/log
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 cd.

Are you a bit lost in the Linux command line? Check this article first for the most important commands to remember, and a free downloadable cheat sheet so you can have the commands at your fingertips.

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:

cd /home/pi/scripts
./myscript.sh

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?

  1. scripts/myscripts.sh?
  2. ./scripts/myscript.sh?
  3. home/pi/myscript.sh?
  4. /home/pi/myscript.sh?

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.

Want to chat with other Raspberry Pi enthusiasts? Join the community, share your current projects and ask for help directly in the forums.

Conclusion

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:

Additional Resources

Not sure where to start?
Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Watch the Raspberry Pi Bootcamp course now.

Master your Raspberry Pi in 30 days
Don’t want the basic stuff only? If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.
Download the e-book.

VIP Community
If you just want to hang out with me and other Raspberry Pi fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Create, understand, and improve any Python script for your Raspberry Pi.
Learn the essentials step-by-step without losing time understanding useless concepts.
Get the e-book now.

You can also find all my recommendations for tools and hardware on this page.

How Does the Raspberry Pi File System Work?

The Raspberry Pi is a small, low-cost computer that has become popular for its versatility and ease of use. It runs a variety of operating systems, including the popular Linux-based Raspbian. The Raspberry Pi also has its own file system, which is used to store and manage files on the device.

The Raspberry Pi file system is based on the Linux file system, which is a hierarchical structure of directories and files. The root directory is the top-level directory, and all other directories and files are contained within it. The root directory is represented by a single forward slash (/) character. All other directories and files are located within the root directory.

The Raspberry Pi file system is organized into several different directories. The most important of these is the /home directory, which is where all of the user’s files are stored. The /bin directory contains all of the executable programs, while the /etc directory contains configuration files. The /usr directory contains user-specific programs and data, while the /var directory contains system-specific data.

The Raspberry Pi file system also includes several special directories. The /dev directory contains device files, which are used to access hardware devices. The /proc directory contains information about the system’s processes, while the /sys directory contains information about the system’s hardware. The /tmp directory is used for temporary files, while the /mnt directory is used for mounting external storage devices.

The Raspberry Pi file system is designed to be easy to use and understand. All of the directories and files are organized in a logical manner, making it easy to find what you need. The file system also allows users to create their own directories and files, giving them the flexibility to customize their Raspberry Pi experience.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?