Linux file system entry details

  
                

There are thousands of files in the computer system. If they are not classified, they will become messy and inconvenient to use. The file system can sort all kinds of files very well. Let's take a closer look at the Linux file system and learn together.

For computers, the so-called data is a sequence of 0 and 1. Such a sequence can be stored in memory, but the data in memory will disappear with the shutdown. In order to save the data for a long time, we store the data on a CD or hard disk. According to our needs, we usually save the data separately to a small unit such as a file (so-called small, relative to all data). But if the data can only be organized into files, but can not be classified, the files will still be chaotic. Every time we search for a file, we have to check one file and one file, which is too much trouble. A file system is a logically organized form of a file that stores individual files in a clearer way.

Introduction to Paths and Files

Files are organized into file systems and usually become a tree structure. Linux has a root directory /, which is the top of the tree structure. The end of the fork of the tree represents a file, and the fork of the tree is a directory (the equivalent of the folder we see in the windows interface). What you see in Figure 1 is the entire file tree. If we take a part of the tree, for example, starting from the directory vamei, it actually constitutes a file system.

To find a file, in addition to knowing the file name of the file, you need to know all the directory names from the root of the tree to the file. The directory name and file name of all paths starting from the root directory form a path. For example, we look for a file file.txt in Linux, not only the file name (file.txt), but also the full path, which is the absolute path (/home/vamei/doc/file.txt). From the top of the root directory /, that is, the top of the tree structure, after the directory home, vamei, doc, finally see the file file.txt. The entire file system is hierarchical (hierarchy), vamei is a subdirectory of home, and home is the parent directory of vamei.

In Linux, we use the ls command to display all files in a directory, such as $ ls /home /vamei /doc

1 file tree

As illustrated in the The file system shown in it is the tree of green. At the top of the root directory (/), along the path marked by the red arrow, we finally found the file file.txt.

Directory

On Linux systems, directories are also a type of file. So /home/vamei is the absolute path to the directory file vamei.

This file contains at least the following entries:

The code is as follows:

. Point to the current directory

. . Point to the parent directory

In addition, the directory file also contains the file name of the file belonging to the directory. For example, vamei also has the following entries, pointing to the files belonging to the directory:
< The code is as follows:

doc

movie

photo

The way Linux interprets an absolute path is as follows: first find the root directory file, from that directory The location of the home directory file is read in the file, and then the location of vamei …… is read from the home file until the location of file.txt in the directory doc is found.

Because it is in the catalog file. with. . The entry we can add in the path. or. . To indicate the current directory or parent directory, such as /home/vamei/doc/. . Equivalent to /home/vamei.

In addition, Linux maintains a variable in the working directory of the process. In the shell, you can always query the working directory (in the command line, type $pwd). This is to save the trouble of entering a long absolute path each time. For example, if we change the working directory to /home/vamei ($cd /home/vamei), then we can go to file.txt and save /home/vamei/($ls doc/file.txt). The path thus obtained is called a relative path. The above doc/file.txt is such a relative path.

When a file appears in a directory file, we insert the file into the file system. We call it a hard link to the file. A file is allowed to appear in multiple directories, so that it has multiple hard links. When the number of hard links (link count) is reduced to 0, the file will be deleted by Linux. So many times, unlink and remove are a meaning in the Linux operating system. Due to the widespread use of soft links (soft links do not affect link count and can span file systems), it is now less likely to manually establish hard links.

File Operations

For files, we can read (write), write (write) and run (execute). Reading is to get data from an existing file. Write is to write data to a new file or an old file. If the file stores executable binary code, it can be loaded into memory and run as a program. In a Linux file system, if a user wants to perform an operation on a file, the user must have permission to perform the operation on the file. Information about file permissions is stored in the file information (metadata), see the next section.

File Additional Information (metadata)

The file itself contains only data. The file name is actually stored in the directory file. In addition to these, there are file additional information maintained by the operating system, such as file type, file size, file permissions, file modification time, file read time, and so on. You can use the ls command to query the file information ($ls -l file.txt) and get the following result:

The code is as follows:

-rw-r--r-- 1 vamei vamei 8445 Sep 8 07:33 file1.txt

The meaning of each part is as follows:

1. Let us introduce the initial -, which indicates the file type, indicating that file1.txt is a regular file (if it is Directory files should display d).

2. Then there are nine characters, rw-r--r--, which are used to indicate file permissions. These nine characters are divided into three groups, rw-, r--, r--, corresponding to the owner, owning group and all others. Looking back at Linux boot, after logging in, I will have a user identity and a group identity, which is equivalent to my business card. The first group indicates that if the user identity on my business card proves that I am the owner of the file, then I can read (r) the file, write (w) the file permissions, but do not have the implementation. (-, if you have execute permission, x) the permissions of the file. The second group indicates that if the group identity on my business card proves that my group is a member of the file's own group, then I have permission to read from the file. The third group said that if my business card shows that I am neither the owner nor a member of the group, then I only have permission to read. When I want to do a read operation, Linux will first see if I am the owner. The following explains the owner and owning the group further.

3. The next 1 is the number of hard links (link count).

4. After vamei indicates that the user vamei is the owner of the file (owner), the owner of the file has the right to change the file permissions (for example, changed to rwxrwxrwx). The possession group of the latter vamei file is the group vamei. The owner and owner of the file are attached to the file when it is created (equivalent to locking the file, only users with the appropriate business card can open the operation). Note that Linux has a superuser root (also known as the root user) that has all the files.

5. The following 8445 indicates the file size in bytes.

6.Sep 8 07:33 indicates the last modification time of the file (modification time). In fact, the file attachment information also contains the last access time of the file, which is not displayed.

Soft link, or symbolic link

As mentioned in the discussion of hard links, soft links do not affect the link count of a file. If you remember the shortcuts of the windows system, the soft link (soft link, also called symbolic link) of Linux is a shortcut to linux. A soft link is essentially a file whose file type is a symbolic link. In this file, there is an absolute path to the file pointed to by the link. When you read data from this file, Linux will direct you to the file you are pointing to and then read from that file (just as if you double-clicked on the shortcut). Soft links can be easily created anywhere and point to any absolute path.

The soft link itself is also a file, and it can also perform the operations that the file can do. When we operate on soft links, we should pay attention to whether we operate on the soft link itself or on the target operation pointed to by the soft link. If it is the latter, we say that the operation follows the link.

umask

When we create a file, such as touch, it will try to create a new file as permission 666, which is rw-rw-rw-. But the operating system should refer to the permission mask to see if the file is actually created as 666. The permission mask indicates the permission bits that the operating system does not allow. For example, the permission mask of 037 (----wxrwx) means that the wx bit of the group and the rwx bit of the other are not allowed to be set. If this is the permission mask, the final file permission is rw-r----- (the w bit of group and the rw bit of other are masked).

We can change the permission mask by

as follows:

$umask 022


Summary

Computers are essentially tools for processing data, and files are the logical carrier for data storage, so it's important to understand the Linux file system. The understanding of the file system should be organically combined with other aspects of Linux (such as user management).

File permissions, owner, own group, super user root

hard link, soft link, follow the link

The above is a detailed introduction of the Linux file system, if Without a file system, files can be very cumbersome to find and increase the difficulty of working. Now you have a deeper understanding of the Linux file system?

Copyright © Windows knowledge All Rights Reserved