Explain the file name, file type and file authority in Linux system


Linux file type and secondary file name

I have always emphasized the concept that any device is a file under Linux. Not only that, but also the data communication interface has its own files in charge. Therefore, you will understand that there are many kinds of files in Linux. Besides the general files (-) and directory files (d) mentioned above, what kinds of files are there?

*Document type:

We have just mentioned using ‘LS – L’ to observe that the first character in the ten characters in the first column is the file type. In addition to the common general file (-) and directory file (d), what kinds of file types are there?

* regular file:
In general, the first character of the attribute displayed by LS – Al is [-], such as [- rwxrwxrwx]. In addition, according to the contents of the documents, they can be roughly divided into:

O plain text file (ASCII): This is one of the most popular file types in Linux system. It is called plain text file because it contains information that we can read directly, such as numbers, letters, etc. Almost all the files we can use as settings belong to this file type. For example, you can post ‘cat ~ /. Bashrc’ to see the contents of the file. (cat is the instruction to read the contents of a file)

O binary file: remember we mentioned in the operation of software programs in Chapter 0, introduction to computers that our system only knows and can execute binary files, right? That’s right ~ your Linux executable files (scripts, text batch files are not counted) are in this format. For example, the instruction cat just issued is a binary file.

O data format file (data): some programs will read certain format files in the process of operation, which can be called data files. For example, when a user logs in to Linux, the login data will be recorded in the file / var / log / wtmp, which is a data file that can be read through the last command! However, when using cat, it will read out the garbled code, because it belongs to a special file format. Is it over?

* Directory:
The first attribute is [D], such as [drwxrwxrwx].

* link file:
Is similar to the shortcut under the windows system! The first attribute is [l] (lowercase of L), such as [lrwxrwxrwx];

* device:
Some files related to the peripheral and storage of the system are usually concentrated in the directory / dev! It is usually divided into two types:

O block device file: it is a peripheral device that stores data to provide random access to the system. For example, hard disk and soft disk are just the same! You can read and write randomly in different blocks of the hard disk. This device is called block device! You can check / dev / SDA and find that the first attribute is [b]!

O character device file: that is, some peripheral devices of serial port, such as keyboard, mouse, etc! The feature of these devices is “read once” and cannot truncate the output. For example, you can’t let the mouse “jump” to another picture, but “slide” to another place! The first attribute is [C].

* data interface files (sockets)
Since it is called a data interface file, of course, this type of file is usually used to undertake data on the network. We can start a program to listen to the client’s request, and the client can communicate the data through the socket. The first attribute is [S], which is most often seen in the / var / run directory.

* FIFO, pipe:
FIFO is also a special file type, its main purpose is to solve the error problem caused by multiple programs accessing a file at the same time. FIFO is the abbreviation of first in first out. The first attribute is [P].

In addition to the device file is a very important file in our system, it is best not to modify it at will (usually he will not let you modify it! )Another interesting file is the link file. If you often capture applications to the desktop, you should know that there are so-called “shortcuts” under windows. Similarly, you can simply think of link files in Linux as a shortcut to a file or directory. As for socket and FIFO file, it is difficult to understand, because these two characters are related to the process. After you understand the process in the future, come back to check it! In addition, you can also check the system instructions through man FIFO and man socket!

*Linux file secondary file name:

Basically, there is no so-called “secondary file name” in Linux files. As we have just discussed, whether a Linux file can be executed depends on the ten attributes in the first column, and has nothing to do with the file name. This concept is different from that of windows! Under windows, the secondary file name that can be executed is usually. Com. Exe. Bat, and so on. Under Linux, as long as your permission has x, for example [- rwx-r-xr-x], it means that the file can be executed!

However, it is not the same that can be executed successfully – for example, in the root home directory install.log It is a pure text file. If it is changed to – rwxrwxrwx after modification, can the file be executed successfully? Of course not, because there is no executable data for his content. Therefore, the X represents the executable ability of the file. However, whether the file can be successfully executed depends on the contents of the file

However, we still want to know what the file is by the secondary file name, so we usually use the appropriate secondary file name to indicate what kind of file it is. There are several common secondary file names:

**. Sh: batch files (scripts), because batch files are written in shell, the secondary file name is edited as. Sh;

* *Z, *.tar, *. tar.gz , *. Zip, *. Tgz: packed compressed file. This is because the compression software is gunzip, tar and so on. Because of different compression software, it takes the associated secondary file name!

**. HTML, *. PHP: Web page related files, which represent HTML syntax and PHP syntax respectively! As for the web page that can be accessed directly from the browser through. PHP, the results can be obtained by using the browser!

Basically, the file name on the Linux system really just let you know the possible purpose of the file, and whether it is really executed or not still requires the specification of permissions! For example, although a file is an executable file, such as the common command / bin / LS, which displays the file properties, if the permission of the file is modified to be unable to execute, then LS will become unable to execute!

The above problems occur most often during file transfer. For example, if you download an executable file on the Internet, it will not work on your Linux system! ha-ha! Then it is possible that the attributes of the file have been changed! Don’t doubt that the attributes and permissions of files will be changed when they are transferred from the network to your Linux system!

*Linux file length limit:

Under Linux, when using the default ext2 / ext3 file system, the file name length for the file is limited to:

* the maximum allowable file name of a single file or directory is 255 characters;
* the full file name containing the full path name and directory (/) is 4096 characters.

It’s quite a long file name! We hope that the file name of Linux can know what the file is doing at a glance, so the file name is usually very long! People who are used to windows may not be able to bear it, because the file names are usually very long. For friends who are used to windows and lead to poor typing speed, eh! It’s really troublesome… However, I have to advise you to strengthen your typing training!

*Limitation of Linux File Name:

Due to the operating relationship of some instructions under the text interface of Linux, generally speaking, it is better to avoid some special characters when setting the file name under Linux! For example, the following:

    * ? > < ; & ! [ ] | \ ‘ ” ` ( ) { }

Because these symbols in the text interface, is a special significance! In addition, the name of the file is “hidden”! At the same time, since the command is issued, you often use the option such as – option, so you’d better avoid naming the beginning of the file name with – or +!

Directory and file permissions
Now we know the three identities of files in Linux system (owner, group and others). We know that each identity has three permissions (RWX). We know that we can use chown, chgrp, Chmod to modify these permissions and attributes. Of course, it is OK to use LS – L to observe files. The first two sections also discussed the importance of these file permissions for data security. So, how are these file permissions different for general files and directory files? It’s a big difference! Below let bird brother say clearly, explain clearly!

*The importance of permissions to files

A file is a place where data is actually contained, including general text files, database content files, binary programs, and so on. Therefore, the meaning of permission to a file is as follows:

*R (read): can read the actual content of the file, such as reading the text content of the text file, etc;
*W (write): you can edit, add or modify the contents of the file (but not delete the file);
*X (execute): the file has permissions that can be executed by the system.

The readable (R) means that it is easy to understand the contents of the file, but what about executable (x)? You have to be careful here! Because under windows, whether a file has the ability to execute is determined by the “secondary file name”, for example,. Exe,. Bat,. Com, etc., but under Linux, whether our file can be executed depends on whether it has the permission of “X”! There is no absolute relationship with the file name!

What about the last w permission? When you have w permission on a file, you can write / edit / add / modify the content of the file, but you do not have the right to delete the file itself! For the RWX of a file, it is mainly about the “content of the file”. It has nothing to do with the existence of the file name! Because the document records the actual information!

*The importance of permissions to directories

The file is where the actual data is stored. What is the main storage of the directory? The main content of the directory in the record file name list, file name and directory have a strong relationship! So what does R, W, X mean to a directory if it’s for a directory?

* r (read contents in directory):

It means that you have the permission to read the directory structure list. Therefore, when you have the permission to read (R) a directory, it means that you can query the file name data in the directory. So you can use the LS command to display the content list of the directory!

* w (modify contents of directory):

This writable permission is great for directories! Because it means that you have the permission to move the directory structure list, that is, the following permissions:

O establish new files and directories;
O delete existing files and directories (regardless of the permissions of the file! )
O rename existing files or directories;
O move the file and directory location in the directory.

In short, the w permission of the directory is related to the file name movement under the directory!

* x (access directory):

Eh! What is the purpose of directory execution permission? Directory is just a record of the file name, can not be used to execute it? you ‘re right! The directory cannot be executed. The X of the directory represents whether the user can enter the directory and become the working directory! The so-called work directory is your current directory! For example, when you log on to Linux, your home directory is your current working directory. The command to change the directory is “CD”!

The X permission of the directory determines whether users can enter the directory.

The general concept of directory permission is like this. Let’s take a look at several examples to let you know what is directory permission!

The permissions for a directory are as follows:

Copy code

The code is as follows:

drwxr–r– 3 root root 4096 Jun 25 08:35 .ssh

The system has an account named vbird, which does not support the root group. What permissions does vbird have on this directory? Can I switch to this directory?
Vbird has only R permission to this directory, so vbird can query the list of file names in this directory. Because vbird does not have permission to x, vbird cannot switch to this directory! (quite an important concept! )

In the above example, vbird has the permission of R. at first glance, it seems that R has the permission to enter this directory. In fact, that is wrong. Can enter a directory, only with the directory x permission! In addition, the working directory is very important for the execution of instructions. If you do not have the permission of X in a certain directory, then you cannot switch to the directory, and you cannot execute any instructions under the directory, even if you have the permission of R in the directory.

When setting up a website, many friends will be stuck in some permission settings. They open the directory data to anyone on the Internet, but only the permission of R is opened. As shown in the above example, the result is that the website server software cannot read the files in the directory (only the file name can be seen at most), The end user always can’t check the content of the file correctly (display authority is insufficient! )。 Note: to open the directory for anyone to browse, should at least give R and X permission, but w permission can not be given casually! Why can’t we just give it to you

Suppose you have an account named dmtsai whose home directory is in / home / dmtsai /, and dmtsai has [RWX] permission on this directory. If there is a name in this directory called the_ root.data The permissions of the file are as follows:

Copy code

The code is as follows:

-rwx—— 1 root root 4365 Sep 19 23:20 the_root.data

What is the permission of dmtsai to this file? Can I delete this file?
As shown above, since dmtsai is “others” to this file, it cannot be read, edited or executed. In other words, it cannot change the contents of this file.

However, since this file is in his home directory, where he has full permissions for RWX, the_ root.data This “file name” can be “deleted”! The conclusion is that the user dmtsai can delete the_ root.data This document!

Or don’t you understand? Do you understand me! It doesn’t matter – we’ll design an exercise below, so that you can actually play, should be able to get closer to the situation! However, since many instructions have not been taught, some of the following instructions can be understood first. We will continue to introduce the detailed instruction usage later.

*First use the identity of root to establish the required file and directory environment

We use the identity of root to create a directory named testing in the / tmp directory where everyone can work. The permission of this directory is 744 and the directory owner is root. In addition, create an empty file named testing in the testing directory. MKDIR (make directory) can be used to create a directory, and touch (to be explained in the next chapter) can be used to create an empty file. So the process is as follows:

Copy code

The code is as follows:

[ [email protected] ~]#CD / tmp < = = switch working directory to / tmp
[ [email protected] TMP] ා MKDIR testing < = = create a new directory
[ [email protected] TMP] ා Chmod 744 testing < = = change permissions
[ [email protected] TMP] # touch testing / testing < = = create an empty file
[ [email protected] TMP] ා Chmod 600 testing / testing < = = change permission
[[email protected] tmp]# ls -ald testing testing/testing
drwxr–r– 2 root root 4096 Sep 19 16:01 testing
-rw——- 1 root root 0 Sep 19 16:01 testing/testing

#Take a closer look, the directory permission is 744, and its group and user are both root!
#In this case, what are the permissions of the general identity user on the directory / file?

*What are the read and write permissions of general users? Under observation

In the above example, although the directory is set with the permission of 744, the general user should have the permission of R, but what can the user with such permission do? Suppose there is an account named vbird in Birdie’s system. We can change the identity through “Su – vbird” command! Look at the operation below first!

Copy code

The code is as follows:

[ [email protected] TMP] ා Su – vbird < = = switch identity to vbird!
[ [email protected] ~]$CD / tmp < = = take a look, the identity has changed! The prompt character has changed to $too!
[[email protected] tmp]$ ls -l testing/
?——— ? ? ? ? ? testing
#Because of the permission of R, you can query the file name. But you don’t have enough permissions (NO x), so there’s a bunch of question marks.
[[email protected] tmp]$ cd testing/
-bash: cd: testing/: Permission denied
#Because does not have X, so certainly does not have the permission to enter! There is no response to the previous authority description ah! </p>

In the above exercise, we know that only R can enable users to read the file name list of the directory, but the detailed information is still unavailable. At the same time, we can’t change the directory into the working directory. If we make the directory user’s, can users delete files under this directory? Do the following exercises:

Copy code

The code is as follows:

[ [email protected] TMP] $exit < = = let vbird change back to the original root identity!
[ [email protected] TMP] ා chown vbird testing < = = modify permissions to let vbird own this directory
[ [email protected] TMP] ා Su – vbird < = = become vbird again to operate
[ [email protected] ~]$CD / TMP / testing < = = can enter the directory!
[[email protected] testing]$ ls -l
-RW ——- 1 root root 0 SEP 19 16:01 testing < = = the file is not vbird!
[ [email protected] Testing] $RM testing < = = try to kill this file!
rm: remove write-protected regular empty file `testing’? y
#It can be deleted! Do you understand that?!

Through the above simple steps, you can clearly know that x in the directory is related to whether or not to enter the directory. As for that w, it has very important permissions, because it can let users delete, update, and create new files or directories, which is a very important parameter! Is that understandable?! C