Linux Directory Structure

Learn via video courses
Topics Covered

Linux, unlike Windows, has a unique directory structure where everything is considered a file, including directories and devices like printers. Familiar terms such as root, lib, bin are part of this structure, governed by the Linux Foundation's Filesystem Hierarchy Standard (FHS). This article delves into the detailed hierarchy, aiding newcomers in navigating Linux's distinct environment.

File-system Hierarchy Standard

Linux runs on the structure of the UNIX operating system. Operating systems like BSD and macOS also use a similar directory structure. Hence, if you understand this directory structure, you can navigate better on Linux systems. Without further ado, let's jump into the various Linux directory structures.

/ — The Root Directory

We can read the '/' as the root directory. It is the only Linux directory under which all other files and directories reside. Only the root users get the privilege to write or change the directory structure. Also, note that the /root is the user's home directory & is not the same as /. Both are different. In other words, the / is the starting point in the Linux directory structure. Let us consider that you have a folder of documents; then, the Linux directory will look like /home/user/documents. You can read the directory structure as root -> home -> user -> documents.

/bin — Essential User Binaries

The /bin is another popular directory that contains necessary user binaries. In other words, it holds the executable files of numerous basic shell commands like cp, ls, cd, and other such commands. Since these executable programs remain in binary format, they reside in this directory. The operating system leverages these binary files for system-related operations. Other applications like Firefox and Chrome reside in the /usr/bin, which is not the same as the /bin directory. All the essential system files & utility programs like the bash shell lives in the /bin directory. The /bin directory ensures that no files within it are mounted because it contains numerous system administration binaries.

/boot — Static Boot Files

Booting is an essential process that involves various hardware and operating system files to start the computer and load the operating system into working mode. Linux directory structure also contains the boot directory (/boot) that holds specific files required for the system booting. For example, the GRUB bootloader & LILO, with some other booting files associated with the boot image and Linux kernel, reside in this directory.

/cdrom — Historical Mount Point for CD-ROMs

This directory is not a part of the standard Linux directory structure. But some well-known operating systems like Ubuntu have this directory. When users insert the CD into the CD-ROM, this temporary folder appears to show the files and other directories within the CD. However, the standard Linux directory structure has the /media directory to show temporary media files.

/dev — Device Files

The /dev directory is another Linux directory location that contains device files such as dev/sda1, dev/sda2, etc. These files represent the device. These are not actual files. It means they do not reside physically on the drive. The /dev/sda holds the first SATA drive. For partitioning it, you have to use a partition editor to edit `/dev/SDA.

/etc — Configuration Files

All the system's core configuration file resides in this directory. The system administrator & various other internal core services like the networking or password files use configuration files from here. If the admin wants to configure the system in any way (for example: changing the hostname) they would have to work with config files located in this directory.

/home — Home Folders

In the Linux directory structure, each user gets a home directory, which is the user's directory. This directory holds user-specific configuration files and other user data. As a Linux user, you can put all your files, programs, notes, etc., in this home directory. When a system administrator creates a user account on the Linux system, it is a good practice to create a home directory for that user.

/lib — Essential Shared Libraries

Libraries are a collection of codes that other binary executables use to perform various system-related tasks. All these library files reside in the /lib directory. Libraries needed for various binary files residing in the /usr/bin & /usr/sbin remains in the /usr/lib directory.

/lost+found — Recovered Files

Each Linux directory structure has a lost+found directory. It is a critical Linux directory that stores corrupted files when your system crashes. If your file system crashes, the Linux operating system will perform a check at the next boot. During boot-up, all the corrupt files will get stored in the lost+found directory. From this directory, users can recover the majority of their data.

/media — Removable Media

It is a directory where various removable media devices like SD cards, USB disks, or DVDs get mounted. All these removable media files reside as subdirectories within the /media directory. These subdirectories get created automatically. For instance, if you insert a CD into your CD-ROM, the media directory gets created automatically. Within the /media's subdirectory, you can navigate the files stored in the CD.

/mnt — Temporary Mount Points

It is similar to that of /mount. The only difference is instead of automatically getting mounted when the Linux encounters media files - the system administrator manually does the mounting. For instance, if you wish to mount a Windows partition for recovering operations, you will mount its files at /mnt/windows.

/opt — Optional Packages

Linux also leverages a lot of optional software packages. For these, there is a separate directory in the Linux directory structure. Usually, the /opt directory helps install & store the files of various third-party apps. In other words, all proprietary software that does not obey the standard Linux file system hierarchy gets piled up when installed.

/proc — Kernel & Process Files

It is another popular directory under the Linux directory structure that holds details and information about currently executing processes. It contains particular files that represent the system operations and various kernel parameters. For instance, if you want to check your system's processor status, you can navigate to the /proc/cpuinfo file. For memory-related information, you can navigate to the /proc/meminfo. All these reside under the /proc directory.

/root — Root Home Directory

It is another crucial directory in the Linux directory structure, which acts as the root user's home directory. It is not the same as the / (root). Instead of having the directory structure as /home/root, it directly resides like /root.

/run — Application State Files

It is a comparatively new directory in the Linux directory structure. In this location, all the short-lived files that are generated or run for specific purposes like process IDs or sockets get stored in this directory. Files under the /run directory are critical process files that other applications or kernel use. So, we cannot hold them in the /tmp directory because the /tmp files might get deleted.

/sbin — System Administration Binaries

It is another integral directory under the Linux directory structure similar to the /bin directory. It contains essential binaries that the root or sudo user executes for system administrations. We can consider the s in the sbin as super or sudo (super user do).

/selinux — SELinux Virtual File System:

The /selinux is a directory that contains particular files when your Linux distribution leverages the SELinux for security purposes. Some well-known operating systems that have this directory are Red Hat and Fedora. Ubuntu does not leverage the SELinux, hence does not contain the /selinux directory.

/srv — Service Data

Various service-related data associated with the system or kernel reside in this directory. We often employ services like Apache server, HTTP server, etc., while developing websites or other web applications. Storing such website data in these directories is a good practice.

/tmp — Temporary Files

Numerous applications and third-party software generate the temporary file(s) when they start executing. Those temporary files reside in the /tmp directory. These files get deleted automatically when the user restarts the system or uses utilities like "tmpwatch" to delete those temporary files after a fixed duration.

/usr — User Binaries & Read-Only Data

It is another important directory in the Linux directory structure. It contains various files and apps that the user or the systems use. For instance, all the non-essential files and apps reside in the /usr/bin directory rather than the /bin directory.

/var — Variable Data Files

It is the writable counterpart of the /usr directory. It remains in read-only mode for normal operations. Various log files & any other files that get written to /usr during regular operation reside within this directory. For example, apps or programs that create log files will remain stored in /var/log.

Conclusion

  • We hope this article has given a crisp idea of Linux directory structure.
  • We have gathered insight into file system hierarchy standards.
  • Then we came across the / (root) directory of Linux.
  • Then we understood the various directories like /bin, /boot, /cdrom, /dev, /etc, /home, /lib, /lost+found, /media, /mnt, /opt, /proc, /root, /run, /sbin, /selinux, /srv, /tmp, /usr, and /var that store different files.