0


Linux shell编程学习笔记40:stat命令

程序员必备的面试技巧

“程序员必备的面试技巧,就像是编写一段完美的代码一样重要。在面试战场上,我们需要像忍者一样灵活,像侦探一样聪明,还要像无敌铁金刚一样坚定。只有掌握了这些技巧,我们才能在面试的舞台上闪耀光芒,成为那个令HR们心动的程序猿!”

目录

  1. 0 前言
  2. 1 DOS、Windows、Linux中的文件存储
    1. 1.1 扇区2. 1.2 DOS、Windows:簇(Cluster)3. 1.3 Linux:块(block)和索引节点(inode)
  3. 2 stat命令的功能、格式和选项说明
    1. 2.1 stat命令的功能2. 2.2 stat命令的格式3. 2.3 stat命令的选项
  4. 3 stat命令使用实例
    1. 3.1 stat 文件或目录:查看文件或目录信息2. 3.2 stat -t 文件或目录:以简洁的形式显示文件或目录状态3. 3.3 stat -f 文件或目录:显示文件或目录的系统状态而不是文件或目录状态4. 3.4 stat -L 文件 或 stat --dereference 文件:取消对引用(跟随)符号链接显示并显示该符号链接指向的文件的信息5. 3.5 stat -c "格式" 文件 或 stat --format="格式" 文件 或 stat --printf="格式":自定义输出
  5. 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 --format=FORMAT use the specified FORMAT instead of the default;
output a newline after each use of FORMAT
--printf=FORMAT 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 shell's 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

--format=FORMAT
使用指定的 FORMAT 而不是默认值; 每次使用 FORMAT 后输出换行符--printf=FORMAT与 --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:块信息,包括以下内容:
  1. Total:文件系统中的总块数。
  2. Free:文件系统中可用的块数。
  3. Available:非root用户可用的可用块数。
  • Inodes:索引节点信息,包括以下内容:
  1. Total :文件系统中的总索引节点数。
  2. 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 Inode:1986380 硬链接: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 Inode:1986451 硬链接: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 Inode:1986451 硬链接: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 $


本文转载自: https://blog.csdn.net/Purpleendurer/article/details/135684614
版权归原作者 紫郢剑侠 所有, 如有侵权,请联系我们删除。

“Linux shell编程学习笔记40:stat命令”的评论:

还没有评论