Linux File Management Background

Source: Internet
Author: User

For computers, the so-called data is the sequence of 0 and 1. Such a sequence can be stored in the memory, but the data in the memory disappears with the shutdown. To save the data for a long time, we store the data on a CD or hard disk. Based on our needs, we usually save data separately to small units such as files (small, relative to all data ). However, if the data can only be organized as files, but cannot be classified, the files will still be disorganized. Every time we search for a file, it is too troublesome to check one file after another. A file system is a logical organization of files. It stores each file in a clearer way.

 

1. Introduction to paths and files

A file is organized into a file system, which usually becomes a tree structure. Linux has a root directory/, that is, the top of the tree structure. The end of the tree's Branch represents a file, and the branch of the tree is a directory (directory, equivalent to the folder we see on the windows interface ). Figure 1 shows the entire file tree. If we extract a part of the tree, for example, starting from the directory vamei, it actually forms a file system.

To find a file, you must know the file name and all the directory names from the root to the file. The directory names and file names of all paths starting from the root directory constitute a path ). For example, if you want to find a file.txt file in linux, you must not only know the file name (file.txt), but also know the complete path, that is, the absolute path (/home/vamei/doc/file.txt ). Starting from the root directory, that is, the top of the tree structure, the file.txt file is viewed at the end of the Directory home, vamei, and docs. The entire file system is hierarchical (hierarchy). vamei is the subdirectory of home, while home is the parent directory of vamei.

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

Figure 1 file tree

 

(As shown in the figure, the file system is a green tree. The top root directory (/folder, marked in red, we find file.txt at the end .)

 

2. Directory

In Linux, a directory is also a file. Therefore,/home/vamei is the absolute path pointing to the directory file vamei.

This file contains at least the following entries:

. Pointing to current directory

.. Pointing to parent directory

In addition, the directory file also contains the file name of the file that belongs to the directory. For example, there are also the following entries in vamei, pointing to the file that belongs to the directory:

Doc

Movie

Photo

Linux explains an absolute path in the following way: first find the root directory file, read the location of the home directory file from the directory file, and then read the location of vamei from the home file ...... Go to the location of file.txt in the directory.

Because the directory files contain. and ... or .. to indicate the current directory or parent directory, such as/home/vamei/doc /.. it is equivalent to/home/vamei.

In addition, Linux maintains a variable of the working directory (present working directory. You can query the working directory at any time (enter $ pwd in the command line ). This is to save the trouble of entering a long absolute path every time. For example, if we change the working directory to/home/vamei ($ cd/home/vamei.pdf, then we can find file.txt to save/home/vamei/($ ls doc/file.txt ), the obtained path is relative path, and the above doc/file.txt is such a relative path.

 

3. File Operations

For files, we can read (read), write (write), and run (execute ). Read data is obtained from an existing file. Data is written to a new or old file. If the file stores executable binary code, it can be loaded into the 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 the permission to perform this operation on the file. The File Permission information is stored in the file information (metadata). For details, see the next section.

 

3. file additional information (metadata)

Files only contain data. The file name is actually stored in the directory file. In addition, there are additional file information maintained by the operating system, such as file type, file size, File Permission, file modification time, and file read time. You can use the ls command to query the file information ($ ls-l file.txt) and obtain the following results:

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

First, we will introduce the most open-ended file, indicating that file1.txt is a regular file (if it is a directory file, it should be displayed as d ).

It then contains nine characters, rw-r --, which are used to indicate file permissions. The nine characters are divided into three groups: rw-, r --, and r --, corresponding to owner, owner group, and all others ). After Linux is started, I will have a user and a group of identities after logging on., Which is equivalent to my business card. The first group indicates that if the user ID on my business card proves that I am the owner of the file, then I can read the file (r), write (w) this file has the permission, but does not have the permission to execute the file (-. If you have the permission to execute the file, it is x. The second group indicates that if the group ID on my business card proves that I am in the group that owns the file, then I have the permission to read from the file. The third group indicates that if my business card shows that I am neither the owner nor a member of the group, then I only have the read permission. When I want to perform a read operation, Linux first checks whether I am the owner. The following sections further explain the owner and owner group.

The following 1 indicates the number of hard links (link count ). When file1.txt appears in the directory file/home/vamei/doc, a hard link is established. File1.txt is allowed to belong to multiple directory files, equivalent to the same file appears in multiple directories, that is, link count can be more than 1. When link count = 0, the file is removed from the file tree, and the operating system deletes the file. Therefore, unlink and remove are often used in the operating system. Since soft link is widely used (soft link does not affect link count and can span the file system), hard connections are rarely established manually.

Vamei indicates that the user vamei is the owner of the file, and the owner of the file has the right to change the file permissions (for example, rwxrwxrwx ). The owner group of the vamei file is vamei. The owner and owner Group of the file are attached to the file when the file is created (it is equivalent to locking the file, and only users with proper business cards can open the file ). Note that Linux has a Super User root (also called root User) who owns all files.

The subsequent value 8445 indicates the file size, in bytes.

The last time the file was written (modification time ). In fact, the append information of the file contains the last access time of the file, which is not displayed.

 

4. soft link (or symbolic link)

As mentioned above, soft links do not affect the link count of files. If you still remember the shortcut in windows, the soft link (also called symbolic link) in Linux is the shortcut in linux. A soft link is essentially a file. Its file type is symbolic link. This file contains the absolute path of the file to which the link points. When you read data from this file, linux directs you to the file you direct to and reads data from that file (as if you double-click the shortcut ). Soft links can be easily created anywhere and point to any absolute path.

A soft link is also a file and can also perform operations on the file. When we operate soft links, we should note whether we operate soft links or target operations that soft links point. If it is the latter, we will say that this operation follows the link guide ).

 

5. umask

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

We can use

$ Umask 022

To change the permission mask.

 

Summary

Computers are essentially tools for processing data, and files are the logical carrier of data storage. Therefore, it is important to understand the Linux File System. An understanding of the file system should be combined with other aspects of Linux (such as user management) for organic learning.

File Permission, owner, owner group, Super User root

Hard link, soft link, follow the link

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.