网站模板免费,友情链接交易网站源码,坪山手机网站建设,网上国网推广程序员必备的面试技巧
“程序员必备的面试技巧#xff0c;就像是编写一段完美的代码一样重要。在面试战场上#xff0c;我们需要像忍者一样灵活#xff0c;像侦探一样聪明#xff0c;还要像无敌铁金刚一样坚定。只有掌握了这些技巧#xff0c;我们才能在面试的舞台上闪耀…程序员必备的面试技巧
“程序员必备的面试技巧就像是编写一段完美的代码一样重要。在面试战场上我们需要像忍者一样灵活像侦探一样聪明还要像无敌铁金刚一样坚定。只有掌握了这些技巧我们才能在面试的舞台上闪耀光芒成为那个令HR们心动的程序猿” 目录
0 前言1 DOS、Windows、Linux中的文件存储 1.1 扇区1.2 DOS、Windows簇Cluster1.3 Linux块block和索引节点inode2 stat命令的功能、格式和选项说明 2.1 stat命令的功能2.2 stat命令的格式2.3 stat命令的选项3 stat命令使用实例 3.1 stat 文件或目录查看文件或目录信息3.2 stat -t 文件或目录以简洁的形式显示文件或目录状态3.3 stat -f 文件或目录显示文件或目录的系统状态而不是文件或目录状态3.4 stat -L 文件 或 stat --dereference 文件取消对引用跟随符号链接显示并显示该符号链接指向的文件的信息3.5 stat -c 格式 文件 或 stat --format格式 文件 或 stat --printf格式自定义输出4 其它查看Inodes的命令 0 前言
在
4 其它查看Inodes的命令
中我们提到 df -i或--inodes的功能是 列出索引节点而不是块使用情况信息。
那么什么inode索引节点呢我们一起来探讨看看……
1 DOS、Windows、Linux中的文件存储
1.1 扇区
文件一般是存储在磁盘上的磁盘的最小存储单位叫做扇区sector每个扇区大小为512字节。
扇区是磁盘的最小存储单元。
1.2 DOS、Windows簇Cluster
为了提高磁盘读取效率操作系统通常会一次读取多个扇区的内容。
在微软的DOS、WINDOWS等操作系统中把一次读取多个扇区称为簇Cluster簇是DOS、WINDOWS等操作系统中磁盘文件存储管理的最小单位。1个长度非0的文件至少占用1簇的存储空间。
1.3 Linux块block和索引节点inode
在Linux操作系统中把一次读取多个扇区称为块block最常见的是4KB即连续八个扇区组成一个块。文件数据存储在块中块是Linux操作系统中文件存取的最小单位。
索引节点inode是文件元信息的存储区域文件元信息包括以下信息但不包含文件名文件名存放在目录当中。
文件的字节数文件拥有者的 User ID文件的 Group ID文件的读、写、执行权限文件的时间戳共有三个ctime 指 inode 上一次变动的时间mtime 指文件内容上一次变动的时间atime 指文件上一次打开的时间。链接数即有多少文件名指向这个 inode文件数据 block 的位置
Linux系统内部不使用文件名而是使用索引节点号码识别文件。对于系统来说文件名只是为便于识别索引节点号码而使用的别称。
在Linux系统中一个文件必须占用一个索引节点inode占用至少一个块block。
要查看索引节点inode的信息除了使用df命令我们还可以使用stat命令。
2 stat命令的功能、格式和选项说明
我们可以使用 stat --help命令查看 stat 命令的帮助信息。 purpleEndurer bash $ stat --help Usage: stat [OPTION]... FILE... Display file or file system status. Mandatory arguments to long options are mandatory for short options too. -L, --dereference follow links -f, --file-system display file system status instead of file status -c --formatFORMAT use the specified FORMAT instead of the default; output a newline after each use of FORMAT --printfFORMAT like --format, but interpret backslash escapes, and do not output a mandatory trailing newline; if you want a newline, include \n in FORMAT -t, --terse print the information in terse form --help display this help and exit --version output version information and exit The valid format sequences for files (without --file-system): %a access rights in octal %A access rights in human readable form %b number of blocks allocated (see %B) %B the size in bytes of each block reported by %b %C SELinux security context string %d device number in decimal %D device number in hex %f raw mode in hex %F file type %g group ID of owner %G group name of owner %h number of hard links %i inode number %m mount point %n file name %N quoted file name with dereference if symbolic link %o optimal I/O transfer size hint %s total size, in bytes %t major device type in hex, for character/block device special files %T minor device type in hex, for character/block device special files %u user ID of owner %U user name of owner %w time of file birth, human-readable; - if unknown %W time of file birth, seconds since Epoch; 0 if unknown %x time of last access, human-readable %X time of last access, seconds since Epoch %y time of last modification, human-readable %Y time of last modification, seconds since Epoch %z time of last change, human-readable %Z time of last change, seconds since Epoch Valid format sequences for file systems: %a free blocks available to non-superuser %b total data blocks in file system %c total file nodes in file system %d free file nodes in file system %f free blocks in file system %i file system ID in hex %l maximum length of filenames %n file name %s block size (for faster transfers) %S fundamental block size (for block counts) %t file system type in hex %T file system type in human readable form NOTE: your shell may have its own version of stat, which usually supersedes the version described here. Please refer to your shells documentation for details about the options it supports. GNU coreutils online help: http://www.gnu.org/software/coreutils/ Report stat translation bugs to http://translationproject.org/team/ For complete documentation, run: info coreutils stat invocation purpleEndurer bash $ 2.1 stat命令的功能
显示文件或文件系统状态。 2.2 stat命令的格式 stat [选项]... 文件... 注意
1.使用stat命令必须指定要显示信息的文件 purpleEndurer bash $ stat stat: missing operand Try stat --help for more information. purpleEndurer bash $
2.不同的Linux shell 可能会有自己的 stat 版本请参阅自己所用的 shell 的文档了解有关其支持的选项的详细信息。
2.3 stat命令的选项
选项功能 -L 或 --dereference 取消对引用跟随符号链接显示并显示该符号链接指向的文件的信息 -f 或 --file-system 显示文件系统状态而不是文件状态 -c FORMAT 或 --formatFORMAT 使用指定的 FORMAT 而不是默认值; 每次使用 FORMAT 后输出换行符--printfFORMAT与 --format 类似但解释反斜杠转义并且不输出强制性的尾随换行符; 如果需要换行符请在 FORMAT 中包含 n -t 或 --terse 以简洁的形式打印信息--help显示此帮助并退出--version输出版本信息并退出 3 stat命令使用实例
3.1 stat 文件或目录查看文件或目录信息
例3.1.1在当前目录下用mkdir命令创建test目录并用stat查看test目录的信息 purpleEndurer bash $ mkdir test purpleEndurer bash $ stat test File: ‘test’ Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 4ch/76d Inode: 1319986 Links: 2 Access: (0775/drwxrwxr-x) Uid: ( 1000/ csdn) Gid: ( 1000/ csdn) Access: 2024-01-17 19:23:39.555845513 0800 Modify: 2024-01-17 19:23:39.555845513 0800 Change: 2024-01-17 19:23:39.555845513 0800 Birth: - purpleEndurer bash $ 命令返回了如下信息
File文件名Size文件大小以字节表示Blocks文件占用已分配的块数。IO Block文件系统块大小directory文件类型普通文件、目录、文件系统Device文件所在的设备十六进制和十进制Inode文件所在的 Inode 号Links与文件关联的硬链接数Access (0775/drwxrwxr-x) 数字/符号格式的文件权限UID:所有者的用户ID和用户名GID:所有者的群组ID和组名Access 2024-01-17 19:23:39.555845513 0800上次访问文件的时间Modify 2024-01-17 19:23:39.555845513 0800上次修改文件内容的时间Change 2024-01-17 19:23:39.555845513 0800上次更改文件文件元数据的时间Birth文件状态被更改的的时间戳Linux不支持
例3.1.2 在当前目录创建文件test.txt并用并用stat查看test.txt文件的信息 purpleEndurer bash $ ls test.txt purpleEndurer bash $ stat test.txt File: ‘test.txt’ Size: 27 Blocks: 8 IO Block: 4096 regular file Device: 4ch/76d Inode: 1319971 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ csdn) Gid: ( 1000/ csdn) Access: 2024-01-17 19:40:48.612393492 0800 Modify: 2024-01-17 19:40:48.613393535 0800 Change: 2024-01-17 19:40:48.613393535 0800 Birth: - purpleEndurer bash $
对于文件stat显示的信息不同之处在于
directory普通文件
3.2 stat -t 文件或目录以简洁的形式显示文件或目录状态
例3.2 分别以stat -t 和 stat 命令查看文件test.txt 的状态 purpleEndurer bash $ ls test.txt purpleEndurer bash $ stat -t test.txt test.txt 14 8 81b4 1000 1000 4c 1319933 1 0 0 1705553539 1705553539 1705553539 0 4096 purpleEndurer bash $ stat test.txt File: ‘test.txt’ Size: 14 Blocks: 8 IO Block: 4096 regular file Device: 4ch/76d Inode: 1319933 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ csdn) Gid: ( 1000/ csdn) Access: 2024-01-18 12:52:19.515288704 0800 Modify: 2024-01-18 12:52:19.516288743 0800 Change: 2024-01-18 12:52:19.516288743 0800 Birth: - purpleEndurer bash $
3.3 stat -f 文件或目录显示文件或目录的系统状态而不是文件或目录状态
例3.3.1 查看test目录的文件系统状态 purpleEndurer bash $ stat -f test File: test ID: d52c0294601b7508 Namelen: 255 Type: overlayfs Block size: 4096 Fundamental block size: 4096 Blocks: Total: 7584002 Free: 6796649 Available: 6441294 Inodes: Total: 1933312 Free: 1870694 purpleEndurer bash $
命令返回信息包括以下内容
File文件名。ID十六进制文件系统ID。Namelen 文件名称最大的长度。Fundamental block size 文件系统上每个块大小。Blocks块信息包括以下内容
Total文件系统中的总块数。Free文件系统中可用的块数。Available非root用户可用的可用块数。
Inodes索引节点信息包括以下内容
Total 文件系统中的总索引节点数。Free文件系统中空闲索引节点的数量。 例3.3.2 查看文件test.txt的文件系统状态 purpleEndurer bash $ stat -f test.txt File: test.txt ID: d52c0294601b7508 Namelen: 255 Type: overlayfs Block size: 4096 Fundamental block size: 4096 Blocks: Total: 7584002 Free: 6796654 Available: 6441299 Inodes: Total: 1933312 Free: 1870694 purpleEndurer bash $
3.4 stat -L 文件 或 stat --dereference 文件取消对引用跟随符号链接显示并显示该符号链接指向的文件的信息
例 3.4.1 用stat命令查看文件 /usr/share/zoneinfo/America/Cayman 的信息 purpleEndurer bash $ stat /usr/share/zoneinfo/America/Cayman 文件/usr/share/zoneinfo/America/Cayman - Panama 大小6 块0 IO 块4096 符号链接 设备802h/2050d Inode1986380 硬链接1 权限(0777/lrwxrwxrwx) Uid( 0/ root) Gid( 0/ root) 最近访问2024-01-18 12:30:25.329503131 0800 最近更改2021-05-25 16:19:07.819859046 0800 最近改动2021-05-25 16:19:07.819859046 0800 创建时间-
从命令返回信息可以看到/usr/share/zoneinfo/America/Cayman 的文件类型是符号链接文件大小只有6个字节指向的文件是 Panama
要想获取/usr/share/zoneinfo/America/Cayman 指向的文件 Panama 的信息可以使用 -L选项。
例 3.4.2 用stat -L 命令查看文件 /usr/share/zoneinfo/America/Cayman 的信息 purpleEndurer bash $ stat -L /usr/share/zoneinfo/America/Cayman 文件/usr/share/zoneinfo/America/Cayman 大小203 块8 IO 块4096 普通文件 设备802h/2050d Inode1986451 硬链接1 权限(0644/-rw-r--r--) Uid( 0/ root) Gid( 0/ root) 最近访问2023-05-06 12:37:04.230304278 0800 最近更改2019-10-13 09:36:01.000000000 0800 最近改动2021-05-25 16:19:07.835859046 0800 创建时间- 返回信息表示这个文件的大小是203字节文件类型是普通文件那么这是不是文件 Panama 的信息呢
例 3.4.3 用stat命令查看文件 /usr/share/zoneinfo/America/Panama 的信息 purpleEndurer bash $ stat -L /usr/share/zoneinfo/America/Panama 文件/usr/share/zoneinfo/America/Panama 大小203 块8 IO 块4096 普通文件 设备802h/2050d Inode1986451 硬链接1 权限(0644/-rw-r--r--) Uid( 0/ root) Gid( 0/ root) 最近访问2023-05-06 12:37:04.230304278 0800 最近更改2019-10-13 09:36:01.000000000 0800 最近改动2021-05-25 16:19:07.835859046 0800 创建时间-
命令返回的文件信息与例 3.4.2中的信息是一致的。 3.5 stat -c 格式 文件 或 stat --format格式 文件 或 stat --printf格式自定义输出
stat命令有两个选项可让我们根据需要自定义输出-c 格式 (--format格式和 --printf格式。
其中的格式 可以使用许多文件和文件系统的格式指令 a 以八进制格式显示访问权限。 A 以易于阅读的格式显示访问权限。 b 这是分配的块数请参见B。 B 由b报告的每个块的字节大小。 C 显示SELinux安全上下文字符串。 d 以十进制格式显示设备编号。 D 十六进制格式的设备号。 f 以十六进制显示原始模式。 F 显示文件类型。 g 打印所有者的组ID。 G 打印所有者的组名。 h 显示硬链接数。 i 打印出索引节点号。 m 打印安装点。 n 显示文件的文件名 N 显示带符号引用的文件名如果使用符号链接则取消引用 o 打印最佳I/O传输大小提示。 s 总大小以字节为单位。 t 主要设备类型十六进制用于字符/块设备特殊文件 T 次要设备类型十六进制用于字符/块设备特殊文件 u 显示所有者的用户ID。 U 打印所有者的用户名。 w 显示文件的产生时间易于阅读–如果未知。 W 打印文件诞生的时间距离纪元后的秒数如果未知则为0。 x 上次访问的时间易于我们理解的格式。 X 上次访问的时间距离纪元以来的秒数。 y 显示上次修改的最后时间便于阅读。 Y 打印上次修改的时间距离纪元以来的秒数。 z 这是上次更改的时间人类可以理解。 Z 最后一次更改的时间距离纪元以来的秒数。 这两个选项的区别在于
当两个或多个文件用作操作符时--format在每个操作数的输出之后自动添加换行符。 --printf解释反斜杠转义字符。
例 3.5.1 用stat -c命令显示Code目录和test.txt 的文件类型、所有者的组ID和组名信息。 purpleEndurer bash $ stat -c %F,%g,%G Code test.txt directory,1000,csdn regular file,1000,csdn purpleEndurer bash $
例3.5.2 用stat --printf命令显示Code目录和test.txt 的文件类型、所有者的组ID和组名信息。 purpleEndurer bash $ stat --printf %F,%g,%G Code test.txt directory,1000,csdnregular file,1000,csdnpurpleEndurer bash $
可见返回的目录和文件信息在同一行不容易区分因此我们可以改进一下在格式串末尾加上换行符\n purpleEndurer bash $ stat --printf %F,%g,%G\n Code test.txt directory,1000,csdn regular file,1000,csdn purpleEndurer bash $
这样看起来就清晰多了。
4 其它查看Inodes的命令
除了上节介绍的df -i或--inodes命令以及本节介绍的 stat命令我们还可以使用ls -i命令。
例如查看当目前下的目录和文件的inodes purpleEndurer bash $ ls -i 1051744 Code 1319965 test.txt purpleEndurer bash $