File attributes (Ls-lhi see the information)
1.1 First column: Inode number 1.1.1 What is inode
The file is stored on the hard disk, and the minimum storage unit for the hard disk is called "Sector" (sector). The size of each "sector" is 512 bytes (byte),
, when the operating system reads the hard disk, it does not read each sector, so the efficiency is too slow. He reads multiple sectors at once, which is a one-time read of block blocks. A block consists of 8 contiguous sectors (sector).
The data is in block blocks, but how do we know which block blocks the data is stored in? At this point, you need an index that will guide us to find the amount of data stored in block blocks. This is where we call the index node (Inode), the index node includes: type of file, owner, group, permission, and timestamp some information, but does not include the file name,
What the 1.1.2 Inode contains
1:文件的类型2:文件的权限,属主、属组、其他人的权限(r:读;w:写;o:其他人)3:文件的硬连接数4:属主5:属组6:文件的大小,对于目录而言:只是目录本身的大小,而不是里面内容的大小7:默认是文件的修改时间[[email protected]_50 ~]# stat / File: `/‘ #除了这个其他的都存放在inode里面 Size: 4096 Blocks: 8 IO Block: 4096 directoryDevice: 803h/2051d Inode: 2 Links: 29Access: (0555/dr-xr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)Access: 2018-12-13 08:11:33.331896121 -0500Modify: 2018-07-27 00:10:10.415506122 -0400Change: 2018-07-27 00:10:10.415506122 -0400
Size of the 1.1.3 Inode
Think about it, since he wants to store the content, so he must also need to occupy disk space size. The hard disk partition automatically divides the hard disk partition into two zones when creating the file system (format):
1)Block块,数据区:存放实际的数据 2)Inode块,索引区:存放inode所包含的信息(文件属性信息)inode节点的大小和总数,是创建文件系统的时候就给定的,后期没办法更改,一般是128字节(byte)或者256字节(byte)。
1.1.4 How to view the number and size of Inode: 1.1.4.1 View Quantity
df -i [[email protected]_50 ~]# dumpe2fs /dev/sda3 | grep -i "inode count"dumpe2fs 1.41.12 (17-May-2010)Inode count: 1250928
1.1.4.2 View the size of the inode:
[[email protected]_50 ~]# dumpe2fs /dev/sda3 | grep -i "inode size"dumpe2fs 1.41.12 (17-May-2010)Inode size: 256
If the inode is fully occupied, it will indicate that the disk is full when the data is written in the file, no space left on device
1.1.5 inode Number
Each inode has a number, and the operating system uses Inode numbers to identify different files. Linux does not test file names to differentiate files, just as Linux does not rely on usernames to differentiate users. The filename is simply a nickname for the inode number to be easily recognizable.
The process of opening or editing a file:
1.1.6 Directory File
Linux under all the files, so listing the contents of the directory is also equivalent to listing the contents of the directory files, into the directory, it is equivalent to enter the directory file.
The structure of the catalog file is very simple, a list of a series of catalog items (dirent), each of which consists of two parts:
1) The file name that is included
2) The corresponding relationship between the file name and the Inode (index node number) contained
Modifying the file name under the directory is actually the corresponding information in the block block that modifies the directory file itself. So can not change the directory under the name of the file is based on the permissions of the directory. It is not based on the permissions of the file province.
1.1.7 Inode Summary
磁盘被分区格式化为ext4文件系统后会生成一定数量的inode和block1)inode被称为索引点,存放文件的属性信息及作为文件的索引(类似于C语言指针)2)ext3/ext4文件系统的block存放的是文件的实际内容3)inode是磁盘上的一块存储空间,CentOS5是128字节,CentOS6是256字节4)inode的表现形式是一串数字,不同文件对应inode不相同5)inode号相同的互为硬链接6)ext3和ext4文件系统下,一个文件至少占用一个inode和block7)ext3和ext4文件系统下,一个文件只能占用一个inode改变inode大小,mkfs.ext4 –b 2048 –i 256#-b指定block大小,-i指定inode大小
1.1.8 Block Summary
1)用来实际存放数据的地方,如果是目录,里面存放下级文件的文件名称2)磁盘读取数据是按照block为单位读取的3)一个文件至少占用一个block,未用完的浪费,可以占用多个block4)要提升磁盘I/O性能,那就要一次性读取数据尽量的多5)block并非越大越好。block太大会对小文件存放浪费太多磁盘空间,太小对于大文件来说,会更大的消耗I/O。一般默认为4K(4096字节)
1.2 File types
-: Representing ordinary files
D: Representative Directory
L: Represents a soft connection (ln–s source file link file)
B: block devices and other peripherals, are special types of files
Common file "-" is also divided into: (using file to view)
1) Plain text file (ASCLL): Document content can be directly supervised data
2) binary (binary): The command program in Linux is this format
3) Data format file: Some programs will read files in certain formats while they are running, and those files in a particular format are called data files.
Although Linux does not differentiate files by suffix, it is best to write them for easy administration.
In general, this sets the suffix name:
*.txt 文本文件*.tar 打包文件*.tar.bz bzip2格式压缩打包文件(j)*.tar.gz gzip格式压缩打包文件(z)*.py 表示python语言文件*.sh shell编程脚本*.pl 表示perl文件*.html,*.php,*.htm,*.php,*.jsp,*.do 网页语言文件*.conf 配置文件*.rpm rpm安装包
1.3 Permissions: 1.3.1 Normal permissions 1.3.1.1 permissions to files:
读:r(read):可读权限,表示具有读取,和阅读文件的内容写:w(write):写权限,表示具有增加,删除,修改文件内容的权限(是对于文件内容来操作的) 1)如果没有r权限,用vim编辑的时候会提示无法编辑,但是可以使用cat,或echo重定向追加执行:x(execute):文件可以被系统执行 2)如果没有r权限,就是由执行权限,它也会提示权限不足,(想一下,你连里面的内容都不知道是啥,怎么去执行他呢。) 3)删除文件,或修改文件名是跟他的父目录有关的,因为文件名是存放在上级目录的block块里面的。
1.3.1.2 permissions on a directory
读:r:表示是否具有浏览目录下面及子目录的权限(ls或者tree)写:w:具有增加、删除和修改目录下的文件名或目录名的权限,但需要x权限的配合(如果没有x权限,更改完成之后却不能执行(保存)那修改还有什么意义呢?)x:表示具有进入的权限,没有r权限能进去,没有w选线不能修改和创建和删除。 1)如果没有x权限,就不能进去到目录里面 2)如果有r权限,没有x权限,ls是可以看到目录下面的内容的,但是不能cd进入到这个目录内。ls -l查看这个目录里面内容的属性的时候全都是???[[email protected]_50 ~]$ ll -d /tmp/123drwxr-xrw- 2 root root 4096 Jul 27 03:05 /tmp/123[[email protected]_50 ~]$ ls -l /tmp/123/ls: cannot access /tmp/123/1.txt: Permission deniedtotal 0-????????? ? ? ? ? ? 1.txt
1.3.1.3 the permissions of a file or directory are determined by umask
···
Temporary setting umask value: Umask 0022
The directory default total permissions is 0777, then the actual permissions are 0777-umask== actual permissions
The default total permissions for the file is 0666, then the actual permissions are 0666-umask (when an odd number is encountered in the odd bit plus 1) = = Actual permissions
···
1.3.3 Special Permissions 1.3.3.1 SUID
The set UID number "4" is added to a program or command after Setuid (u+s), so that the owner has the S permission, meaning that any user in the execution of this program is to use the identity of the owner to execute, passwd This command is the most representative.
1) When the file or directory itself has x permission, plus s permission, in the X permission bit is s
2) When the file or directory itself does not have X permission, plus s permission, the X permission bit is s
[Email protected]_50 ~]# which passwd
/usr/bin/passwd
[Email protected]_50 ~]# ll-d/usr/bin/passwd
-rwsr-xr-x. 1 root root 30768 Nov 2015/usr/bin/passwd
[Email protected]_50 ~]# ll-d/etc/shadow
----------1 root 945 Jul 23:44/etc/shadow Ordinary users do not have permission to modify this file, but he can change the password, he changed the password when the root user, so he can change the password
1.3.3.2 GUID
Set GID number Code "2", after adding a set GID on a program or command (G+S), so that the group has the S permission, meaning that any user in the execution of this program, the use of the file belongs to the group. Sets the set GID permission to the directory, and any user creates files in that directory, and the group of files is consistent with the directory's genus.
1) When the file or directory itself has x permission, plus s permission, in the X permission bit is s
2) When the file or directory itself does not have X permission, plus s permission, the X permission bit is s
1.3.3.3 T permissions
The sticky number represents 1, attached to other permissions, and when this permission is set, everyone's files or directories can only be deleted by the user itself.
The access control list for the 1.3.4 file 1.3.4.1 the SETFACL directive to
Set the access control list for a file, we can use this command if we want a file to have special permissions on which user. An ACL equivalent to a route that has precedence over the priority of other people's permissions
1.3.4.2-m Add a list of ACLs
Setfacl-m u:user_name:perms file_name Set a list of permissions for a single user for a file
Setfacl-m d:u:user_name:perms file_name Set a list of permissions for a single user for a directory
Setfacl-m g:group_name:perms file_name Set a list of permissions for a user group to a file
Setfacl-m d:g:group_name:perms file_name Set a list of permissions to the directory for the user group
1.3.5 Sticky bit permissions (to be added) 1.4 Links 1.4.1 Hard Links
In general, the file name and inode number is a "one by one correspondence" relationship, it is not possible to appear in the same partition two inode number of the same file, but there is an inode number in the Linux system corresponding to multiple file names. These files are hard links to each other. Although their filenames are different, they are actually working on a file.
Files after the ln source file is linked
ln test.txt TEST.TXT_LN
[Email protected]_50 tmp]# ll-i
Total 0
130937-rw-r--r--2 root root 0 Jul 05:34 test.txt
130937-rw-r--r--2 root root 0 Jul 05:34 test.txt_ln
1.4.1.1 Summary
1) inode号相同的,可以认为互为硬链接2) 硬链接的创建不能跨越文件系统(跨越分区),目录不能创建硬链接3) 删除源文件或链接文件,文件实例未被改变,只有删除所有的硬链接文件和源文件,文件的实体才会被改变。4) 当所有的硬链接和源文件被删除,没有进程调用后,在存放新的数据会占用这个文件的空间或者磁盘fsck检查的时候,删除的数据才会被回收,5) 可以通过文件设置硬链接文件,来防止重要文件被误删除6) 每在目录下创建一个子目录,他的上级目录的硬连接数加1 7) 目录硬链接的个数减去2代表他下面有多少个目录(减去他本身的1,和“.”)
1.4.2 Soft link 1.4.2.1 effect
When a software compiler path is changed, we also want to use the previous path, this time we need a soft connection, soft connection equivalent to a portal, delete the soft connection to the source file has no effect, but delete the source file, the soft connection is invalid.
1.4.2.2 Summary
1) 软链接类似于windows的快捷方式(可以用readlink查看指向)2) 软连接类似于一个文本文件,里面存放的是源文件的路径,指向源文件实体3) 删除源文件,软链接失效,一般显示白字红底闪烁提示4) 软链接具备不同的inode号5) 软链接和源文件属于不同类型的文件
1.4.3 Soft links and hard-link diagrams
1.5 owner, group, and modification time
.```
Owner: This file belongs to that person
Group, which user group this file belongs to
You can modify the "Chown: Genus" File name by Chown
Last modified:
[Email protected] tmp]# stat 1| grep ^modify #可查看
modify:2018-04-08 09:55:03.976989445-0400
Can be modified by touch-m.
[Email protected] tmp]# touch-m 1-d "2011-11-11"
[Email protected] tmp]# stat 1| grep ^modify
Modify:2011-11-11 00:00:00.000000000-0500
## 1.6 系统创建文件提示:no space left on device磁盘空间不足Linux系统中提示这个可能三种原因:1)block块被占满2)inode块被占满3)文件全部都删除了,还是提示空间不足,因为这些文件在被进程占用### 1.6.1 block被占满:#### 1.6.1.1 模拟环境:
The
#虚拟出来一个100M的磁盘.
[[email protected]_50 ~]# dd if=/dev/zero of=/tmp/test_block bs=100m count=1
1+0 Records in
1+0 Records Out
104857600 bytes (MB) copied, 8.69812 S, 12.1 mb/s
[[email protected]_50 ~]# du-h/tmp/test_block
1 00m/tmp/test_block
#给这个分区创建文件系统
[[email protected]_50 ~]# mkfs.ext4/tmp/test_block
mke2fs 1.41.12 ( 17-may-2010)
/tmp/test_block is not a block special device.
Proceed anyway? (y,n) y
Filesystem label=
OS type:linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the Super user
First D ATA block=1
Maximum filesystem blocks=67371008
block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Writing inode tables:done
Creating Journal (4096 blocks): Done
Writing superblocks and filesystem accounting in Formation:done
This filesystem'll be automatically checked every mounts or
, whichever comes first. Use Tune2fs-c or-i to override.
#指定挂载分区格式, mount the virtual disk
[[email protected]_50 ~]# mount-o loop/tmp/test_block/mnt/
[[email Protected]_50 ~]# df-th
Filesystem Type Size used Avail use% mounted on
/dev/sda3 ext4 19G 2.0G 16G 12%/
Tmpfs Tmpfs 2.5G 0 2.5G 0%/dev/shm
/dev/sda1 ext4 190M 66M 115M 37%/boot
/dev/test ext4 93M 2.1M 86M 3%/app/logs
/t Mp/test_block ext4 93M 1.6M 87M 2%/mnt
[[email protected]_50 ~]# cd/mnt
#生成一个大文件 to fill this 100M disk
[[email& Nbsp;protected]_50 mnt]# seq 100000000 >>1.txt
[[email protected]_50 mnt]# df-th
Filesystem Type Size Used Avail use% mounted on
/dev/sda3 ext4 19G 2.0G 16G 12%/
Tmpfs tmpfs 2.5G 0 2.5G 0%/dev/shm
/dev/sda1 ext4 1 90M 66M 115M 37%/boot
/dev/test ext4 93M 2.1M 86M 3%/app/logs
/tmp/test_block ext4 93M 91M 0 100%/mnt
[Email protected]_50 mnt]# seq >>1.txt
Seq:write Error:no space left on device
Error has occurred
#### 1.6.1.2 解决办法:
Find the large file with the DU-SH directive and delete it.
[Email protected]_50 mnt]# du-sh/
#找到了一个比较大目录 "/mnt", in the statistics to view the large directory under this directory, according to this step-by-step search, find this large file, and analyze what content, can be deleted, delete directly delete
[Email protected]_50 mnt]# du-sh/mnt/
90m/mnt/1.txt
[Email protected]_50 mnt]# df-th
Filesystem Type Size used Avail use% mounted on
/dev/sda3 ext4 19G 2.0G 16G 12%/
Tmpfs tmpfs 2.5G 0 2.5G 0%/dev/shm
/DEV/SDA1 ext4 190M 66M 115M 37%/boot
/dev/test ext4 93M 2.1M 86M 3%/app/logs
/tmp/test_block ext4 93M 1.6M 87M 2%/mnt
Reduced disk usage to solve this problem
### 1.6.2 inode被占满inode被沾满的原因,小文件过多,一个文件就要占用一个inode,inode的数量是有限的,所以小文件过多,很少消耗block,所以inode就被占满了。#### 1.6.2.1 模拟环境
[[Email protected]_50 mnt]# touch file_inode{1..100000}.txt
Touch:cannot Touch file_inode27266.txt ': No space left on Device<br/>touch:cannot touch
file_inode27267.txt ': No space left on device
#### 1.6.2.2 解决办法:小文件多,说明他的所在的目录大小肯定大,因为文件名是存放在上级目录的block里面的,小文件多,就说明文件名多,文件名多,就说明占用上级目录的block多。解决思路就是用find查找目录大小大大的文件。[[email protected]_50 /]# find / -type d -size +1M/mnt[[email protected]_50 /]# ls /mnt/ |xargs rm -rf### 1.6.3 被进程占用,找到调用这个文件的进程,删除然后重启一下服务就行。## 1.7 Linux下删除文件的原理![](http://i2.51cto.com/images/blog/201807/29/c746b84255b1496443901d37f83eba52.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)## 1.8 Linux删除文件的详解![](http://i2.51cto.com/images/blog/201807/29/0fb60c5f02b746225aba4bd38b02c0e9.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
Detailed description of Linux file attributes