Linux quickies | 3/5 — File System (Where to explore in File System)
Welcome!
Hey everyone, I’m Saman, a passionate Cloud Engineer, and I’m here to document my knowledge on a wide range of stuff, from Cloud computing and networking, to Linux machines and automation tools!Hey everyone,
welcome back to Linux quickies series, part 3/5. I hope you enjoyed the previous articles and found it useful! Also, If you haven’t read them yet, make sure to do so as everything is connected and in a special order. Check my profile for previous stories!
In this article we’ll discuss:
- Filesystem Hierarchy Standards (FHS)
- Reminder on the
cd
command and how to move between directories - Main purpose of each directory
- Conclusion
Linux File System; Where to Explore in the File System
In the first article (1/5), we presented imaginary scenarios of connecting to Linux servers and primarily discussed “How to look for” and locate files in a Linux environment. In this article, we will delve into the file system and focus on “Where to look for” for specific files that you might need to gain insights into your system, processes, applications, and more.
It’s a relatively short article, but incredibly important as I believe it will fill some pieces of the Linux puzzle for you! With that, Let’s go ahead and begin this story.
File System
The Linux file system is a hierarchical structure that organizes and stores files and directories. It follows a unified file system hierarchy which refers to Filesystem Hierarchy Standard (FHS) to ensure consistency across different Linux distributions, Making it a very useful knowledge to learn!
Now what it means to be hierarchical?
As you can see above, the file system is a collection of files in your system that has a Tree style structure that starts from the root directory. We’ll dive into purpose of each directory (Folder) shortly.
Moving between directories from Terminal
As you perhaps remember from the 1/6 article, you can change your directory with cd /path/to/the/directory
command. you can also go one directory back with cd ..
command. This means if your in the /usr/bin/
directory, running cd ..
will take you one step back, to the /usr/
directory.
If you’re current working directory is for example /var
, for moving to log
directory you can simply run cd var
, and you don’t need the /
before log
, because you’re already one level above to the var
in the hierarchy structure.
But if you’re current working directory is /var
and you want to switch to usr/share/
you only can reach there from the /
directory, relative to the position you are in the hierarchy structure.
So the command to move right away from /var
directory to usr/share
will be cd /usr/share/
.
The cd ~
command will take you the /home/<user-directory>/
directory from wherever you’re currently at. and for that you can use ~
as a Relative path to access the files in the /home/<user-name>/
directory.
For example, regardless of where you are at the file system, you can cat
the notes.txt
file from the Documents
directory right away by running:cat ~/Documents/notes.txt
, instead of running /home/username/Documents/notes.txt
. as the ~
relative path refers to the home directory of the current user.
Now let’s have a brief look to see what kind of files usually reside in each of these directories:
Directories and their main purpose
- Root Directory
/
: The root directory is the Top level directory in the file system hierarchy. All other directories and files are located beneath the root directory. For example, if you have a folder called weekly-planner, and inside this folder you have folders for each day of the week that you keep your plans in them, the weekly-planner is your root directory.
[weekly-planner]$ tree
.
├── friday
│ └── plan.txt
├── monday
│ └── plan.txt
├── saturday
│ └── plan.txt
├── sunday
│ └── plan.txt
├── thursday
│ └── plan.txt
├── tuesday
│ └── plan.txt
└── wednesday
└── plan.txt
7 directories, 7 files
Above, you can see the tree structure by running thetree
command in the weekly-planner directory. here, you can see the hierarchy clearly. The.
represents the root directory; Theweekly-planner/
- /bin : The /bin directory contains the executable files (codes) that we use to achieve basic system functionality. The executable for the commands such as
tree
,ls
,cat
and virtually everything you can run without root privileges exist in the /bin directory. When you runls -l
command, in the background, the system looks for its code in the /bin directory and run it. - /sbin : Its similar to
/bin
directory, but it holds executable for the commands that are primarily used by the system administrator (the root user). For example, if you try to restart a service, like docker usingsystemctl
command, as its executable resides in the/sbin
directory, you can not run it as a non-root user, and you will face a Permission denied error. You can run such commands either by switching to root user, or by running them withsudo
command; the superuser. We will thoroughly cover this topic in the next article “Users & Permissions”. The executable for crucial commands reside in the /sbin. - /etc : This is one of the directories you’re going to visit a lot as a DevOps Engineer or SRE. This directory contains configuration files for the system and various applications. You often might visit this directory to customize the system behavior and application settings. For example, let’s say your frontend is running on an nginx webserver, and you need to modify the nginx.conf file to make your frontend function. Where you will look for this file? Correct,
/etc/nginx/nginx.conf
. We’ll break this down with some examples in the end.
- A notable file residing in/etc/
ispasswd
, which we will cover completely in the next article (Users & Permissions). - /var : This is another directory you’re going to pass by a lot. The
/var
directory holds the variable data that changes during the system operation. It contains various sub-directories that are quite relevant to DevOps and SREs:
-/var/log
: Log files generated by system services and applications are stored here, and most of the times, it can be all about analyzing the logs for troubleshooting, monitoring, and maintaining system health.
-/var/www
: This directory is commonly used to store web content (HTML, CSS, JavaScript, etc) files for web servers like Apache and nginx.
-/var/lib
: Contains the variable state information and databases for applications and services. For example, database files for MySQL or PostgreSQL might reside here.
-/var/lock
: Is used to store lock files. Lock files are used to coordinate access to resources between multiple processes. When a process wants exclusive acces to a resource, it can create a lock file in this directory to indicate that the resource is currently in use. Other processes can check the presence of the lock file before accessing the resource to avoid conflicts. It kinda reserves the resource in VIP style.
-/var/cache
: Application-specific cache files can be find in this directory. - /usr : The
/usr
directory, as its self explanatory, contains user-related files including user binaries, libraries, documentations, and more. It also has sub-directories such as:
-/usr/bin
: user executables for commands are store in this directory.
-/usr/sbin
: as you can guess, the executables used by the root are here.
-/usr/local
: This directory often is where locally installed software, scripts, or libraries that are not part of the core system reside.
-/usr/man
: As its almost self explanatory, this directory contain’s the system’s manual pages. If you remember theman
command, that is used to acces and view these manual pages. - /home : Each user on the system, typically has a separate sub-directory within the
/home
for itself. like for me its/home/saman
. user-specific files and directories such as /Documents, and /Downloads reside here. - /tmp : The
/tmp
directory is used to store temporary files created by various applications, and its often cleared on system reboot. do not keep anything important there. - /opt : This directory is used for installing optional or third-party software. it can be a common location for installing development tools, or additional applications such as docker-desktop, google chrome, Adobe, and more.
- /proc : The /proc is an interesting directory. It’s a virtual file system that provides a view into running processes and system’s kernel. It contains the information about the processes, hardware, devices, system configuration, and more, represented also as a hierarchy of directories and special files. For example,
/proc/cpuinfo
provides information about the system’s CPUs,/proc/meminfo
provides information about memory usage, and/proc/PID
contains information about a specific process with the corresponding process ID.
All these directories play important roles in the Linux file system and provide essential functionality for managing processes, accessing devices, sharing data files, and obtaining system information. Understanding their purposes is very useful, and can help you navigate, utilize and observe the Linux system effectively.
With knowing what to look for in linux systems, e.g log files, config files, user-related files, etc and knowing how to efficiently access, and potentially modify them, you will be in a very good position as DevOps Engineer or SRE. Without a solid understanding of the concepts above of course, it’s going to be painful, and will take much of your time in an Agile environment, which is not what you want.
Conclusion
I hope you enjoyed the article and you encountered something that you didn’t know! Thanks for reading, and stay tuned for the future articles in the Linux quickies series.