Exploring Linux File System
Tags: linux
Published: 2021-03-29 17:58:43

---
layout: post
title: "Exploring Linux File System"
description: "Exploring Linux File System in detail and going over each directory of file system"
keywords: "linux unix filesystem sysadmin"
tags: linux
published: true
---

## What is File System and How they work?

In this post I will mostly explore linux file system and its directory structure but In order to explore linux file system first we need to understand what's a file system and how do they work?. In simple words, A filesystem is way in which files are named and logically placed in the computer for storage and retrieval. 
Without a filesystem, information placed in a storage medium would be one large body of data with no way to tell where it begins or stops and this is one of the reasons why having a file system is important as it keeps the data organized and makes it easier for the computer to retrieve the data.

A file system consists of two or three layers, sometimes these layers are explicitly separated, and sometimes the functions are combined.

- **Logical File System**: This layer is responsible for interaction with user applications such as providing with an `api` that allows functions such as 
`OPEN`,`CLOSE` and `READ`.These functions passes through this layer for processing. 
The logical file system also contains metadata of the file and directory for instance you can run `ls -la -h` to see file metadata such as 
`name`,`size`,`file permissions` and `etc`.
if the program doesn't have access to a file or directory then this layer will throw an error. This layer provides `file access`,
`directory operations` and `security`.

- **VFS(Virtual File System)**: This is an optional layer but you can think of VFS as a layer on the top of real file system or an interface between the kernel and a concrete file system. This allows you to access files across different filesystems. For example Windows can access Linux filesystem and vice versa without having to know which filesystem is being accessed.

- **Physical File System**: This layer is concerned with how physical blocks are being written or read. It handles memory management and buffering. It handles physical placement of the blocks in specific location of the storage medium. In simple words this layer decides where on the hard drive your files are supposed to be saved and how space should be saved by efficiently placing the files in the right location. Last but not least this layer mostly interacts with device drivers which allows saving on the storage medium such as HDD or SSD.


## Linux File System.

After understanding how a filesystem works now I will be covering specifically linux file system and its directory structure, explaining how the data is stored and kept in linux. Linux supports many other filesystems such as `ext3`,
`ext4` ,`btrfs` and many more. It supports around 100 types of filesytems and even the old ones but the most common filesystem among linux distributions is 
`ext4`.

## The Directory Structure.

In Linux and its many other distributions, A directory is structured in a tree like hiearchy system. Linux directory structure is well defined and documented  in the **FHS (File Hiearchy Standard)**. FHS is maintained by the Linux Foundation and its followed by major linux distributions.

The root `/` is the top of the filesystem (_basically it contains everything that is required or used by the OS_).


```sh
/
├── bin -> usr/bin
├── boot
├── dev
├── etc
├── home
├── lib -> usr/lib
├── lib64 -> usr/lib
├── lost+found
├── mnt
├── opt
├── proc
├── root
├── run
├── sbin -> usr/bin
├── snap -> /var/lib/snapd/snap
├── srv
├── sys
├── tmp
├── usr
└── var

20 directories, 0 files
```
As you can see how root directory represented using (`/`) contains everything.

_Note: You can also think of directory as file that holds bunch of addresses to other files._

I will be covering each directory separately explaining what each directory holds and represents.

### `bin/`

This directory is called `binaries` as this holds all the programs that live in our machine. For example here's what a `bin/` directory might look like and it mostly contains `executables`.
 
```sh
➜  ~ tree -L 1 /bin -C | tail -n 20
├── znew
├── zonetab2pot.py
├── zoom -> /opt/zoom/ZoomLauncher
├── zramctl
├── zresample
├── zretune
├── zsh
├── zsh-5.8
├── zsoelim -> soelim
├── zstd
├── zstdcat -> zstd
├── zstdgrep
├── zstdless
├── zstdmt -> /usr/bin/zstd
├── zvbi-atsc-cc
├── zvbi-chains
├── zvbid
└── zvbi-ntsc-cc
```

### `boot/`

This directory contains all the files required by the kernel at the time of boot and our bootloader also resides in this directory.

```sh
/boot
├── grub
├── initramfs-linux-fallback.img
├── initramfs-linux.img
├── initramfs-linux-lts-fallback.img
├── initramfs-linux-lts.img
├── intel-ucode.img
├── lost+found
├── vmlinuz-linux
└── vmlinuz-linux-lts
```

As you can see `grub` is also present in the `boot/` directory

### `sbin/`

This directory contains the system binaries that are required for the system adminstration.

### `dev/`

This directory contains all the devices and each device is represented with a file. This only represents devices attached to the system. For example your disk might show up as `dev/sda` and partition might show up as `dev/sda1`. This folder is usually accesed by the drivers and applications.

### `etc/`

This directory is known as `edit to configure` but it was also known as 
`et cetera` as you can [read here](https://unix.stackexchange.com/questions/5665/what-does-etc-stand-for) more about the history of 
`etc/` directory name.

`etc/` directory is where all your configs are stored for software used by the system. For example ,package manager such as `pacman` or `apt` as `etc/` holds the `config` of your package manager.

```sh
➜  ~ tree -L 1 /etc | grep "pacman"
├── pacman.conf
```

### `lib/`

This directory is also known as `libraries` and it contains all the libraries that are required to boot the system and used by different applications to perform different functions.

### `media & mnt/`

This directory contains the mounted drives. For example this is where you will find an another mounted drive such as `External HDD` or `SSD`. If you are mounting things manually use the `mnt/` directory and `media/` is where your 
`OS` will automatically mount.

### `opt/`

This drive contains all the manually installed software usually from the vendor.

_Note: Some software installed from package manager might also live here_

```sh
➜  ~ tree -L 1 /opt
/opt
├── Simplenote
├── sublime_text_3
└── zoom
```
For example you can see `zoom` and `sublime text`.

### `proc/`

In this directory you will mostly find files which contains the information of the hardware and even the running processes in the system. Each process is represented by a directory in `proc/`.

For example `spotifyd` (spotify daemon) represented as a process in `proc/`

```sh
➜  ~ ps aux | grep "spotifyd"
hackerm+   98147  0.0  0.1 365184 13324 ?        Ssl  07:15   0:00 /usr/bin/spotifyd --no-daemon
```

```sh
➜  ~ tree -L 1 /proc | grep "98147"
├── 98147
```


### `root/`

This folder is basically considered as the `home/` folder of the `root` user and it is only accessed by the user with root permission.

### `run/`

This is a fairly new folder and different linux distributions use this in different ways. This folder is basically mounted as temporary filesystem (tmpfs) as this folder is wiped when the system is rebooted or shutdown and it contains programs required early in the boot procedure. 


```sh
➜  ~ tree -L 1 /run
/run
├── credentials
├── cups
├── dbus
├── dhcpcd
├── dmeventd-client
├── media
├── mount
├── mysqld
├── named
├── NetworkManager
├── nscd
├── openvpn-client
├── user
├── utmp
└── wpa_supplicant
```

### `srv/`

This is also known as `service` directory and this is where all the files stored that are accessed by external users when using `ftp` server.

### `sys/`.
Its also known as the system folder and this folder contains files that interact with the kernel. This directory is created when the system boots up.

```sh
➜  ~ tree -L 1 /sys
/sys
├── block
├── bus
├── class
├── dev
├── devices
├── firmware
├── fs
├── hypervisor
├── kernel
├── module
└── power
```

### `tmp/`

This directory is known as temporary directory where files are stored by the applications that can be stored during a session. For example a word processor like libreoffice might store temporarily if the program crashes or system reboots.

### `usr/`

This directory contains shareable, read-only files, including executable binaries and libraries, man files, and other types of documentation.

### `var/`

This directory contains files or directories that are expected to grow and that's why its know as `variable` folder. For example you can find logs of 
`databases`,`webservers` `emailboxes` and etc.


### `home/`

Home directory is the storage for user files. Each user has a subdirectory in /home. This is where you will find application settings as hidden directories for example browser cache `.cache/` and this is where your [dotfiles](https://wiki.archlinux.org/index.php/Dotfiles) live.



&nbsp;
&nbsp;




I have been using linux for more than 4 years now and as a linux user I have always been intrigued to explore the linux file system and its directory structure. This posts aims towards high level explanation of how filesystem works,linux file system and its directory structure. I do not intend to go over in detail about a specific filesystem such as `ext4` but that's a topic for my another blog post.


I hope you enjoyed reading this post and got to learn something new. If you think I missed anything you can always [DM](https://twitter.com/muhammad_o7) on twitter or [EMAIL ME](mailto:muhammadraza0047@gmail.com). Last but not least I would love to hear your thoughts.


### Originally Posted [Here](https://muhammadraza.me/2021/Linux-FS/)