Linux File attributes detailed description

Source: Internet
Author: User

Transferred from: http://hi.baidu.com/inclover/blog/item/0a7f6f97efddde6755fb96e5.html

1, the Linux file attributes summary;

The properties of a Linux file or directory include: the file or directory node, type, permission mode, number of links, the user and user groups belonging to, recently visited or modified time and so on;

[[email protected] ~]# ls -lih
总计 104K

2408949 -rwxr-xr-x 1 root root    7 04-21 12:47 lsfile.sh
2408830 drwxr-xr-x 2 root root 4.0K 04-21 12:46 mkuml-2004.07.17
2408260 drwxr-xr-x 2 root root 4.0K 04-21 22:15 mydir
2408258 lrwxrwxrwx 1 root root    7 04-21 22:16 sun001.txt -> sun.txt
2408263 -rw-r--r-- 2 root root   11 04-20 14:17 sun002.txt
2408263 -rw-r--r-- 2 root root   11 04-20 14:17 sun.txt

Explain:
first field:inode
second field: file type and permissions;
The third field: the number of hard links;
Fourth field: owner;
Fifth field: the group to which it belongs;
Sixth field: the size of the file or directory;
Seventh field and eighth field: last access or modification time;
nineth field: file name or directory name

We take lsfile.sh as an example:

2408949 -rwxr-xr-x 1 root root    7 04-21 12:47 lsfile.sh

the Inode value is:2408949
file Type: file Type Yes-, indicates that this is an ordinary file; For the type of file, refer to: "Linux file types and file extensions"
file permissions: file permissions are Rwxr-xr-x, indicating that the file belongs to the main readable, writable, executable, the user group that the file belongs to the readable executable, other users can read executable;
Number of hard links: lsfile.sh This file does not have a hard link, because the value is 1, which is himself;
file owner: that is the file belongs to which user, it is attributed to root, that is, the first root;
file group: that is, for this file, it belongs to which user group, here is the root user group;
File Size: file size is 7 bytes;
Access Modifiable time: The time here is the last access time, the last access and the time the file was modified or created, sometimes not consistent;

Of course, the properties of the document not only include these, but these are some of the most commonly used properties. We have more important to say, such as the type of documents belong to, permissions, the number of hard links ...


2, about the inode;

The inode is translated into Chinese as an index node. Partition of each storage device or storage device (storage device is hard disk, floppy disk, USB stick ...) After being formatted as a filesystem, there should be two parts, one for the inode, and the other for the Block,block to store the data. The inode is the information that is used to store this data, including file size, owner, attribution user group, read-write permission, and so on. The Inode indexes the information for each file, so there is a value for the inode. According to the instructions, the operating system can find the corresponding file by the Inode value.

To make a metaphor, such as a book, a storage device or partition equivalent to this book, block equivalent to each page of the book, the Inode is equivalent to the book in front of the directory, a book has a lot of content, if you want to find a part of the content, we can first look at the directory, through the directory can find the fastest we want to see Although not very appropriate, but still a comparative image.

When we use LS to view a directory or file, if you add the-i parameter, you can see the Inode node, such as the example we said earlier;

[[email protected] ~]# ls -li lsfile.sh
2408949 -rwxr-xr-x 1 root root 7 04-21 12:47 lsfile.sh

The inode value of the lsfile.sh is 2408949; View the inode of a file or directory to pass the-i parameter of the LS command.


2.10 Inode The same file is a hard-link file;

In a Linux file system, files with the same inode value are hard-linked files, that is, different file names, the inode may be the same, and an inode value can correspond to multiple files. It's not hard to understand the link file, just take a look at the example. In Linux, linked files are created through the LN tool.


2.11 Create hard links, hard links and source file relationships;

Syntax for creating file hard links with Ln:

# ln   源文件   目标文件

Let's take an example, in this example, to create its hard-link sun002.txt for sun.txt. Then look at the changes in the properties of Sun.txt and Sun002.txt;

[[email protected] ~]# ls -li sun.txt   注:查看sun.txt的属性;
2408263 -rw-r--r-- 1 root root 29 04-22 21:02 sun.txt   注:这是sun.txt的属性;

[[email protected] ~]# ln sun.txt sun002.txt 注:我们通过ln 来创建sun.txt的硬链接文件sun002.txt 

[[email protected] ~]# ls -li sun*   注:我们列一下sun.txt 和sun002.txt

2408263 -rw-r--r-- 2 root root 29 04-22 21:02 sun002.txt
2408263 -rw-r--r-- 2 root root 29 04-22 21:02 sun.txt

We can see that sun.txt when the hard link file sun002.txt is not created, the number of links is 1 (that is, the value after-rw-r--r--), after creating a hard link sun002.txt created, this value becomes 2. In other words, each time we create a new hard-link file for Sun.txt, the number of hard links increases by 1.

The Inode value is the same as the file, and their relationship is the relationship of the hard link to each other. When we modify the contents of one of these files, the contents of the files that are hard-linked are changed as well. If we delete a file that is a hard-link relationship, the other files are not affected. For example, when we delete sun.txt, we can still see the content of Sun002.txt, and Sun02.txt is still there.

As you can understand, the files that are hard-linked to each other, they seem to be clones, and their attributes are almost exactly the same;

In the following example, we remove sun.txt, and then we look at whether Sun002.txt is able to see its contents.

[[email protected] ~]# rm -rf sun.txt 
[[email protected] ~]# more sun002.txt

Note: hard links cannot be created for a directory, only files can create hard links.


2.12 The creation of soft links, and the relationship between soft and source files;

Create a soft link (also known as a symbolic link) syntax;

# ln   -s 源文文件或目录     目标文件或目录

Soft links are also called Symbolic links, unlike hard links, where a soft link file is just a token of its source file. When we delete the source file, the linked file cannot exist independently, although we still keep the file name, but we can not view the contents of the soft link file.

[[email protected] ~]# ls -li linuxsir001.txt
2408274 -rw-r--r-- 1 root root 29 04-22 21:53 linuxsir001.txt

[[email protected] ~]# ln -s linuxsir001.txt linuxsir002.txt

[[email protected] ~]# ls -li linuxsir001.txt linuxsir002.txt
2408274 -rw-r--r-- 1 root root 29 04-22 21:53 linuxsir001.txt
2408795 lrwxrwxrwx 1 root root 15 04-22 21:54 linuxsir002.txt -> linuxsir001.txt

Explain

In the above example, we first look at the properties of the linuxsir001.txt, such as the inode, the type of file it belongs to, the time it was created or modified ... Let's compare:

First, a comparison of nodes: Two files of different nodes;
Next two files belong to different kinds of linuxsir001.txt-, that is, ordinary files, and Linuxsir002.txt is L, it is a link file;
The third two files read and write permissions are different linuxsir001.txt is rw-r--r--, and Linuxsir002.txt Read and write permission is rwxrwxrwx
The third two have the same number of hard links; they're all 1.
The owner and the user group belonging to the two file are the same;
The change (or access, creation) time is different;

We also note that Linuxsir002.txt has a tag-> behind it, which means that linuxsir002.txt is a soft-link file for Linuxsir001.txt.

It is worth noting that when we modify the contents of a linked file, it means that we are modifying the contents of the source file. Of course, the properties of the source file will also change, and the properties of the linked file will not change. When we delete the source file, the link file only has a file name, because the source file is lost, so the soft link file will not exist. This is different from hard links;

[[email protected] ~]# rm -rf linuxsir001.txt  注:删除linuxsir001.txt 
[[email protected] ~]# ls -li linuxsir002.txt  注:查看linuxsir002 的属性;
2408795 lrwxrwxrwx 1 root root 15 04-22 21:54 linuxsir002.txt -> linuxsir001.txt

[[email protected] ~]# more linuxsir002.txt  注:查看linuxsir002.txt的内容; 
linuxsir002.txt: 没有那个文件或目录       注:得到提示,linuxsir002.txt不存在。

The above example tells us that if a linked file loses its source, it means that he is no longer present;

We can see the soft link file, in fact, just a token of the source file, when the source file is lost, he is there. A soft link file consumes only the inode to store information such as the soft link file properties, but the file store points to the source file.

Software links that can be used for files or directories. Both soft links and hard links can be deleted using RM. The RM tool is generic.


3, file type;

When displaying the properties of a file or directory using the long format of the LS instruction;

[[email protected] ~]# ls -lih
总计 104K

2408949 -rwxr-xr-x 1 root root    7 04-21 12:47 lsfile.sh
2408830 drwxr-xr-x 2 root root 4.0K 04-21 12:46 mkuml-2004.07.17
2408260 drwxr-xr-x 2 root root 4.0K 04-21 22:15 mydir
2408258 lrwxrwxrwx 1 root root    7 04-21 22:16 sun001.txt -> sun.txt
2408263 -rw-r--r-- 2 root root   11 04-20 14:17 sun002.txt
2408263 -rw-r--r-- 2 root root   11 04-20 14:17 sun.txt

Let's take a look at the lsfile.sh line, which has a field-rwxr-xr-x. This field contains two information, one is the file type, and the other is the file's permission; The file type is the first character, the file category that the lsfile.sh file belongs to IS-. Similarly mkuml-2004.07.17 this paragraph is drwxr-xr-x, it belongs to the file type should be D;sun001.txt file, corresponding lrwxrwxrwx,sun001.txt belong to the file type should be-L.

We can know that Linux files can be divided into such as ordinary files, directories, symbolic link files, character and block device files, socket files and so on.

For more information, refer to:"Linux file types and file extensions"


4, the permission of the file;

The permissions of a Linux file or directory are associated with users and user groups, so to understand this part, you first need to understand Linux user management knowledge. Refer to: Linux User and User Group Management Overview. If you are new to this document, it is important for you to understand the documentation for this article.

Each file or directory has a set of 9 permission bits, each three bits are divided into a group, they are sovereign limit (three locations), User group permissions (three locations), other user privilege bits (three locations). For example, Rwxr-xr-x, we can count to know if it is 9 locations, it is these 9 permission bits to control the file owner, user group and other user's permissions.


4.1 about the permission bit;

The permission bits of a Linux file or directory are controlled by 9 permission bits, each of which is a set of three bits, which are the read, write, execute, read, write, execute, and other user's reading, writing and execution of the file owner (ower);

File owner: Read R, write W, execute X
User groups: Read R, write W, execute X


If the permission bit is unreadable, non-writable, non-executable, it is represented by-.


Read, write, and execute permissions for ordinary files can be understood as follows:

可读:意味着我们可以查看阅读;
可写:意味着,可以修改或删除(不过删除或修改的权限受父目录上的权限控制);
可执行:意味着如果文件就可以运行,比如二进制文件(比如命令),或脚本(要用脚本语言解释器来解释运行)。

Like what:

[[email protected] ~]# ls -l lsfile.sh
-rwxr-xr-x 1 root root 7 04-21 12:47 lsfile.sh

The first character in the first field,-rwxr-xr-x, is--that indicates that lsfile.sh is an ordinary file;

The lsfile.sh permission is rwxr-xr-x. Represents the lsfile.sh file, the main root of the file, with rwx (readable, writable, executable) permissions, user group root, with r-x (readable, writable) permissions, other users have r-x (readable, writable) permissions. These 9 rights together is rwxr-xr-x, that is, the lsfile.sh file, the file is the main root has readable, writable, executable permissions, user group root all users have the ability to read executable permissions, other users have the ability to read executable permissions.

View the properties of the file with the Ls-l file; The properties of the view directory are the ls-d directory. Please refer to:


4.2 Command chmod to change permissions;

chmod is a command used to change the permissions of a file or directory, but only the owner and Superuser root of the file have this permission. There are two ways to change the permissions of a file or directory through chmod, one is through the octal syntax and the other by mnemonic syntax;

Example:

[[email protected] ~]# touch linuxsir007.txt 注:创建linuxsir007.txt文件;
[[email protected] ~]# touch linuxsir008.txt 注:创建linuxsir008.txt 文件;

[[email protected] ~]# ls -lh linuxsir007.txt linuxsir008.txt 注:查看linuxsir007.txt和linuxsir008.txt文件属性;
-rw-r--r-- 1 root root 0 04-23 20:11 linuxsir007.txt 注:linuxsir007.txt 文件属性;
-rw-r--r-- 1 root root 0 04-23 20:11 linuxsir008.txt 注:linuxsir008.txt 文件属性; 


[[email protected] ~]# chmod 755 linuxsir007.txt    注:通过八进制语法来改变linuxsir007.txt的权限;
[[email protected] ~]# chmod u+x,og+x linuxsir008.txt 注:通过助记语法来改变linuxsir008.txt的权限;
[[email protected] ~]# ls -lh linuxsir007.txt linuxsir008.txt   注:查看linuxsir007.txt和linuxsir008.txt文件属性;

-rwxr-xr-x 1 root root 0 04-23 20:11 linuxsir007.txt 
-rwxr-xr-x 1 root root 0 04-23 20:11 linuxsir008.txt

The above example is an example of a way to change permissions by Chmod's two syntaxes, and what I want to illustrate is that both methods can achieve the same goal.

This process is:

First of all: Create Linuxsir007.txt and linuxsir008.txt two files;
Second: View the properties of the two files; their permissions are the same, are rw-r--r--, indicating that the file belongs to the main readable writable, the user group belonging to the file is readable, other user-readable;
Third: Through the chmod octal syntax to change the permissions of the Linuxsir007.txt, through the chmod mnemonic grammar to change the permissions of Linuxsir008.txt; We change the linuxsir007.txt and Linuxsir008.txt files in two ways, using the two different syntaxes of chmod to achieve the same permissions for two files. In the octal syntax, we used 755, and in mnemonic grammar, we used u+x,og+x. Although two different grammars are used, the goal is the same, and the final result, we can see that the permissions of the two files are rwxr-xr-x. That is, the owner of the file has a readable writable executable permission, the file belongs to the user group has a readable executable permissions, other users have the ability to read executable permissions.

Thus we have introduced two methods to change the permissions of files or directories through the chmod tool: Chmod's octal grammar, chmod's mnemonic grammar;


4.21 changing the permissions of a file or directory by chmod the octal syntax;

As we have said before, the permissions of the file or directory has a total of 9 locations, the file owner, the group belonging to the file occupies three bits and other users occupy three locations. Look at an example:

-rwxr-xr-x 1 root root 0 04-23 20:11 linuxsir007.txt

Description

is a sovereign limit: occupies three positions, the order of three positions is read R, write W, execute x. If there is no permission, then-. In this example, we see RWX, which indicates that the owner has access to three positions of authority, that is, readable and writable executable;
Group permission bit: takes three positions, three positions in the order of read R, write W, execute x. If there is no permission, then-. In this example, what we see is r-x, which is in the written position--that there is no write permission, the group to which the file belongs has a readable executable, but no write permission.
Other user privilege bits: Occupy three positions, three positions in the order of read R, write W, execute x, if there is no permission, then-. In this example, we look at the other user's permission bit is r-x, in the write position is-, indicates that there is no write permission, the file belongs to the group to the file has a readable executable, but no write permission.


A numerical description of the octal syntax of chmod;

R     4
W
X
- 0

the authority of a master is expressed in numbers: the sum of the numbers of the three permission bits of the master are added together. For example, the above example is the owner of the authority is rwx, that is, 4+2+1, should be 7;
the permission of the genus Group is expressed in numbers: The sum of the number of the permission bits of the genus Group. For example in the above example of r-x, that is, 4+0+1, should be 5;
Other user's permission number expression: the sum of the numbers added to the other user's permission bits. For example in the above example is r-x, that is, 4+0+1, should be 5;

[[email protected] ~]# ls -l sun.txt
-rwxr-xr-x 2 root root 29 04-22 21:02 sun.txt 注:查看sun.txt的属性,我们看到sun.txt的权限位是rwxr-xr-x,用八进制数字表示是755 ;

[[email protected] ~]# chmod 644 sun.txt 注;我们改变它的权限为属主可读可写、属组可读、其它用户可读,也就是rw-r--r--,用数字表示就是644;

[[email protected] ~]# ls -l sun.txt
-rw-r--r-- 2 root root 29 04-22 21:02 sun.txt 注:是不是达到目的了?是的!


Each three-bit permission code (belong to the main, belong to group, other users) combination, there are 8 possibilities;

Octal Digital      Permissions         
0 ---
1 --x
2 -w-
3 -WX
4 r--
5 R-x
6 rw-
7 rwx

Note: We can combine the permissions according to the number list above, for example, I want the owner to have rwx (the corresponding number is 7), the group has--x (the corresponding number is 1), other users have---(the corresponding number is 0), This way we combine the permissions of each group to be rwx--x---(the corresponding number is 710).

[[email protected] ~]# ls -l sun.txt
-rw-r--r-- 2 root root 29 04-22 21:02 sun.txt

[[email protected] ~]# chmod 710 sun.txt

[[email protected] ~]# ls -l sun.txt
-rwx--x--- 2 root root 29 04-22 21:02 sun.txt


If we want to change only the permission to open the directory, do not add any parameters. If you want to change all files or subdirectories below the directory, you should add the-r parameter;

[[email protected] ~]# ls -ld testdir/
drwxr-xr-x 3 root root 4096 04-24 11:01 testdir/

[[email protected] ~]# ls -lr testdir/
总计 4
-rw-r--r-- 1 root root    0 04-24 11:01 sir01.txt
drwxr-xr-x 2 root root 4096 04-24 11:01 linuxsir

We only change the TestDir's permission to have the master readable writable, group readable, other user-readable, that is, rw-r--r--, with octal arithmetic should be 644.

[[email protected] ~]# ls -ld testdir/ 注:我们查看到的testdir目录的权限是rwxr--r--;
drwxr-xr-x 3 root root 4096 04-24 11:01 testdir/ 

[[email protected] ~]# chmod 644 testdir/

[[email protected] ~]# ls -ld testdir/
drw-r--r-- 3 root root 4096 04-24 11:01 testdir/ 注:改变成功;

[[email protected] ~]# ls -lr testdir/ 注:我们查看一下testdir目录下的子目录是否改变了?

Try changing the TestDir and its subdirectories and file permissions to change to rw-r--r--permissions. We need to use the-R parameter;

[[email protected] ~]# chmod -R 644 testdir/ 注:我们改变testdir下所有的文件或子目录的权限都为 rw-r--r--

[[email protected] ~]# ls -ld testdir/ 
drw-r--r-- 3 root root 4096 04-24 11:01 testdir/
[[email protected] ~]# ls -lr testdir/
总计 4
-rw-r--r-- 1 root root    0 04-24 11:01 sir01.txt
drw-r--r-- 2 root root 4096 04-24 11:01 linuxsir

It isimportant to note that chmod changes all. File in a directory with caution. Like we're going to change everything under TestDir. File has permissions of rwxr--r--, but we do not want to change the permissions of the TestDir directory. The following example operation is wrong.

[[email protected] ~]# ls -ld testdir/
drw-r--r-- 3 root root 4096 04-24 11:01 testdir/

[[email protected] ~]# ls -lr testdir/
总计 4
-rw-r--r-- 1 root root    0 04-24 11:01 sir01.txt
drw-r--r-- 2 root root 4096 04-24 11:01 linuxsir

[[email protected] ~]# chmod -R 755 testdir/.* 注:这样操作,会把testdir目录的权限一并改为 rwxr--r-- 

[[email protected] ~]# ls -ld testdir/
drwxr-xr-x 3 root root 4096 04-24 11:01 testdir/

So when you want to change all the. File permissions in a directory, and you want to preserve the original permissions for that directory, do not use. * Wildcard characters. As to what should be done, I think we all understand. For example, the following match;

[[email protected] ~]# chmod -R 644 testdir/.[a-z]*

The above example chmod will match all files that begin with. A To. Z.


4.22 Change the permissions of a file or directory by chmod mnemonic syntax;

The mnemonic grammar of chmod is relatively simple, when the change of file or directory permission is done by the form of more intuitionistic characters, and the definition of related letters in mnemonic grammar;


User or user group definition:

U represents the Owner
G Representative Group
O on behalf of other users
A represents the owner, the group and other users, that is, all of the above three users (or groups);


Permission Definition Letter:

R represents Read permission;
W represents write permission;
x represents execution rights;


Permission increment or decrement character;

-delegate minus the relevant permission;
+ representative to increase the relevant authority;

Example one:

[[email protected] ~]# ls -l linuxsir007.txt
-rwxr-xr-x 1 root root 0 04-23 20:11 linuxsir007.txt

[[email protected] ~]# chmod ugo-x linuxsir007.txt 注:把属主、用户组及其它用户的执行权限都减掉;

[[email protected] ~]# ls -l linuxsir007.txt
-rw-r--r-- 1 root root 0 04-23 20:11 linuxsir007.txt

Example two:

[[email protected] ~]# ls -l linuxsir007.txt
-rw-r--r-- 1 root root 0 04-23 20:11 linuxsir007.txt

[[email protected] ~]# chmod u+x linuxsir007.txt 注:为文件的属主增加执行权限 

[[email protected] ~]# ls -l linuxsir007.txt 
-rwxr--r-- 1 root root 0 04-23 20:11 linuxsir007.txt

Example three:

[[email protected] ~]# ls -l linuxsir007.txt 
-rwxr--r-- 1 root root 0 04-23 20:11 linuxsir007.txt

[[email protected] ~]# chmod u-x,go+rw linuxsir007.txt 注:减去文件属主对文件的执行权,增加属组和其它用户对文件的可读可写权;

[[email protected] ~]# ls -l linuxsir007.txt
-rw-rw-rw- 1 root root 0 04-23 20:11 linuxsir007.txt

The use of mnemonic grammar is more flexible, more convenient combination;

U=r+x add read and Write permission for the file owner;
Ug=rwx,o=r add read, write, execute permissions for the owner and the group, and set read permissions for other users.
A+x Add execute permission for the owner, group and other users of the file;
G=u the group of files and the owner and permission of the same;

For directory permission settings, use the-R parameter;

As with the Octal method, if we have the same properties for a directory and its subdirectories and files, you can use the-r parameter;

[[email protected] ~]# chmod -R a+rwx testdir/
[[email protected] ~]# ls -lr testdir/
总计 4
-rwxrwxrwx 1 root root    0 04-24 11:01 sir01.txt
drwxrwxrwx 2 root root 4096 04-24 11:01 linuxsir

Linux File attributes detailed description

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.