Linux Directory Structure Explained

Linux Directory Structure File system hierarchy

Learning Linux isn’t restricted to learning different Linux commands. It is also important to understand the directory structure of the entire Linux file system and the functions of each directory.

When using Linux, a command line input ls -l / can be seen, contains numerous subdirectories (referred to as a first-level directory) in Linux root directory (/), e.g. etc, home, lib like.

Linux Directory structure Explained

Everything on Linux is stored in the root directory. The root directory contains a directory known as First Level Directory. You can consider the root directory as “C:/” in windows. Every first-level directory also contains many sub-directories also known as second-level directories, such as /etc/vim, /home/ubuntu, etc. Linux file system directories generally present a tree structure, and the /root directory is equivalent to the root of the tree.

Linux system is free and open-source, there are many Linux distributions, and there are many teams that use Linux to develop products. If everyone is allowed to configure the Linux system file directory according to their own ideas, many management problems may arise later. Just imagine, if you are new in a company, the file directory structure of the Linux system used is completely different from what you have learned, which is really a headache.

Linux Foundation has issued FHS(Filesystem Hierarchy Standard) Standard in order to prevent many users from arguing about the Linux system directory structure. Most Linux distribution systems follow FHS standards.

FHS standard specifies the purpose of all first-level directories and some second-level directories (/usr and /var) in the Linux system. The main purpose of publishing this standard is to let users clearly understand what types of files should be stored in the first-level and second-level directory.

[Note*: Remember a classic saying: In the Linux world, everything is a file.]

Schematic diagram of Directory Structure in Linux

Linux Directory structure Explained
Linux Directory structure Explained

The specific directory structure

FHS believes that the root directory (/) of the Linux system is the most important. The reasons are as follows:

  1. All directories are derived from the root directory.
  2. The root directory is closely related to the booting, repairing and restoring of the system;

Therefore, the root directory must contain the boot software, core files, programs required for booting, function libraries, repair system programs and other files, as shown in Table 1.

Table 1 Linux a directory and its role

First-level directoryFunction
/bin/Stores system programs and utilities such as bash shell. It contains important binary files that must be present when the system is mounted in single-user mode.
/boot/It contains the file that is needed to boot the system. Kernel files and GRUB Bootloader files are stored in /boot directory.
/dev/In Linux, everything is a file. /dev Directory contains devices that are represented as files. /dev/sda represents the first SATA drive-in system. /dev/random produces random numbers.
/etc/The location where the configuration file is saved. All service configuration files in the system that use the default installation method (rpm installation) are saved in this directory, such as user information, service startup scripts, configuration files for common services, environment variable configuration files, etc.
/home/When creating a user, each user must have a default location to log in and save their own data, which is the user’s home directory. For all new users in Linux, a new home directory is created with /home/username For example, the home directory of user infohubblog is /home/infohubblog
/lib/It Contains library files that support the binaries located under /bin and /sbin. Library filenames are either ld* or lib*.so.*. For example: ld-2.11.1.so, libncurses.so.5.7
/media/Temporary Mount directory for removal devices. The system is recommended to mount media devices, such as floppy disks and CDs
/mnt/There was only this mount directory in early Linux, and there was no subdivision. The system recommends that this directory be used to mount additional devices, such as U disks, mobile hard drives and other operating system partitions
/misc/Mount the directory. The system recommends mounting the shared directory of the NFS service. Although the system has prepared three default mount directories /media/, /mnt/, /misc/, the administrator can decide which device to mount in which directory. For example, when I came into contact with Linux, the default mount directory was only /mnt/, so I developed the habit of creating different directories under /mnt/ to mount different devices, such as /mnt/cdrom/ to mount CD-ROM, /mnt/usb/ mount U disk, all are possible
/opt/The storage location of the software installed by the third party. This directory is the location for placing and installing other software. Manually installed source code package software can be installed in this directory. But I still used to put the software in the /usr/local/ directory, that is to say, the /usr/local/ directory can also be used to install software
/root/The home directory of root. “/” and root directory under “/” is not the same.
/sbin/Save the commands related to the system environment settings, only root can use these commands to set the system environment, but there are also some commands that can be viewed by ordinary users
/srv/Service data directory. After some system services are started, you can save the required data in this directory
/tmp/The directory where the system or user stores temporary files. All users can access and write in this directory. It is recommended not to save important files in this directory. Files under this directory are deleted whenever the system reboots.

The FHS is limited to Table 1 for the subdirectories contained in the root directory, but in addition, the root directory of the Linux system usually contains several first-level directories in Table 2.

Linux /usr directory

usr (note that it is not user), the full name is Unix Software Resource, this directory is used to store system software resources.FHS recommends that all developers should reasonably place software product data in subdirectories under the /usr directory instead of creating separate directories for their products.

In the Linux system, all system default software is stored in the /usr directory. The /usr directory is similar to the combination of C:\Windows\ + C:\Program files\ in the Windows system.

FHS recommends that the /usr directory should have the subdirectories shown in Table 3.

Table 3 / usr subdirectories and their function

SubdirectoryFunction
/usr/bin/Store system commands, which can be executed by ordinary users and super users. These commands have nothing to do with system startup and cannot be executed in single-user mode
/usr/sbin/Store unnecessary system management commands for the root file system, such as most service programs, only root can use.
/usr/lib/The storage location of the function library called by the application
/usr/XllR6/Graphical interface system save location
/usr/local/The location where the manually installed software is saved. We generally recommend that the source package software be installed in this location
/usr/share/The storage location of the application’s resource files, such as help files, help files, and font directories
/usr/src/The storage location of the source code package. Both the source code package and the kernel source package that we manually downloaded can be saved here. However, I am more accustomed to saving the manually downloaded source code package in the /usr/local/src/ directory, and the kernel source code in the /usr/src/linux/ directory
/usr/includeC/ C++ and other programming language header files placement directory

Linux /var directory

The /var directory is used to store dynamic data, such as cache, log files, and files generated during software running. Generally, it is recommended to include these subdirectories as shown in Table 4 under this directory.

Table 4 / var subdirectory and Function

/var subdirectoryFunction
/var/lib/The data storage location needs to be called or changed during program running. For example, the MySQL database is stored in the /var/lib/mysql/ directory
/var/log/The directory where the log file is placed, which contains important files such as /var/log/messages, /var/log/wtmp, etc.
/var/run/After some services and programs are running, their PID (process ID) save location
/var/spool/There are mainly some temporary storage data that will be called by users at any time. For example, /var/spool/mail/ stores newly received mails, and /var/spool/cron/ stores system timing tasks.
/var/www/The home directory of the Apache web page installed by the RPM package
/var/nis and /var/ypThe directory used by the NIS service mechanism, nis mainly records the connection information of each client in all networks; yp is the directory where the log files of the nis service of Linux are stored
/var/tmpWhen some applications are installed or executed, certain files that need to be used after restarting, this directory can temporarily store such files, and then delete them after completion

According to the directories and functions listed in the above tables, if we do some experiments and exercises, we need to create some temporary files, where should they be stored?

The answer is the user’s home directory or /tmp/ temporary directory. But be careful that some directories cannot directly modify and save data, such as the /proc/fn/sys/ directory, because they are stored in memory. If you write data here, your memory will become smaller and smaller until The system crashes; the /boot/ directory cannot save additional data, because the /boot/ directory will be partitioned separately as the boot partition. If there is no free space, the system will not start normally.

In short, Linux should be operated and modified in a reasonable directory.

Hope you like our “Linux Directory Structure Explained” blog. Please subscribe to our blog for getting email updates on upcoming blogs.

Happy Coding!

Read More : Advantages of Linux | Architecture, User Management in Linux | Complete Guide,Important Groupmod Command in Linux with Example

Be the first to comment on "Linux Directory Structure Explained"

Leave a comment

Your email address will not be published.


*