[New Star Project 2023] Explanation of the meaning of Linux directories and file permissions

Author: Insist--

Personal homepage: insist--personal homepage

The author will continue to update network knowledge and python basic knowledge , looking forward to your attention

foreword

Through the previous article, we know the three identities of files in the Linux system ( owner, group and others ), and also know that each identity has three permissions ( rwx ), and we know that we can use chown, chgrp, chmod to Modify these permissions and attributes, of course, use ls -l to observe the file is no problem.

Question: How are these file permissions different for normal files and directory files? The following will give you a detailed explanation

Table of contents

1. The importance of permissions to files

2. The importance of permissions to directories

3. To summarize the above

4. Examples


1. The importance of permissions to files

Files are places that actually contain data , including general text files, database content files, binary executable files, and so on. Therefore, the meaning of permissions for files is as follows:

r (read): The actual content of this file can be read, such as the text content of a text file.

w (write): You can edit, add or modify the content of the file (but not delete the file).

x (eXecute): The file has permission to be executed by the system.

Looking at it again, readable (r) means reading the content of the file is quite easy to understand, but what about executable (x)?

Under the Windows system , whether a file has the ability to execute is judged by the extension (for example: .exe or .bat) . But under Linux , whether our files can be executed depends on whether we have the x permission!

As for the last w permission, when you have w permission on a file, you can have the permission to write/edit/add/modify the content of the file, but you don't have the permission to delete the file itself!

2. The importance of permissions to directories

Files store actual data, so what does a directory mainly store? The main content of the directory is to record the list of file names, and the file name has a strong relationship with the directory. So if it is for a directory, what do the r, w, and x mean for a directory?

r (read contents in directory): Indicates that you have the permission to read the directory structure list , so when you have the permission to read (r) a directory, it means that you can query the file name data in the directory. 

w (modify contents of directory): This writable permission is very important for directories! Because he said that you have the permission to change the directory structure list , that is, the following permissions:

1. Create new files and directories

2. Delete existing files and directories (regardless of the permissions of the file!)

3. Rename an existing file or directory

4. Move the files and directories in the directory.

x (access directory): So what is the use of the execution permission of the directory? Some people may say: the directory is just to record the file name, it can't be used for execution, right? That's right! The directory cannot be executed, and the x of the directory represents whether the user can enter the directory and become the working directory (the current directory) !

For example: when you log in to Linux, your home directory is your current working directory. And the command to change the directory is cd!

3. To summarize the above

After reading the above knowledge, some people may find it confusing, so is there a clearer explanation? Of course there is!

Now assume "the file is a bunch of folders of files" so you might be able to write/modify some stuff on it. And "a directory is a bunch of drawers", so you can classify folders into different drawers. Therefore, the biggest purpose of the drawer is to take out/put in folders! Now let's aggregate the data:

43c8dd31b0d84cc289b18fa49a391ea0.jpg

According to the above analysis, you can see that for general files, rwx mainly designs permissions for " file content ", and for directories, rwx designs permissions for " list of file names in directories ". Probably the most interesting of these are the x permissions on directories! In fact, this x permission design is equivalent to "this directory is the key to this drawer"! How can you open a drawer without a key? Right. The general concept of directory permissions is this.

4. Examples

Let's take a look at an example question to let you understand what directory permissions are!

60e763f278ed4100902afdf8eaa098ae.jpg

Answer: vbird has only r permissionon this directory, so vbird can query the list of file names under this directory. Because vbird does not have x permissions, so vbird does not have the key for this drawer! So vbird cannot switch to this directory! (Quite an important concept!)

In the above example, because vbird has the permission of r, it seems that r has the permission to enter this directory at first glance, but it is wrong . Whether you can enter a certain directory is only related to the x permission of the directory ! In addition, the working directory is very important for the execution of instructions. If you do not have the x permission in a certain directory, then you cannot switch to this directory, and you cannot execute any instructions in this directory, even if you have this directory. r or w permissions for the directory.

Guess you like

Origin blog.csdn.net/m0_73995538/article/details/131561647