A linux command a day (23): Linux directory structure

For every Linux learner, understanding the directory structure of the Linux file system is a crucial step in learning Linux well. An in-depth understanding of the standard of the Linux file directory structure and the detailed functions of each directory is essential for us to make good use of the Linux system. As important as it is, let's start to understand the relevant knowledge of the linux directory structure.
When using Linux, if you pass ls –l /, you will find that there are many directories under /, such as etc, usr, var, bin, etc. In these directories, we Go in and have a look and find that there are also many directories or files. The file system looks like a tree structure under Linux, so we can call the structure of the file system a tree structure.
The file system is used to organize and arrange file access, so it is visible. In Linux, we can view its structure through tools such as ls. In Linux system, all we see are tree structures; For example, the operating system is installed in a file system, which is represented by a tree structure starting with /. The top of the Linux file system is /, we call / the root of Linux, which is the file system of the Linux operating system. The entry of the Linux file system is /, and all directories, files, and devices are under /. / is the organizer of the Linux file system and the highest-level leader.
Since linux is an open source code, major companies and groups do their own operations and programming according to the core code of linux. This creates a difference in the directories under the root. In this way, individuals cannot use the PCs of other people's linux systems. Because you simply don't know some basic configuration, where are the files. . . This creates confusion. This is why the FHS (Filesystem Hierarchy Standard) institution was born. This organization is a group formed spontaneously by linux enthusiasts, mainly to make some basic requirements for linux, so that the operator will not become 'illiterate' of linux by changing a host.
According to the official document of FHS (http://www.pathname.com/fhs/), their main purpose is to let users know that the installed software is usually placed in that directory, so they want independent software development Businesses, operating system makers, and users who want to maintain systems can all follow FHS standards. In other words, the focus of FHS is to regulate what kind of data should be placed in each specific directory. This is very beneficial, because the Linux operating system can develop the unique style that the developer wants under the existing appearance (the directory structure remains unchanged).
In fact, FHS has been continuously revised based on past experience. FHS defines the directory into four interactive forms according to the frequent use of the file system and whether users are allowed to change it at will. A bit like

the following: shareable (shareable)
unshareable (unshareable)
immutable (static)
/usr (software placement)
/etc (configuration files)
/opt (third-party software)
/boot (boot and core ) file)
variable (variable)
/var/mail (user mailbox)
/var/run (program related)
/var/spool/news (newsgroup)
/var/lock (program related)
#p#page title# e#

Four types:
1. Shareable:
The directory that can be shared with other systems for mounting, so including executable files and user emails and other data, is a directory that can be shared with other hosts on the network for mounting;
2. Not available Shared:
Device files running on your own machine or socket files related to programs, etc. are only related to your own machine, so of course they are not suitable for sharing with other hosts.
3. Immutable:
Some data will not change frequently, and will not change with distribution. For example, the function library, the file description file, the host service configuration file managed by the system administrator, etc.;
4. Changeable:
data that changes frequently, such as login files, newsgroups that general users can accept by themselves, etc.

In fact, FHS only defines what data should be placed under the three-level directory for the directory tree structure, which are the definitions of the three directories below:
/ (root, root directory): related to the boot system;
/usr (unix software resource ): related to software installation/execution;
/var (variable): related to system operation.

1. The meaning and content of the root directory (/):
The root directory is the most important directory in the entire system, because not only all directories are derived from the root directory, but also the root directory is also associated with actions such as boot/restore/system repair related. Since the system needs specific boot software, core files, programs required for boot, function libraries and other file data when the system starts up, if the system fails, the root directory must also contain a program that can repair the file system. Because the root directory is so important, in terms of the requirements of FHS, he hopes that the root directory should not be placed in a very large partition, because the larger the partition, the more data you will put in, so that the partition where the root directory is located will be There may be more opportunities for errors to occur.
Therefore, the FHS standard recommends that the partition where the root directory (/) is located should be as small as possible, and the software installed by the application should not be placed in the same partition as the root directory. Keep the root directory as small as possible. Not only does this provide better performance, but the file system where the root directory is located is less prone to problems. #p#Paging title#e# To put it bluntly, the root directory is the same as the C drive of Windows.
Based on the above reasons, FHS believes that the root directory (/) should contain the following subdirectories:
Directory The file content
should be placed in
/bin
The system has many directories for placing executable files, but /bin is special. Because /bin places instructions that can still be operated in single-person maintenance mode. Commands under /bin can be used by root and general accounts, mainly: cat, chmod (modify permissions), chown, date, mv, mkdir, cp, bash and other commonly used commands.
/boot
mainly stores files that will be used for booting, including Linux kernel files, boot menus, and configuration files required for booting. The commonly used file name of Linux kernel is: vmlinuz. If the boot management program grub is used, the directory /boot/grub/ will also exist.
/dev
On Linux systems, all devices and peripherals exist in this directory in the form of files. By accessing a file in this directory, it is equivalent to accessing a device. More important files are /dev/null, /dev/zero, /dev/tty , /dev/lp*, /dev/hd*, /dev/sd* etc.
/etc
Most of the system's main configuration files are placed in this directory, such as personnel's account and password files, startup files of various services, and so on. Generally speaking, the attributes of each file in this directory can be viewed by ordinary users, but only root has the right to modify it. FHS recommends not placing executable files (binary) in this directory. The more important files are: /etc/inittab, /etc/init.d/, /etc/modprobe.conf, /etc/X11/, /etc/fstab, /etc/sysconfig/ and so on. In addition, the important directories under it are: /etc/init.d/ : The preset startup scripts of all services are placed here, for example, if you want to start or close iptables: /etc/init.d/iptables start, / etc/init.d/ iptables stop
/etc/xinetd.d/ : This is the configuration file directory of various services managed by the so-called super daemon.
/etc/X11/ : Various configuration files related to X Window are here, especially xorg.conf or XF86Config, the two X Server configuration files.
/home
This is the user's home directory by default. When you add a general user account, the default user home directory will be standardized here. More importantly, the home directory has two codes:
~ : represents the home directory of the current user, and ~guest: represents the home directory of the user named guest.
/lib
There are a lot of function libraries in the system, and /lib places the function libraries that will be used at boot time, and the function libraries that are called by the commands under /bin or /sbin. What is a library? You can think of it as a plug-in, and certain instructions must have these plug-ins to successfully complete the execution of the program. Especially important is the /lib/modules/ directory, because this directory will place the core-related modules (drivers).
/media
media is the English word for media. As the name suggests, the removable device is placed under this /media. Devices including floppy disks, CDs, DVDs, etc. are temporarily mounted here. Common file names are: /media/floppy, /media/cdrom and so on.
/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. Only after /media is available, this directory is used for temporary mounting.
/opt
This is the directory for third-party third-party software. What is third-party software? For example, the KDE desktop management system is a separate project, but it can be installed on Linux systems, so KDE software is recommended to be placed in this directory. Also, if you want to install additional software yourself (not provided by the original distribution), you can also install your software here. However, in the previous Linux system, we were still used to placing it in the /usr/local directory.
/root
The home directory of the system administrator (root). The reason why it is put here is because if you enter the single-person maintenance mode and only mount the root directory, the directory can have the root's home directory, so we will want the root's home directory and the root directory to be placed in the same partition.
/sbin
Linux has a lot of commands to set the system environment, these commands can only be used by root to set the system, and other users can only be used to query at most. The ones placed under /sbin are needed during the boot process, and include the commands needed to boot, repair, and restore the system. As for some server software programs, they are generally placed in /usr/sbin/. As for the system binary generated by the software installed by the machine itself, it is placed in /usr/local/sbin/. Common commands include: fdisk, fsck, ifconfig, init, mkfs, etc.
/srv
srv can be regarded as the abbreviation of service, which is the data directory 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 data required by the WWW server can be placed in /srv/www/. Oh, it seems that the code we usually write should be put here.
/tmp
This is a place for general users or programs that are running to 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. Because FHS even recommends that the data under /tmp should be deleted when booting.
#p#Pagination title#e#

In fact, the standards defined by FHS for the root directory are limited to the above table, but there are still some directories that we need to understand, as follows: The

directory
should place the file content
/lost+found
This directory is using the standard A directory generated only by the ext2/ext3 file system format. The purpose is to place some missing fragments in this directory when the file system fails. This directory usually exists at the top level of the partition slot. For example, if you add a hard disk to /disk, a directory like this /disk/lost+found will be automatically generated under this system.
The /proc
directory itself is a virtual filesystem. The data he places are all in memory, such as the system core, process information (process) (is it a process?), the status of peripheral devices and network status, and so on. Because the data in this directory is in the memory (memory), it does not occupy any hard disk space. More important files (directories) such as: /proc/cpuinfo, /proc/dma, /proc/interrupts, /proc/ioports, /proc/net/* and so on. Oh, is it virtual memory [guest]?
The /sys
directory is actually very similar to /proc. It is also a virtual file system that mainly records information related to the core. Including currently loaded core modules and hardware device information detected by the core, etc. This directory also does not occupy the hard disk capacity.
#p#Page title#e#

In addition to the contents of these directories, it should also be noted that because the root directory is related to booting, only the root directory will be mounted during the booting process, and other partitions will be mounted after the booting is completed. The behavior of persistent mounts. Because of this, the directory related to the boot process under the root directory cannot be placed in a different partition from the root directory. Which directories cannot be separated from the root directory? There are the following:
/etc: configuration files
/bin: important executable files
/dev: required device files
/lib: function libraries required by executable files and modules required by the core
/sbin: important system executable files
These five The directory must not be separated from the root directory in a different partition. Please take it down.

2. The meaning and content of /usr:
According to the basic definition of FHS, the data placed in /usr is shareable and immutable (shareable, static). If you know how to mount partitions through the network (such as the NFS server mentioned in the server chapter), Then /usr can indeed be shared with other hosts in the local area network for use.
/usr is not the abbreviation of user, in fact, usr is the abbreviation of Unix Software Resource, that is, the directory where the software resources of the Unix operating system are placed, not the user's data. Pay attention to this. FHS recommends that all software developers should reasonably place their data into subdirectories under this directory, rather than creating their own independent directory for the software.

Because all system default software (software provided by the distribution publisher) will be placed under /usr, this directory is somewhat similar to the combination of the two directories C:\Windows\ + C:\Program files\ of Windows system, When the system is just installed, this directory will occupy the most hard disk capacity. Generally speaking, the subdirectories of /usr are suggested to have the following: the
directory where the file content
should be placed
/usr/X11R6/
is the directory where the important data of the X Window System is placed, the reason why it is named X11R6 is because the last X version is the 11th edition, and the meaning of the sixth release of that edition.
/usr/bin/
Most of the user-available instructions are placed here. Note how it differs from /bin. (Whether it is related to the boot process)
/usr/include/
c/c++ and other programming languages ​​where the headers and include files are placed, when we install the software in tarball mode (*.tar.gz way to install software) For some data, many include files are used.
/usr/lib/
It includes function libraries, object files, and executable files or scripts that are not used by ordinary users. Some software will provide some special instructions to set the server, these instructions will not be frequently operated by the system administrator, so they will be placed in this directory. It should be noted that if you are using an X86_64 Linux system, there may be a /usr/lib64/ directory to generate the
/usr/local/
system administrator to install the software downloaded by himself on the machine (non-distribution default provider) , it is recommended to install to this directory, which will be easier to manage. For example, the software provided by your distribution is old, and you want to install the newer software but do not want to remove the old version. At this time, you can install the new version of the software in the /usr/local/ directory, which is compatible with the original old version. There is a difference. You can go to /usr/local to see by yourself, this directory is also the sub-directory
/usr/sbin/ with bin, etc, include, lib... for
the system instructions required for the normal operation of the non-system. The most common is the place where the service instruction (daemon)
/usr/share/ of some network server software
places shared files. The data placed in this directory is almost readable data regardless of hardware architecture, because almost all Text file. Commonly found in this directory are these subdirectories: /usr/share/man: online help files
/usr/share/doc: documentation for miscellaneous software
/usr/share/zoneinfo: time zone files related to time zones

/usr/ src/
General source code is recommended to be placed here, src means source. As for the core source code, it is recommended to place it in the /usr/src/linux/ directory.
#p#Pagination title#e#

3. The meaning and content of /var:
If /usr is a directory that will occupy a larger hard disk capacity during installation, then /var is a directory that will gradually occupy the hard disk capacity after the system is running. Because the /var directory is mainly for regularly changing files, including caches, log files, and files generated by certain software operations, including program files (lock files, run files), or for example MySQL database files. documents, etc. Common subdirectories are:
Directory Where the file content
should be placed
/var/cache/
Some temporary archives that will be generated during the operation of the application itself
/var/lib/
During the execution of the program itself, the directory where the data files that need to be used are placed. 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 to
/var/lock/
Some devices or file resources can only be accessed by one application at a time. If there are two programs using the device at the same time, some error conditions may occur, so the device has to be locked to ensure that the device is only used by a single software. For example, the recorder is burning a disc. Think about it, will there be two people using the same recorder to burn the disc at the same time? If two people burn at the same time, whose data is written to the film? So it's very important that the recorder will be locked while the first person is burning, and the second person will have to unlock the device (that is, the first person has used up) before they can continue to use
/var/log/
. This is the directory where the login files are placed. There are more important files such as /var/log/messages, /var/log/wtmp (recording the information of the login person) and so on.
/var/mail/
Directory for personal e-mail mailboxes, but this directory is also placed in the /var/spool/mail/ directory, usually these two directories are linked files.
/var/run/
After some programs or services are started, their PIDs will be placed in this directory.
/var/spool/
This directory usually places some queue data. The so-called "queue" is queued for data used by other programs. . These data are usually deleted after use. For example, when the system receives a new letter, it will be placed in /var/spool/mail/, but after the user accepts the letter, the letter will be deleted in principle. If the letter cannot be sent temporarily, it will be placed in /var/spool/mqueue/, and it will be deleted after it is sent. If it is job scheduling data (crontab), it will be placed in the /var/spool/cron/ directory.
#p#Paging title#e#

Because FHS only defines the file or directory data that should be placed in the directory contents of the top level (/) and sublevel (/usr, /var), therefore, in other subdirectory levels, It can be configured by the developer.

4. Directory tree:
Under Linux, all files and directories start from the root directory. That is the source of all directories and files, and then branch 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 Root directory (/, root);
each directory can use not only the filesystem of the partition on the local side, but also the filesystem on the network. For example, a specific directory can be mounted using a Network File System (NFS) server.
The filename (including the full path) of each file in this directory tree is unique.

If we display the entire directory tree as a diagram and list the more important file data, then the directory tree structure is as shown in the following figure:

5. Absolute path and relative path
In addition to the FHS directory configuration that requires special attention , we should also pay special attention to the file name part. Because the so-called path (path) can also be defined as an absolute path (absolute) and a relative path (relative) according to the different ways of writing the file name. The two file names/paths are written as follows:
Absolute path:
the file name or directory name starting from the root directory (/), such as /home/dmtsai/.bashrc;
relative path:
relative to the current path File name notation. For example ./home/dmtsai or http://www.cnblogs.com/home/dmtsai/ etc. Anyway, the beginning of a / is not a relative path,
and you must understand that a relative path is represented by the relative position of your current path. For example, if you are currently in the /home directory, if you want to enter the /var/log directory, how can you write it? #p#Page title#e#
cd /var/log (absolute)
cd ../var/log (relative)
Because you are under /home, you have to go back to the previous layer (../) before continuing to / var to move, pay special attention to these two special directories:
. : Represents the current directory, which can also be represented by ./;
.. : Represents the upper-level directory, or ../.
The concept of . and .. directories is very important. You often see commands such as cd .. or ./command, which represent the working status of the previous layer and the current directory.

Example 1: How to enter the /var/spool/mail/ directory first, and then enter the /var/spool/cron/ directory?
Command:
cd /var/spool/mail
cd ../cron
Description:
Because /var/spool/mail and /var/spool/cron are the same in the /var/spool/ directory. In this way, there is no need to start writing from the root directory. This relative path is very helpful, especially for some software developers. Typically, software developers place data in relative directories in /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 mention data like ./run.sh. What is the meaning of this command?
Note:
Since the execution of the instruction requires the support of variables, if your executable file is placed in this directory, and this directory is not a regular executable file directory (/bin, /usr/bin, etc. are regular), you need to execute the instruction at this time. To strictly specify the executable file. ./ represents the meaning of this directory, so ./run.sh represents the execution of the file named run.sh in this directory.
      Reference: "Brother Bird's Linux Private Kitchen"


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326710206&siteId=291194637