For computers , The so-called data is 0 and 1 Sequence . Such a sequence can be stored in memory , But the data in memory will disappear with the shutdown . In order to preserve data for a long time , We store data on CD or hard disk . According to our needs , We usually save the data separately in small units like files ( So called small , It's relative to all the data ). But if data can only be organized as files , If it can't be classified , The documents will still be messy . Every time we search for a file , We have to check one file after another , Too much trouble . file system (file system) Yes, it is the logical organization of documents , It stores files in a clearer way .


Introduction to paths and files

Files are organized into file systems (file system) in , It usually becomes a tree (tree) structure .Linux There is a root directory /, That's the top of the tree . The very end of the tree's fork represents a file , And at the fork of the tree is a directory (directory, It's like we're in windows The folder you see in the interface ). In the figure 1 What you see in is a whole file tree . If we take part of the tree , For example, from the directory vamei Start to go down , It's actually a file system .

To find a file , In addition to knowing the name of the file , You also need to know all the directory names from the root of the tree to the file . The directory names and file names of all paths starting from the root constitute a path (path). for instance , We are Linux Find a file in file.txt, Not just the file name (file.txt), And know the full path , It's the absolute path (/home/vamei/doc/file.txt). From the root directory /, It's the top of the tree , Through the catalog home, vamei, doc, Finally I saw the file file.txt. The whole file system is hierarchical (hierarchy),vamei yes home A subdirectory , and home yes vamei The parent directory of .


stay Linux in , We use it ls Command to display all the files in the directory , such as  $ls /home/vamei/doc

file system chart 1 File tree


The file system shown in the figure , It's a green tree . The top root (/), Follow the path indicated by the red arrow , We finally found the file file.txt.



stay Linux In the system , A directory is also a kind of file . therefore /home/vamei It's pointing to the directory file vamei The absolute path of .

This file contains at least the following entries :

.       Point to the current directory

..       Point to the parent directory

besides , The directory file also contains the file name of the file belonging to the directory , such as vamei There will be the following items in , Point to the file that belongs to the directory :





Linux The way to explain an absolute path is as follows : First find the root file , Read... From the directory file home The location of the catalog file , And then from home File read vamei The location of …… Until you find the directory doc Medium file.txt The location of .

Because the directory file has . and .. The entry of , We can add... To the path . perhaps .. To represent the current directory or parent directory , such as /home/vamei/doc/.. And /home/vamei equivalent .

Besides ,Linux In the process , Maintain a working directory (present working directory) The variable of . stay shell in , You can check the working directory at any time ( Enter at the command line $pwd). This is to save the trouble of entering a long absolute path every time . For example, let's change the working directory to /home/vamei ($cd /home/vamei), Then we'll look for it at this time file.txt You can get rid of /home/vamei/ ($ls doc/file.txt), The path obtained in this way is called relative path (relative path), above doc/file.txt It's such a relative path .


When a file appears in a catalog file , We're going to put the files in the file system , We call it creating a hard link to a file (hard link). A file is allowed to appear in multiple directories , such , It has multiple hard links . When the number of hard links (link count) Reduced to 0 when , Papers will be Linux Delete . So a lot of times ,unlink And remove stay Linux In operating system, it means . Because of the soft link (soft link) Widespread use (soft link Does not affect the link count, And across file systems ), Now there is less manual hard connection .


File operations

For the file , We can read (read), write in (write) And operation (execute). Reading is getting data from an existing file . Writing is to write data to a new or old file . If the file stores executable binary code , So it can be loaded into memory , Run as a program . stay Linux In the file system , If a user wants to perform an operation on a file , Then the user must have permission to operate on the file . File permission information is saved in file information (metadata) in , 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 is also additional information about the files maintained by the operating system , For example, file types , File size , File permissions , File modification time , File read time, etc . It can be used ls Command to query file information ($ls -l file.txt), The results are as follows :

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


The meaning of each part is as follows :

  • Let's start with the first -, It represents the file type , explain file1.txt It's a regular file ( If it's a catalog file , It should be displayed d).
  • Then there are nine characters , by rw-r--r--, They are used to represent file permissions . The nine characters are divided into three groups ,rw-, r--, r--, Corresponding to the owner respectively (owner), Owning group (owner group) And all the others (other). review Linux Boot up , After logging in , I'll have a user identity and a group identity , It's my business card . The first group says , 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 in (w) Permissions for this file , But don't have the Executive (-, If you have executive rights , Then for x) Permissions for this file . The second group says , If the group ID on my business card proves that my group is a member of the owning group of the file , So I have access to read from this file . The third group said , If my business card shows that I'm not the owner , It's not a member of the owning group , Then I have only read access . When I want to do a read operation ,Linux I'll see if I'm the owner first, and I'll explain the owner and the owning group further .
  • hinder 1 It's hard wired (hard link) number (link count).
  • After that vamei Represent user vamei It's the owner of the file (owner), The owner of the file has the right to change the file permissions ( For example, change to rwxrwxrwx). And then there's vamei The owning group of the file is the group vamei. The owner and owning group of the file are attached to the file when it is created ( It's like locking a file , Only the user with the right business card can open the operation ). it is to be noted that ,Linux There's a super user root ( Also called root user ), The user owns all the files .
  • And then 8445 Indicates the file size , The unit is byte (byte).
  • Sep 8 07:33 Represents the last write time of the file (modification time). In fact, the file additional information also contains the last read time of the file (access time), It doesn't show .


Soft link (soft link, or symbolic link)

As mentioned above when discussing hard links , Soft links don't affect the file link count. If you remember windows A shortcut to the system ,Linux The soft links (soft link, It's also called symbolic link) Namely linux Shortcut to . Soft link is essentially a file , Its file type is symbolic link. In this file , Contains the absolute path of the file that the link points to . When you read data from this file ,linux Will direct you to the file you're pointing to , And then read from that file ( It's like double clicking on a shortcut ). Soft links can be easily set up anywhere , And point to any absolute path .

The soft link itself is also a file , You can also perform what a file can do . When we work on soft links , Note that we operate on the soft link itself , Or the target operation of the soft link . If it's the latter , Let's say that the operation follows the link guidance (follow the link).



When we create files , For example, use touch, It will try to create the new file as permission 666, That is to say rw-rw-rw-. But the operating system has to refer to permissions mask See if you can actually create a file as 666. jurisdiction mask Indicates the permission bit that is not allowed to be set by the operating system , for instance 037(----wxrwx) Authority mask It means that setting... Is not allowed group Of wx Bit and other Of rwx position . If it's this privilege mask Words , The final file permissions are rw-r----- (group Of w Bit and other Of rw Quilt mask).

We can go through

$umask 022

Change permissions in a different way mask.



Computers are essentially tools for processing data , The file is the logical carrier of data storage , So understand Linux File systems are important . Knowledge of the file system should be combined with Linux Other aspects of ( For example, user management ) Do organic learning .

File permissions , The owner , Owning group , The super user root

Hard links , Soft link ,follow the link