|
| 1 | +--- |
| 2 | +title: Linux tree |
| 3 | +tags: [Linux, programming, divulgation] |
| 4 | +style: fill |
| 5 | +color: success |
| 6 | +description: Briefly analyzing Linux tree structure |
| 7 | +--- |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +# Introduction |
| 12 | + |
| 13 | +I usually find myself having a drink with friends, and a question arises: why would someone want to use Linux? It seems like a reasonable doubt; OSs like Windows are easier to use, have a showcase (UX) that is, at first glance, prettier, you can play more games on them immediately, it does not require computer knowledge to use, everything that a common person needs comes pre-installed, and it has many default tools, it has great support, etc. Moreover, it has the Windows Subsystem for Linux (WSL)!, which is essentially a compatibility layer from Microsoft to run Linux binaries (even using a real Linux kernel) within Windows itself (although obviously more limited than a native one). |
| 14 | + |
| 15 | +But if you are a programmer, you will understand that once you overcome the (not so flat) learning curve of Linux, the flexibility and control it offers will allow you to do things that in other operating systems you would find more limited. And perhaps more relevant, with the certainty that the OS is user- and privacy-respecting; you will be able to use your OS as you prefer. Linux does not operate under a proprietary software policy with forced updates and advertisements; moreover, it advocates much more for open software, which is especially interesting if you are a developer or researcher. It is easier to learn (out of curiosity) computer concepts in Linux than in Windows; and also to program and develop, let’s not fool ourselves. If your machine is very old, it probably cannot run an arbitrary Windows or MacOS, but it can run Linux. Also, if you are a computational artist and you like to customize your GUI or desktop _to the maximum_, Linux offers you more possibilities than any other. |
| 16 | + |
| 17 | +On the other hand, security (let's think about this in a very loosely strict way): the vast majority of users use Windows; so it is logical that the vast majority of cyberattacks are directed at Windows distributions; it is not that we are safer in Linux, which we are, but that we most probably are. Although under this same thinking, it is more frequent to have drivers/hardware support for Windows and not for Linux. |
| 18 | + |
| 19 | +But enough talking about competitions, since each option meets different needs, and they are not mutually exclusive. We will dedicate this post to delving into the Linux directory tree (and other UNIX-like operating systems). For Windows users, the well-known `C:\` drive and drive letters no longer exist; instead, the entire structure is replaced by a labyrinthine hierarchical file system and directories governed by the Filesystem Hierarchy Standard (FHS), which is the standard maintained by the Linux Foundation. |
| 20 | + |
| 21 | +# The Linux structure |
| 22 | + |
| 23 | +Before anything else, let's take a look and appreciate this beautiful tree of Linux distributions (GNU/Linux): |
| 24 | + |
| 25 | + |
| 26 | +This is my system: |
| 27 | +```bash |
| 28 | +$ uname -a |
| 29 | +``` |
| 30 | +```text |
| 31 | +Linux pop-os 6.9.3-76060903-generic #202405300957~1732141768~22.04~f2697e1 SMP PREEMPT_DYNAMIC Wed N x86_64 x86_64 x86_64 GNU/Linux |
| 32 | +``` |
| 33 | + |
| 34 | +Linux is an OS that works like a "big file system", or rather, "through files". Everything we see, even the directories themselves, the mouse, keyboard, printer..., are files. How can that be? It’s not that far-fetched; in the eyes of a computer, its lifeblood is 0s and 1s, and files contain nothing but binary numbers. |
| 35 | + |
| 36 | +There are several types of files in a Linux system: |
| 37 | + |
| 38 | +1. **General/Ordinary/Regular Files**: It may be an image, video, program, text file, or executable. These types of files can be in ASCII or binary format. It is the most commonly used file type in the Linux system. |
| 39 | + |
| 40 | +2. **Directory Files**: These types of files are a storage for other file types, so there may be a directory file within a directory (subdirectory). |
| 41 | + |
| 42 | +3. **Device Files**: In a Windows-like operating system, devices like CD-ROMs and hard drives are represented as drive letters like F:, G:, H:, whereas in the Linux system, devices are represented as files. For example, `/dev/sda1`, `/dev/sda2`, and so on. These comprise symbolic links, block files, socket files, and named pipe files. |
| 43 | + |
| 44 | +We are at the top of the Matrix (root), and we look below and identify 23 distinct directories. Most Windows people keep their whole lives warm inside the `/home` directory. But we are **gente con intriga**, and we will try to understand why each of them exists. We will go in a _certain_ order... |
| 45 | + |
| 46 | +## root (/) - Root Directory |
| 47 | + |
| 48 | +The Linux filesystem hierarchy starts with the root directory as its foundation. It contains all other directories and subdirectories. |
| 49 | + |
| 50 | +Let's take a look at the inmediate response of what we see below here: |
| 51 | + |
| 52 | +```bash |
| 53 | +alejandro@pop-os:/$ tree -D -L 1 |
| 54 | +[Dec 18 14:20] . |
| 55 | +├── [Sep 18 18:35] bin -> usr/bin |
| 56 | +├── [Jan 21 13:26] boot |
| 57 | +├── [Feb 1 11:16] dev |
| 58 | +├── [Jan 30 11:38] etc |
| 59 | +├── [Dec 18 14:18] home |
| 60 | +├── [Sep 18 18:35] lib -> usr/lib |
| 61 | +├── [Sep 18 18:35] lib32 -> usr/lib32 |
| 62 | +├── [Sep 18 18:35] lib64 -> usr/lib64 |
| 63 | +├── [Sep 18 18:35] libx32 -> usr/libx32 |
| 64 | +├── [Dec 18 14:05] lost+found |
| 65 | +├── [Dec 18 14:42] media |
| 66 | +├── [Sep 18 18:35] mnt |
| 67 | +├── [Jan 31 13:48] opt |
| 68 | +├── [Feb 1 11:15] proc |
| 69 | +├── [Jan 1 1970] recovery |
| 70 | +├── [Jan 18 12:39] root |
| 71 | +├── [Feb 1 11:16] run |
| 72 | +├── [Sep 18 18:35] sbin -> usr/sbin |
| 73 | +├── [Sep 18 18:35] srv |
| 74 | +├── [Feb 1 11:15] sys |
| 75 | +├── [Feb 1 11:59] tmp |
| 76 | +├── [Sep 18 18:35] usr |
| 77 | +└── [Dec 17 07:26] var |
| 78 | + |
| 79 | +23 directories, 0 files |
| 80 | +``` |
| 81 | + |
| 82 | +## /bin - Essential Binaries |
| 83 | + |
| 84 | +This directory stores essential executable programs for all users, such as `ls`, `cp`, `mkdir`, `rm`, among others. These binaries are critical for system recovery and maintenance, especially in single-user mode when other directories like `/usr` may not be mounted. |
| 85 | + |
| 86 | +## /boot - Boot Files |
| 87 | + |
| 88 | +Contains files needed for the system to boot, including the Linux kernel (`vmlinuz`), the bootloader (`grub`), and other essential boot configuration files. The `/boot` directory is crucial for the system's ability to start up, and it often resides on a separate partition to ensure it is accessible during the boot process. |
| 89 | + |
| 90 | +## /dev - Device Files |
| 91 | + |
| 92 | +Contains special files representing system devices, such as hard drives (`/dev/sda`), terminals (`/dev/tty`), and other peripherals. These files are not actual files in the traditional sense but rather interfaces to hardware devices. For example, writing to `/dev/sda` directly would write data to the disk, bypassing the filesystem. |
| 93 | + |
| 94 | +## /etc - Configuration Files |
| 95 | + |
| 96 | +Stores system and application configuration files. Examples include: |
| 97 | + |
| 98 | +- `/etc/passwd`: User account information. |
| 99 | +- `/etc/group`: Information about user groups. |
| 100 | +- `/etc/fstab`: File system and mount point information. |
| 101 | + |
| 102 | +The `/etc` directory is one of the most important directories for system administrators, as it contains the configuration files that define how the system behaves. |
| 103 | + |
| 104 | +## /home - User Directories |
| 105 | + |
| 106 | +Each user has a personal directory under `/home` to store their files and configurations. Example: `/home/username`. This directory is where users typically store their personal files, such as documents, music, and pictures. It is also where user-specific configuration files (dotfiles) are stored, such as `.bashrc` and `.config`. |
| 107 | + |
| 108 | +```bash |
| 109 | +alejandro@pop-os:/home$ tree -D -L 2 |
| 110 | +[Dec 18 14:18] . |
| 111 | +└── [Feb 1 11:16] alejandro |
| 112 | + ├── [Jan 24 18:05] Desktop |
| 113 | + ├── [Jan 21 19:16] Documents |
| 114 | + ├── [Jan 29 01:04] Downloads |
| 115 | + ├── [Jan 22 13:04] gems |
| 116 | + ├── [Dec 18 14:29] Music |
| 117 | + ├── [Jan 23 22:00] Pictures |
| 118 | + ├── [Dec 18 14:29] Public |
| 119 | + ├── [Dec 18 14:29] Templates |
| 120 | + └── [Jan 21 19:17] Videos |
| 121 | + |
| 122 | +10 directories, 0 files |
| 123 | +``` |
| 124 | + |
| 125 | +## /lib - Shared Libraries |
| 126 | + |
| 127 | +Holds essential libraries required to run the binaries in `/bin` and `/sbin`. These libraries are critical for both the operating system and the applications in those directories. The `/lib` directory typically contains shared libraries (`.so` files) that are used by multiple programs. |
| 128 | + |
| 129 | +## /lib32- Shared Libraries |
| 130 | + |
| 131 | +Contains 32-bit libraries for running 32-bit applications on 64-bit systems. This directory is essential for compatibility with older software that has not been updated to 64-bit. |
| 132 | + |
| 133 | +## /lib64- Shared Libraries |
| 134 | + |
| 135 | +Stores 64-bit libraries needed to run 64-bit applications on 64-bit systems. This directory is the counterpart to `/lib` for 64-bit systems. |
| 136 | + |
| 137 | +## /libx32- Shared Libraries |
| 138 | + |
| 139 | +Contains shared libraries for the x32 architecture, which uses 32-bit pointers but operates in 64-bit mode. This architecture is less common but can it offer performance benefits in certain scenarios. |
| 140 | + |
| 141 | +## /lost+found - File Recovery |
| 142 | + |
| 143 | +Used to store fragments of files recovered after file system failures. When the filesystem is checked for errors (using tools like `fsck`), any recovered files are placed here. This directory is typically empty unless there has been a filesystem issue. |
| 144 | + |
| 145 | +## /media - Removable Media Devices |
| 146 | + |
| 147 | +Mounts removable devices like USB drives, CD-ROMs, and external disks. When a removable device is connected, it is automatically mounted under `/media` by most modern Linux distributions. |
| 148 | + |
| 149 | +## /mnt - Temporary Mount Points |
| 150 | + |
| 151 | +Temporary mount point for external file systems, such as additional hard drives or partitions. This directory is often used by system administrators to manually mount filesystems for temporary access. |
| 152 | + |
| 153 | +## /opt - Optional Software |
| 154 | + |
| 155 | +Contains manually installed software and third-party applications, typically those not included in the default package management system. Software installed in `/opt` is often self-contained, meaning it includes all necessary libraries and dependencies within its own directory structure. |
| 156 | + |
| 157 | +## /proc - Virtual File System |
| 158 | + |
| 159 | +A pseudo-filesystem that stores information about running processes and system state. Examples: |
| 160 | + |
| 161 | +- `/proc/cpuinfo`: Processor information. |
| 162 | +- `/proc/meminfo`: Memory usage statistics. |
| 163 | +- `/proc/filesystems`: Supported file systems. |
| 164 | + |
| 165 | +The `/proc` directory is a virtual filesystem that provides a window into the kernel's internal data structures. It is dynamically generated and does not exist on disk. |
| 166 | + |
| 167 | +## /recovery - Systen Recovery |
| 168 | + |
| 169 | +Used in some distributions to store recovery tools and files in case of system failures. This directory is not part of the standard FHS but is included in some distributions to aid in system recovery. |
| 170 | + |
| 171 | +## /root - Root User Directory |
| 172 | + |
| 173 | +The personal directory of the root user (administrator), similar to `/home`, but exclusive to the superuser. This directory is where the root user stores their personal files and configurations. |
| 174 | + |
| 175 | +## /run - Volatile Runtime Data |
| 176 | + |
| 177 | +Contains data files used by running processes. It is cleared upon reboot. This directory is used for storing runtime information such as PID files, lock files, and other temporary data needed by running processes. |
| 178 | + |
| 179 | +## /sbin - System Binaries |
| 180 | + |
| 181 | +Stores system binaries, including administrative commands for the superuser, such as `fdisk`, `fsck`, `reboot`, and `shutdown`. These binaries are essential for system administration and are typically not used by regular users. |
| 182 | + |
| 183 | +## /srv - Service Data |
| 184 | + |
| 185 | +Contains data used by services like web servers, FTP servers, and databases. For example, if you are running a web server, the website files might be stored in `/srv/http`. |
| 186 | + |
| 187 | +## /sys - System and Hardware Information |
| 188 | + |
| 189 | +A virtual file system that stores information about hardware devices and kernel configurations. The `/sys` directory is similar to `/proc` but focuses more on hardware and kernel parameters. It is used by the kernel to export information about devices and drivers to user space. |
| 190 | + |
| 191 | +## /tmp - Temporary Files |
| 192 | + |
| 193 | +Stores temporary files that can be deleted after use. It is generally cleared upon reboot, so it's not suitable for important data. The `/tmp` directory is often used by applications to store temporary files during their operation. |
| 194 | + |
| 195 | +## /usr - User Programs and Data |
| 196 | + |
| 197 | +Contains installed programs and applications, along with shared data such as libraries and documentation. Key subdirectories: |
| 198 | + |
| 199 | +- `/usr/bin`: User binaries. |
| 200 | +- `/usr/sbin`: Administrative binaries. |
| 201 | +- `/usr/lib`: Shared libraries. |
| 202 | +- `/usr/share`: Shared files like documentation and themes. |
| 203 | +- `/usr/local`: Manually installed software. |
| 204 | + |
| 205 | +The `/usr` directory is one of the largest directories in the Linux filesystem and contains most of the user-facing applications and libraries. |
| 206 | + |
| 207 | +```bash |
| 208 | +alejandro@pop-os:/usr$ tree -D -L 1 |
| 209 | +[Sep 18 18:35] . |
| 210 | +├── [Jan 30 11:38] bin |
| 211 | +├── [Apr 18 2022] games |
| 212 | +├── [Jan 18 12:49] include |
| 213 | +├── [Jan 29 12:03] lib |
| 214 | +├── [Sep 18 18:35] lib32 |
| 215 | +├── [Dec 17 07:01] lib64 |
| 216 | +├── [Jan 3 13:07] libexec |
| 217 | +├── [Sep 18 18:35] libx32 |
| 218 | +├── [Jan 12 22:17] local |
| 219 | +├── [Jan 30 11:38] sbin |
| 220 | +├── [Jan 30 11:38] share |
| 221 | +└── [Jan 21 13:20] src |
| 222 | + |
| 223 | +12 directories, 0 files |
| 224 | +``` |
| 225 | + |
| 226 | +## /var - Variable Data |
| 227 | + |
| 228 | +Contains files whose content changes frequently, such as logs, mail, and spool files. Examples include: |
| 229 | + |
| 230 | +- `/var/log`: System log files. |
| 231 | +- `/var/cache`: Program cache. |
| 232 | +- `/var/mail`: User emails. |
| 233 | +- `/var/spool`: Files awaiting processing, such as print jobs. |
| 234 | + |
| 235 | +The `/var` directory is used for data that is expected to change during the operation of the system. It is often used for logging, caching, and other dynamic data. |
| 236 | + |
| 237 | +## /snap - Snap Packages |
| 238 | + |
| 239 | +Present on systems using Snap application packaging format, this directory stores applications in Snap format. Snap is a package management system developed by Canonical that allows for the distribution of self-contained software packages. Each Snap package includes all necessary dependencies, making it easy to install and run applications across different Linux distributions. |
| 240 | + |
| 241 | +# Aditional notes |
| 242 | + |
| 243 | +- **Filesystem Hierarchy Standard (FHS)**: The FHS defines the structure of the Linux filesystem, ensuring consistency across different distributions. It specifies the purpose of each directory and the types of files that should be stored there. This standardization makes it easier for users and developers to navigate and manage Linux systems. |
| 244 | + |
| 245 | +- **Symbolic Links**: Many directories in the root filesystem, such as `/bin`, `/lib`, and `/sbin`, are symbolic links to their counterparts in `/usr`. This is part of a modern trend in Linux distributions to consolidate system binaries and libraries in `/usr` for simplicity and efficiency. |
| 246 | + |
| 247 | +- **Permissions and Ownership**: Each file and directory in Linux has associated permissions and ownership. These determine who can read, write, or execute the file. Understanding and managing permissions is crucial for system security and functionality. |
| 248 | + |
| 249 | +- **Mount Points**: Directories like `/media`, `/mnt`, and `/run` are used as mount points for external filesystems. Mounting is the process of making a filesystem accessible at a specific point in the directory tree. This allows users to access data on external devices or network shares. |
| 250 | + |
| 251 | +- **Virtual Filesystems**: Directories like `/proc`, `/sys`, and `/dev` are virtual filesystems that do not exist on disk. They provide interfaces to kernel data structures and hardware devices, allowing users and programs to interact with the system in a filesystem-like manner. |
| 252 | + |
| 253 | + |
0 commit comments