A file system in Linux and UNIX is a tree file structure rooted in /. / is the root directory and the starting point of the file system in Linux and UNIX. All files and directories are located in the/path, including /usr, /etc, /bin, /home, etc. In the early days of UNIX, each vendor defined its own file system naming structure, which was confusing and difficult to distinguish.
FSSTND(FileSystem Standard), a Linux FileSystem hierarchy Standard, was introduced in 1994 in order to avoid this confusion in Linux systems. Later, THE UNIX team promoted FSSTND. It became the FHS(FileSystem Hierarchy Standard).
The FHS standard causes many Linux distributions to have a uniform file system naming standard, which is another way of saying it: FHS is a file system naming standard. In general, Linux distributions are required to follow FHS regulations
- Directory structure and directory content
- Permissions for file types
However, FHS only defines two layers of specification. The first layer is what file data should be stored in each directory under /etc, for example, setting files should be placed under /bin and executable files should be placed under /sbin. The second layer is defined for /usr and /var. For example, /usr/share stores shared data, and /var/log stores system login files.
Here are some definitions and specifications for Linux under /.
The FHS specification
The specifications I describe below are only part of the directories and files specified by the FHS documentation; the full standard is available online at www.pathname.com/fhs/.
First, let’s introduce the importance and significance of the root directory:
Root directory is the most important thing in the whole system a directory, all subsequent directory is based on the root directory of derivatives, more importantly, the root directory is related to boot, restore, system repair, because the system boot need specific environment, so the system error, the root directory must also contains can repair the file system program. So the FHS rules don’t set the partition size of the root directory to too large, in fact, as small as possible to reduce the error rate.
The FHS also specifies that these directories should exist under the root directory.
The/home directory
The /home directory is the default user home directory.
The/boot directory
The /boot directory contains the static files needed to start the operating system, such as the Linux kernel, which are critical to boot the system. The common file for the Linux Kernel is vmlinuz, but if you use grub2, there is also a directory called /boot/grub2.
The Linux I just set up looks like this under boot.
Do not delete the /boot directory. Doing so may cause the system to fail to start.
/ dev directory
The /dev directory is a collection of device nodes, which are devices in Linux or virtual devices provided by the kernel. These device nodes are also critical to the proper operation of the system. The devices in the /dev directory and subdirectories are character devices and block devices. The character device is the mouse, keyboard, and modem, and the block device is the hard disk and floppy disk drive. Storing files in /dev is equivalent to storing a device.
Some of the more important documents are
/dev/hda: The main device on the main IDE channel, which is the channel through which the first and second drives connect to the motherboard or interface card when referring to IDE/ATA hard disks.
/dev/hdb: secondary device on the main IDE channel.
/dev/tty0: the first virtual console. The virtual console, also known as VC or VIRTUAL terminal VT, is a combination of a keyboard and a monitor used for computer user interface.
/dev/tty1: indicates the second virtual console.
/dev/sda: first device on the main SCSI and SATA channels.
/dev/lp0: the first parallel port.
Here is my directory under Linux.
The/etc directory
The /etc directory is reserved for local configuration files of the computer. Major configuration files of the system are stored in this directory, such as account password and service start and stop. Generally speaking, only the root user has the read permission under this directory, and only the root user has the modification permission. You can read the author’s previous article to finish this article Linux permissions, transparent!
The most important files in /etc are
/etc/modprobe.d Kernel driver module
/etc/passwd Specifies the password of the storage user
/etc/fstab stores mounting information
/etc/issue What is displayed when the storage system is started
Do not store binary files in /etc. Binary files are best stored in /sbin and /bin.
The /etc/x11 directory contains two important files. /etc/x11 is used to store X Window system configuration files, such as xorg.conf, and /etc/opt is used to store configuration files related to /opt.
/ lib directory
The /lib directory acts as a repository for libraries needed to execute binaries in /bin and /sbin. These shared library images are especially important for boot and executing commands in the root file system.
/ media directory
Media is media, which means that /media can hold installation points and subdirectories for removable media such as USB, DVD, CD-ROM, and Zip disks.
The/MNT directory
If you want to temporarily mount some additional devices, you can put them in the/MNT directory. In the beginning, / MNT was used for the same purpose as /media, but with /media, this directory was used for temporary mount.
The/opt directory
The /opt/ directory provides storage space for most application packages, and packages that place files in the /opt/ directory create a directory with the same name as the package. This directory, in turn, holds files that would otherwise be scattered throughout the file system, giving system administrators an easy way to determine the role of each file in a particular package.
For example, if sample is the name of a specific package in the /opt/ directory, then all files are placed in the directory in the /opt/sample/ directory, For example, /opt/sample/bin/ indicates the binary file and /opt/sample/man/ is used for the manual page.
The/proc directory
The /proc directory itself is a virtual filesystem that places data in memory, such as system core information, travel information, and network status of peripheral devices. Because the data in this directory is in memory, it does not take up disk space.
The most important files in this directory are /proc/cpuinfo, /proc/dma, /proc/interrupts, and /proc/net/.
/ sbin directory
The /sbin directory stores executable files used by the root user. The /sbin executable file is used during startup to manage and restore the system. In addition to the binaries in /bin, /sbin contains the binaries needed to start, recover, and repair the system.
The /sbin directory contains arp, clock, HALT, init, GRUB, ifconfig, route, and shutdown.
The/TMP directory
This directory is used to store temporary files that are cleared when the system restarts.
/ sys directory
Similar to /proc, this directory is a virtual file system that records core and system hardware information. This directory also takes no disk space.
The/usr directory
Usr stands for Unix Software Resource. FHS recommends that Software developers place their data properly in a subdirectory of this directory. Don’t create your own software-independent directory.
All system default software is placed under /usr, so this directory is similar to the C:\Windows\ + C:\Program Files \ directory in Windows. Generally, this directory takes up more disk capacity after the system is installed. The recommended subdirectory for /usr has the following contents
*/usr/bin/* All common user commands are stored in this directory. Since CentOS 7, /usr/bin commands are identical to /bin commands.
*/usr/lib/* basically has the same functionality as /lib, so /lib is linked to this directory.
/usr/local The system administrator is advised to install all software downloaded from the local PC in this directory for easy management.
/usr/sbin is a system command that is not required for the normal operation of the system. The most common command is the service command of some network server software, but its basic function is similar to /sbin, so /sbin is linked to this directory.
/usr/share/man, /usr/share/doc, and /usr/share/zoneinfo are subdirectories in this directory
/usr/games where data related to the game is placed
/usr/include/c /C++ header and include placement is also directly linked.
The /usr/src common source code is recommended to be placed here, while the core source code is recommended to be placed in the /usr/src/linux directory.
/ run directory
The previous FHS system specified that all information generated after the system is booted should be placed in the /var/run directory. The new FHS system specifies that the information generated after the system is booted should be placed under /run.
The/var directory
If /usr is the directory that takes up a lot of disk capacity during installation, /var is the directory that gradually takes up disk capacity after the system is running. /var mainly includes cache, login files, and directories generated by certain software running, including program files, such as MySQL database files. Common directories include
/var/cache Specifies the directory for the application cache
/var/crash System error information
/var/log Records logs
/var/run Identifies the process
/var/tmp Temporary file directory
/var/lock Records of file locking
/ the SRV directory
SRV can be regarded as the abbreviation of Service. It is the data directory that these services need to access after starting some network services, such as WWW and FTP.
CentOS 7 has a different arrangement of directories than previous versions, as outlined above. The major difference is that many directories that should have been in the root directory have all their data moved to /usr
- /bin -> /usr/bin
- /sbin -> /usr/sbin
- /lib -> /usr/lib
- /lib64 -> /usr/lib64
- /var/lock -> /run/lock
- /var/run -> /run
In order to make it easier for you to see what these directories do, I drew a diagram by hand. The relationship between these directories in the diagram resembles a tree, so this directory is also called a directory tree.
The directory tree is already clear, so there is nothing more to say.
Absolute path and relative path
In Linux, as in other systems, paths are absolute and relative. The terms are described as follows:
- All paths starting from the root directory are absolute paths, such as /boot and /usr/local in the preceding directory tree
- Is written relative to the current file path, such as.. The/var/log directory
Note that relative paths are moved relative to your current path, for example if you are in /boot and want to move to /usr/src
Absolute path representation: /usr/src
Relative path notation:.. /usr/src
There are also two special directories to note:
- . : indicates the current directory. It can also be a./
- . : represents the directory at the next level. You can also use.. / to represent
Relative paths are recommended when the file path is very long, because you don’t have to write all the paths like absolute paths. Instead, you can use… / can;
But in terms of the correctness of file paths, absolute paths are better than relative paths.
If you are writing a program to manage the system, use absolute paths because absolute paths will find the specified file no matter where your file path is. However, if the relative path is moved, the file may not be found.
Directory-related directives
Let’s talk about a few directory-related commands
cd
CD is short for change Directory. This command can switch the current working directory in the following four ways
CD/pathname # indicates switching to the specified working directory CD./ # indicates switching to the current working directory (without any switching) CD.. CD ~ # indicates to return to the current user directoryCopy the code
Let’s demonstrate the operation of the four CD switching modes
As you can see, we switched to the command directory using CD /usr/local respectively; Use CD./ did not switch any directories (wasted two seconds); Using the CD.. Switch to one level above the current working directory; Use CD ~ to switch to the default working directory for the current logged-in user.
The important thing to note here is that CD ~, if you use CD directly, the switching effect is the same as CD ~, that is, “CD ~ == CD”.
pwd
The PWD command is the one we use most often, and it is used almost as often as a CD command because a CD command is usually followed by a PWD command, so you can see how important the PWD command is.
PWD is short for Print Working Directory. It is the command to Print out the current Directory.
However, PWD is not only used to display the current working path, it can also be used to display the file path under the specified working directory
We already know the path to the working directory, so why print it out?
mkdir
Mkdir is used to create a new directory, which is short for make directory and is generally followed by two arguments.
- -m: Sets the file permission directly, regardless of the default file creation permission
- -p: creates a directory recursively
Here is how to create a file directory using mkdir
As shown above, we created an empty directory called test1 under the/TMP file directory and printed out the current working directory.
If you want to create directories that you do not want other users to access, you can use the following command
mkdir -m 711 test2
Copy the code
In the figure above, ls-ld lists the details of the file directory. You can see that the first detail is the permission of the file.
Now we want to create these directories quickly. For example, if we want to create /test1/test2/test3/test4, we use the following code
Linux says we can’t do this.
This is where the -p argument comes in, which allows us to recursively create directories, as shown below
But using -p is not recommended because it’s easy to typo…
rmdir
Rmdir is used to delete empty directories. It is very similar to mkdir. Mkdir -p is used to recursively create directories, and rmdir -p can recursively delete directories. As shown in the figure below
Note that rmdir only deletes an empty directory, which means that it cannot be deleted if there is content in the directory. You can use rm -r directly to delete the file.
mv
Using mv allows you to change the name of a directory. For example, we created a directory called test. Later, we decided that the directory did not reflect its owner, and we wanted to change its name.
After understanding the basic directory operation, let’s think about a question, why can we execute the ls command in various directories can display content? Shouldn’t ls only be available in its storage path?
First of all, we need to understand the concept of PATH. PATH is an environment variable. When we execute a command, the system will first search for the executable file named LS under each PATH according to the setting of PATH.
It took so long to learn these concepts with Linux…