stat 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

stat 命令安装:


-bash: stat command not found

#Debian
apt-get install coreutils

#Ubuntu
apt-get install coreutils

#Alpine
apk add coreutils

#Arch Linux
pacman -S coreutils

#Kali Linux
apt-get install coreutils

#CentOS
yum install coreutils

#Fedora
dnf install coreutils

#OS X
brew install coreutils

#Raspbian
apt-get install coreutils

#Docker
docker run cmd.cat/stat stat

stat 命令补充说明:


stat 命令用于显示文件的状态信息。stat 命令的输出信息比 ls 命令的输出信息要更详细。

stat 命令语法:


stat [OPTION]... FILE...

stat 命令选项:


-L, --dereference:支持符号连接;
-f, --filesystem:显示文件系统状态而非文件状态;
-t, --terse:以简洁方式输出信息;
-c, --format=FORMAT 使用指定的格式而不是默认格式
-Z, --context 打印 SELinux 安全上下文
--help:显示指令的帮助信息;
--version:显示指令的版本信息。

stat 命令参数:


文件:指定要显示信息的普通文件或者文件系统对应的设备文件名。

stat 命令实例


$ stat robots.txt 
  File: ‘robots.txt’
  Size: 10              Blocks: 8          IO Block: 4096   regular file
Device: fc00h/64512d    Inode: 269263395   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-11 11:43:30.000000000 +0800
Modify: 2019-10-11 11:43:30.000000000 +0800
Change: 2019-10-11 14:50:45.944664198 +0800
 Birth: -

$ stat -f robots.txt 
  File: "robots.txt"
    ID: fc0000000000 Namelen: 255     Type: xfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 130745984  Free: 101898563  Available: 101898563
Inodes: Total: 261619712  Free: 261412705

$ stat -t robots.txt 
robots.txt 10 8 81a4 0 0 fc00 269263395 1 0 0 1570765410 1570765410 1570776645 0 4096

stat 命令获取文件对应权限的数字:


方法 1:awk + substr

$ stat robots.txt |awk 'NR==4' |awk -F "(" '{print substr($2,2,3)}'   
644

方法 2:awk 多个分割符

$ stat robots.txt |awk 'NR==4' |awk -F '[(0/]' '{print $3}'
644

方法 3:cut

$ stat robots.txt |awk 'NR==4' |cut -c 11-13 
644

#其中所有的awk 'NR==4'的取行命令可用sed -n '4p'代替,效果相同。

方法 4:stat -c %a

%a Access rights in octal - 8进制显示访问权限,0644

$ stat -c %a robots.txt 
644

stat 命令显示文件属性,例如大小,权限,创建和访问日期等:

stat file

$ stat req_ad_pv_81_1325.log 
  File: ‘req_ad_pv_81_1325.log’
  Size: 4458272         Blocks: 16256      IO Block: 4096   regular file
Device: fc00h/64512d    Inode: 540495123   Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   nginx)   Gid: ( 1000/   nginx)
Access: 2020-08-26 13:25:00.018087752 +0800
Modify: 2020-08-26 13:25:34.154193078 +0800
Change: 2020-08-26 13:25:34.154193078 +0800
 Birth: -

与上述相同,但 stat -t 参数更为简洁:

stat -t file

$ stat -t req_ad_pv_81_1325.log 
req_ad_pv_81_1325.log 5115666 16256 81a4 1000 1000 fc00 540495123 1 0 0 1598419500 1598419538 1598419538 0 4096

stat 命令显示文件系统信息:

stat -f file

$ stat -f req_ad_pv_81_1325.log 
  File: "req_ad_pv_81_1325.log"
    ID: fc0000000000 Namelen: 255     Type: xfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 130745984  Free: 94480345   Available: 94480345
Inodes: Total: 261619712  Free: 261389112

stat 命令仅显示八进制文件权限:

stat -c "%a %n" file

$ stat -c "%a %n" req_ad_pv_81_1325.log 
644 req_ad_pv_81_1325.log

stat 命令显示文件的所有者和组:

stat -c "%U %G" file

stat 命令显示文件大小(以字节为单位):

stat -c "%s %n" file

stat 命令扩展阅读:




stat 命令评论