Welcome to github.com/hsfxuebao/j… , hope to help you, if you feel ok, please click on the Star

For every Linux learner, to understand the directory structure of the Linux file system is a crucial step to learn Linux. Let’s start with a look at the Linux directory structure.

When using Linux, if you use ls -l /, you will find that there are many directories under /, such as etc, usr, var, bin… . And so on directory, and in these directories, we go to have a look, found that there are also a lot of directories or files. The file system looks like a tree structure under Linux, so we can call the file system structure visually a tree structure.

The file system is used to organize and arrange file access, so it is visible. In Linux, we can use tools such as LS to view its structure. In Linux, we see tree structure. For example, if the operating system is installed on a file system, it appears as a/start tree structure. The top of the Linux file system is /, which we call the root of Linux, the file system of the Linux operating system. The entry point of the Linux file system is /, where all directories, files, and devices are located. / is the organizer and top leader of the Linux file system.

Because Linux is open source, companies and organizations do their own operations and programming according to the core code of Linux. This results in a different directory under the root. This makes it impossible for individuals to use someone else’s Linux PC. Because you don’t know some basic configuration, where the files are… This creates confusion. This is why the FHS (Filesystem Hierarchy Standard) organization was born. The organization is a spontaneous group of Linux enthusiasts, mainly to do some basic requirements for Linux, so that operators will not become Linux ‘illiteracy’ when switching to a host.

According to FHS(www.pathname.com/fhs/) official documents refer to… Their main goal is to let users know where installed software is usually placed, so they want independent software developers, operating system makers, and users who want to maintain systems to follow FHS standards. In other words, the focus of the FHS is to regulate what kind of data should be placed in each particular directory. This has a lot of advantages, because Linux is able to develop the unique style that developers want from its existing look (directory architecture unchanged).

In fact, based on past experience, the FHS defines directories as four types of interaction, depending on how frequently the file system is used and whether it allows users to change at will. The table looks something like this:

Shareable

Unshareable

Constant (static)

/usr (where software is placed)

/etc

/opt (Third-party Collaboration software)

/boot (Boot and core files)

Variable

/var/mail (user email address)

/var/run (program dependent)

/var/spool/news (newsgroup)

/var/lock (program specific)

Four types:

1. Shareable:

A directory that can be shared with other systems for mounting. Therefore, data including execution files and users’ emails can be shared with other hosts on the network for mounting.

2. Unshareable:

The device files running on their machines or socket files related to procedures, etc., are only related to their own machines, so of course it is not suitable to share with other hosts.

3. constant

Some data does not change very often, following distribution without change. For example, function libraries, file description files, host service configuration files managed by the system administrator, etc.

4. Changeable:

Frequently changing data, such as login files, newsgroups that can be accepted by the average user, etc.

In fact, FHS only defines what data should be placed at the bottom of the three levels of the directory tree:

/ (root, root directory) : related to the startup system.

/usr (Unix software resource) : related to software installation/execution;

/var (variable) : relates to the system operation process.

Meaning and content of root directory (/) :

The root directory is the most important directory in the entire system, because not only are all directories derived from the root directory, but also the root directory is related to boot/restore/system repair and other actions. Because the system boot requires specific boot software, core files, boot required programs, function libraries and other file data, if the system error, the root directory must also contain the program to repair the file system. Because the root directory is so important, FHS requests that the root directory not be placed in a very large partition, because the larger the partition is, the more data you can put in it, and therefore the greater the chance of errors.

Therefore, the FHS standard suggests that the partition where the root directory (/) resides should be as small as possible, and the software installed by applications should not be in the same partition as the root directory. Keep the root directory as small as possible. Not only is this more efficient, but the file system in which the root directory resides is less prone to problems. To put it bluntly, the root directory and Windows C disk the same.

For these reasons, FHS believes that the root directory (/) should contain subdirectories such as:

directory

File contents should be placed

/bin

The system has a number of directories to place executables, but /bin is special. Because /bin places instructions that can still be manipulated in single-player maintenance mode. The commands under /bin can be used by root and general accounts, including cat,chmod(modify permissions), chown, date, mv, mkdir, cp, bash, etc.

/boot

Mainly place the files used by the open machine, including the Linux core file and the open machine menu and boot required configuration files, etc. The common file name of the Linux kernel is vmlinuz. If you use grub as the boot manager, there is also /boot/grub/.

/dev

On Linux, any devices and peripherals are stored in this directory as files. By accessing a file in this directory, you are accessing a device. The more important files are /dev/null, /dev/zero, /dev/tty, /dev/lp*, /dev/hd *, /dev/sd*, etc

/etc

The main configuration files of the system are almost all placed in this directory, such as personnel account and password files, the start files of various services and so on. In general, the file properties in this directory are viewable by normal users, but only root has the right to modify them. FHS recommends not putting executables in this directory. More important files are: /etc/inittab, /etc/init.d/, /etc/modprobe.conf, /etc/x11 /, /etc/fstab, /etc/sysconfig/, etc. /etc/init.d/ : this is where the default startup scripts for all services, such as to enable or disable iptables, are stored: /etc/init.d/iptables start, /etc/init.d/iptables stop

/etc/xinetd.d/ : this is the configuration directory for the services managed by the Super Daemon.

/etc/x11/: All the configuration files related to X Window are here, especially the xorg.conf or XF86Config configuration files for the X Server.

/home

This is the default user home directory. When you add a general user account, the default user home directory will come here. More importantly, home directories have two codenames: ~ : the home directory of the current user, and ~guest: the home directory of the user named guest.

/lib

There are a lot of libraries in the system, and /lib places libraries that are used when booting up, as well as those called by instructions under /bin or /sbin. What is a function library? You can think of it as a plug-in that some instructions must have in order to complete the execution of the program. Of particular importance is the directory /lib/modules/, because that’s where the core-related modules (drivers) are located.

/media

Media is the English word for media, and as the name implies, the device placed under this /media is removable. Devices including floppy disks, compact discs, DVDS and so on are temporarily mounted here. Common file names are: /media/floppy, /media/cdrom, etc.

/mnt

If you want to temporarily mount some additional devices, it is generally recommended that you place them in this directory. In ancient times, this directory was used for the same purpose as /media. However, with /media, this directory is used for temporary mounts.

/opt

This is a directory for third-party collaboration software. What is third-party collaboration software? For example, the KDE desktop management system is a standalone project, but it can be installed on a Linux system, so it is recommended that the KDE software be placed in this directory. In addition, if you want to install additional software that is not originally provided by Distribution, you can also install your software here. However, on previous Linux systems, we used to place it in /usr/local.

/root

Home directory of the system administrator (root) We put it here because if you mount only the root directory in single-person maintenance mode, that directory can have root’s home directory, so we want root’s home directory to be in the same partition as the root directory.

/sbin

Linux has a lot of instructions for setting up the system environment, which only root can use to set up the system, other users can only use to query. Under /sbin is required during the boot process, including the boot, repair, restore the system required instructions. For some server software programs, it is generally placed in /usr/sbin/. The system binary is put into /usr/local/sbin/. Common instructions include: fdisk, FSCK, ifconfig, init, MKFS, etc.

/srv

SRV is an abbreviation of Service. It is a directory of data that some network services need to access after they are started. Common services such as WWW, FTP and so on. For example, the web page information required by the WWW server can be placed in/SRV/WWW /. Hehe, it seems that the usual code we write should be put here.

/tmp

This is where ordinary users or executing programs can temporarily place files. This directory is accessible to anyone, so you need to clean it up regularly. Of course, important information cannot be placed in this directory. FHS even suggests deleting all files in/TMP at boot time.

In fact, the FHS standard for root directories is limited to the table above, but there are still some directories that we need to know about, as follows:

directory

File contents should be placed

/lost+found

This directory is created using the standard ext2/ext3 file system format to place missing fragments in the event of a file system error. This directory usually exists at the top of the partition slot. For example, if you add a hard drive to /disk, the system will automatically generate a directory like /disk/ Lost +found

/proc

The directory itself is a virtual filesystem. The data he places are in memory, such as system core and process information. , peripheral device status and network status, etc. Because the data in this directory is in memory, it does not take up any hard disk space. Important files (directories) such as /proc/cpuinfo, /proc/dma, /proc/interrupts, /proc/ioports, /proc/net/*, etc. Guest: Virtual memory [guest]?

/sys

This directory, very similar to /proc, is a virtual file system that records core-related information. Includes the currently loaded core modules and hardware device information detected by the core, etc. This directory also does not take up disk capacity.

In addition to the contents of these directories, it should also be noted that only the root directory will be mounted during the boot process because the root directory is related to the boot process. Other partitions will continue to be mounted after the boot process is complete. Because of this, directories related to the boot process in the root directory cannot be placed in a different partition from the root directory. Which directories cannot be separated from the root directory? Here are the following:

/etc/configuration file

/bin: important execution file

/dev: required device file

/lib: library of functions for the execution file and modules for the core

/sbin: important system execution file

These five directories must not be separated from the root directory in different partitions. Recite it, please.

/usr

According to the basic FHS definition, the data placed in /usr is shareable and static. If you know how to mount partitions across the network (such as NFS servers discussed in the Server section), Then /usr can indeed be shared with other hosts in the LAN.

Usr stands for Unix Software Resource. Usr is a directory where Unix Software resources are stored, not user data. Be aware of that. The FHS recommends that all software developers place their data in subdirectories within this directory, rather than creating their own directories.

This directory is similar to the C:\Windows\ + C:\Program Files \ directory on Windows. When the system is first installed, the C:\Windows\ + C:\Program Files \ This directory will take up the most disk capacity. In general, the recommended subdirectory for /usr is the following:

directory

File contents should be placed

/usr/X11R6/

The directory where X Window System important data is placed is named X11R6 because the last X release was version 11 and the sixth release of that release.

/usr/bin/

Most of the user-available instructions are located here. Note the difference between this and /bin. (Whether it is related to the boot process)

/usr/include/

Header and include files inc/C ++ and other programming languages, many of which are used when we install data in tarball mode (*.tar.gz).

/usr/lib/

A library of functions, object files, and execution files or scripts that are not commonly used by ordinary users. Some software provides special commands for server Settings, which are not often handled by system administrators, so they are placed in this directory. Note that if you are running Linux on X86_64, there may be /usr/lib64/

/usr/local/

The administrator installs downloaded software (not the default distribution provider) on the local PC. You are advised to install the software in this directory for easy management. For example, if you are distributing older software and you want to install the newer software but not remove the older version, you can install the new version in /usr/local/, which will be different from the old version. You can go to/usr/local, it also has the bin directory, etc, the include and lib… The time of the directory

/usr/sbin/

System instructions that are not required for proper operation of the system. The most common is the daemons of some web server software

/usr/share/

Where shared files are placed. Data placed in this directory is almost readable regardless of hardware architecture, because it is almost always text files. Other common subdirectories in this directory are: /usr/share/man: online help files

/usr/share/doc: file description of miscellaneous software items

/usr/share/zoneinfo: time zone file related to time zone

/usr/src/

The source code is recommended to be placed here. The core source code is recommended to be placed in /usr/src/linux/.

The meaning and content of /var

If /usr is the directory that takes up a lot of disk capacity during installation, /var is the directory that will gradually take up disk capacity after the system is running. Because the /var directory is mainly for files that change normally, including cache, log files and files generated by some software operations, including program files (lock file, run file), or files such as MySQL database, etc. Common subdirectories are:

directory

File contents should be placed

/var/cache/

Temporary archives that are generated during the operation of the application itself

/var/lib/

The program itself in the process of execution, the need to use the data files placed in the directory. Each software in this directory should have its own directory. For example, the MySQL database is placed in /var/lib/mysql/ and the RPM database is placed in /var/lib/rpm

/var/lock/

Some devices or file resources can only be used by one application at a time. If two applications use the device at the same time, errors may occur. Therefore, the device must be locked to ensure that the device is only used by a single application. For example, if the burner is burning a CD, do you think there are two people burning the same burner at the same time? If two people burn it at the same time, whose data is it writing? So while the first person is burning, the device will be locked, and the second person will have to unlock the device (that is, the previous person used up) before they can continue using it

/var/log/

Very important. This is the directory where the login files are placed. There are some important files such as /var/log/messages, /var/log/wtmp.

/var/mail/

The directory where your personal email address is placed, but this directory is also placed in the /var/spool/mail-/ directory, which is usually linked to each other.

/var/run/

Some programs or services will place their Pids in this directory after they start

/var/spool/

This directory usually holds a queue of data that is queued up for use by other programs. The data is usually deleted after it is used. For example, a new letter received by the system is placed in /var/spool/mail/, but once the user accepts the letter, it is deleted in principle. Messages that cannot be sent will be placed in /var/spool/mqueue/ and then deleted after they are sent. If it is scheduling data (crontab), it will be placed in the /var/spool/cron/ directory.

Since FHS only defines the file or directory data that should be placed in the topmost (/) and sublevel (/usr, /var) directory contents, the rest of the subdirectory levels are configurable at the developer’s discretion.

The directory tree is a directory tree.

Under Linux, all files and directories start at the root directory. It is the source of all directories and files, and then branches down one by one. Therefore, we also call this directory configuration method: directory tree. The main features of this directory tree are:

The starting point of the directory tree is the root directory (/, root).

Each directory can use not only the filesystem of the local partition, but also the filesystem of the network. For example, a Network File System (NFS) server can be used to mount a specific directory, etc.

Each file’s filename in the directory tree (including the full path) is unique.

If we graphically display the entire directory tree, with important file data listed, the directory tree architecture would look like this:

Absolute path and relative path

In addition to the FHS directory configuration that requires special attention, we also need to pay special attention to the file name section. The so-called path can also be defined as absolute and relative according to different file names. The two file names/paths are written as follows:

Absolute path:

A file name or directory name starting from the root directory (/), for example, /home/dmtsai/.bashrc;

Relative path:

File name relative to the current path. For example, / home/dmtsai or www.cnblogs.com/home/dmtsai… And so on. It doesn’t start with a theta, it’s a relative path

And you have to understand that the relative path is the relative position of your current path. For example, if you want to access /var/log from /home, what can you do?

cd /var/log (absolute)

cd .. /var/log (relative)

Since you are under /home, go back to the previous layer (.. /) to proceed to /var, pay special attention to these two special directories:

. : represents the current directory. It can also be a./.

. : represents the upper level of the directory, can also.. / to represent.

This is related to… The catalog concept is very important, you will often see CDS.. Commands, such as./command, represent the working state of the previous layer and the current directory.

Example 1: How to enter the /var/spool/mail/ directory and then the /var/spool/cron/ directory?

Command:

cd /var/spool/mail

cd .. /cron

Description:

Because /var/spool/mail and /var/spool/cron are in the same directory as /var/spool/. This eliminates the need to write from the root directory. This relative path can be very helpful, especially for some software developers. Typically, software developers place data in relative directories within /usr/local/. But what if the user wants to install to a different directory? You have to use relative paths.

Example 2: Network files often refer to data such as./run.sh. What is the significance of this instruction?

Description:

Since the execution of the directive requires variable support, if your executable file is in this directory and it is not a formal executable directory (/bin, /usr/bin, etc.), it must be strictly specified to execute the directive. ./ indicates the name of the directory, so./run.sh indicates the execution of the run.sh file in the directory.

From:

One Linux command per day (23) : Linux directory structure

Birdman’s Linux Home Dish