目錄
一、理論
1.stat命令
二、實(shí)驗(yàn)
1.獲取命令幫助
2.查看命令版本
3.查看文件狀態(tài)信息
4.-f查看文件所在文件系統(tǒng)狀態(tài)信息
5.-t以簡潔形式輸出文件信息
6.-L顯示軟連接原始文件狀態(tài)信息
7.獲取文件inode
8.獲取文件所屬用戶或者用戶ID
9.獲取文件所屬用戶組或者用戶組ID
? ? ? ? ?10.獲取文件磁盤掛載點(diǎn)
11.獲取文件訪問時(shí)間
12.獲取文件修改時(shí)間
13.獲取文件更改時(shí)間
14.獲取文件權(quán)限
15.時(shí)間更新演示
一、理論
1.stat命令
(1)概念
stat命令用于顯示文件的狀態(tài)信息。stat命令的輸出信息比ls命令的輸出信息要更詳細(xì)。stat命令可以查看的信息包括:
表1 stst命令查看信息
查看信息 | 描述 |
File | 顯示文件名 |
Size | 顯示文件大小 |
Blocks | 文件使用的數(shù)據(jù)塊總數(shù) |
IO Block | IO塊大小 |
regular file | 文件類型(常規(guī)文件) |
Device | 設(shè)備編號(hào) |
Inode | Inode號(hào) |
Links | 鏈接數(shù) |
Access | 文件的權(quán)限 |
Gid、Uid | 文件所有權(quán)的Gid和Uid |
access time | 最后存取時(shí)間(ls -lu filename),表示最后一次訪問(僅僅是訪問,沒有改動(dòng))文件的時(shí)間 |
modify time | 最后修改時(shí)間(ls -l ?filename?),表示最后一次修改文件的時(shí)間 |
change time | 最后更改時(shí)間(ls -lc filename ),表示最后一次對文件屬性改變的時(shí)間,包括權(quán)限,大小,屬性等等 |
Birth time | 文件創(chuàng)建時(shí)間,crtime,不過據(jù)查此屬性linux已廢棄,目前狀態(tài)顯示結(jié)果均為- |
(2)語法
? ? ? 用法:stat [OPTION]… FILE…
(3)常用參數(shù)選項(xiàng)
表2 stat命令常用選項(xiàng)
參數(shù)選項(xiàng) | 功能 |
-f |
查看文件所在文件系統(tǒng)狀態(tài)信息 |
-t |
以簡潔形式輸出文件信息 |
-L |
顯示軟連接原始文件狀態(tài)信息 |
-c | 按照指定格式輸出 |
(4)文件的有效格式序列
表3 文件有效格式序列
格式符 | 格式符說明 |
%a | 八進(jìn)制中的訪問權(quán)限(777顯示) |
%A | 人類可讀形式的訪問權(quán)(rwx顯示) |
%b | 分配的塊數(shù)(請參閱%B) |
%B | %b報(bào)告的每個(gè)塊的大?。ㄗ止?jié)) |
%C | SELinux安全上下文字符串 |
%d | 十進(jìn)制設(shè)備編號(hào) |
%D | 十六進(jìn)制的設(shè)備編號(hào) |
%f | 十六進(jìn)制原始模式 |
%F | 文件類型 |
%g | 所有者的組ID |
%G | 所有者的組名稱 |
%h | 硬鏈接數(shù) |
%i | inode數(shù) |
%m | 掛載點(diǎn) |
%n | 文件名 |
%N | 帶取消引用(如果是符號(hào)鏈接)的帶引號(hào)的文件名 |
%o | 最佳I/O傳輸大小提示 |
%s | 總大小,以字節(jié)為單位 |
%t | 主要設(shè)備類型(十六進(jìn)制),用于字符/塊設(shè)備特殊文件 |
%T | 次要設(shè)備類型(十六進(jìn)制),用于字符/塊設(shè)備特殊文件 |
%u | 所有者的用戶ID |
%U | 所有者的用戶名 |
%w | 文件出生時(shí)間,人類可讀;-如果未知 |
%W | 文件生成時(shí)間,自紀(jì)元起的秒數(shù);如果未知,則為0 |
%x | 最后一次訪問的時(shí)間,人類可讀 |
X | 上次訪問的時(shí)間,自紀(jì)元起的秒數(shù) |
%y | 上次修改的時(shí)間,人類可讀 |
%Y | 上次修改的時(shí)間,自紀(jì)元起的秒數(shù) |
%z | 最后更改的時(shí)間,人類可讀 |
%Z | 上次更改的時(shí)間,自新紀(jì)元起的秒數(shù) |
(5)文件系統(tǒng)的有效格式序列
表4 文件系統(tǒng)有效格式序列
格式符 | 格式符說明 |
%a | 非超級(jí)用戶可用的空閑塊 |
%b | 文件系統(tǒng)中的數(shù)據(jù)塊總數(shù) |
%c | 文件系統(tǒng)中的文件節(jié)點(diǎn)總數(shù) |
%d | 文件系統(tǒng)中的空閑文件節(jié)點(diǎn) |
%f | 文件系統(tǒng)中的空閑塊 |
%i | 十六進(jìn)制文件系統(tǒng)ID |
%l | 文件名的最大長度 |
%n | 文件名 |
%s | 塊大小(用于更快的傳輸) |
%S | 基本塊大?。ㄓ糜趬K計(jì)數(shù)) |
%t | 十六進(jìn)制文件系統(tǒng)類型 |
%T | 人類可讀形式的文件系統(tǒng)類型 |
二、實(shí)驗(yàn)
1.獲取命令幫助
[root@centos2 /]# stat --help
用法:stat [選項(xiàng)]... 文件...
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 顯示此幫助信息并退出
--version 顯示版本信息并退出
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
2.查看命令版本
[root@centos2 /]# stat --version
stat (GNU coreutils) 8.22
3.查看文件狀態(tài)信息
[root@centos2 /]# stat disk.sh
文件:"disk.sh"
大?。?58 塊:8 IO 塊:4096 普通文件
設(shè)備:fd00h/64768d Inode:1944081 硬鏈接:1
權(quán)限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
環(huán)境:unconfined_u:object_r:etc_runtime_t:s0
最近訪問:2023-06-07 19:40:02.680115173 +0800
最近更改:2023-06-07 19:40:02.680115173 +0800
最近改動(dòng):2023-06-07 19:40:02.682115156 +0800
創(chuàng)建時(shí)間:-
4.-f查看文件所在文件系統(tǒng)狀態(tài)信息
[root@centos2 /]# stat -f disk.sh
文件:"disk.sh"
ID:fd0000000000 文件名長度:255 類型:xfs
塊大?。?096 基本塊大?。?096
塊:總計(jì):13100800 空閑:10966314 可用:10966314
Inodes: 總計(jì):26214400 空閑:25928770
5.-t以簡潔形式輸出文件信息
簡潔形式主要是把每個(gè)值的列頭去掉了,只顯示值,三個(gè)時(shí)間按照紀(jì)元至今秒數(shù)顯示。
[root@centos2 /]# stat -t disk.sh
disk.sh 358 8 81a4 0 0 fd00 90130 1 0 0 1686556088 1686556088 1686556088 0 4096 unconfined_u:object_r:etc_runtime_t:s0
6.-L顯示軟連接原始文件狀態(tài)信息
? 創(chuàng)建軟連接
[root@centos2 /]# ln -s /disk.sh /abc
[root@centos2 /]# stat abc
文件:"abc" -> "/disk.sh"
大小:8 塊:0 IO 塊:4096 符號(hào)鏈接
設(shè)備:fd00h/64768d Inode:90322 硬鏈接:1
權(quán)限:(0777/lrwxrwxrwx) Uid:( 0/ root) Gid:( 0/ root)
環(huán)境:unconfined_u:object_r:root_t:s0
最近訪問:2023-06-12 15:59:29.879388388 +0800
最近更改:2023-06-12 15:59:29.879388388 +0800
最近改動(dòng):2023-06-12 15:59:29.879388388 +0800
創(chuàng)建時(shí)間:-
[root@centos2 /]# stat -L abc
文件:"abc"
大?。?58 塊:8 IO 塊:4096 普通文件
設(shè)備:fd00h/64768d Inode:90130 硬鏈接:1
權(quán)限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
環(huán)境:unconfined_u:object_r:etc_runtime_t:s0
最近訪問:2023-06-12 15:48:08.482270985 +0800
最近更改:2023-06-12 15:48:08.482270985 +0800
最近改動(dòng):2023-06-12 15:48:08.507270807 +0800
創(chuàng)建時(shí)間:-
7.獲取文件inode
[root@centos2 /]# stat -c %i disk.sh
90130
8.獲取文件所屬用戶或者用戶ID
[root@centos2 /]# stat -c %u disk.sh
0
[root@centos2 /]# stat -c %U disk.sh
root
9.獲取文件所屬用戶組或者用戶組ID
[root@centos2 /]# stat -c %G disk.sh
root
[root@centos2 /]# stat -c %g disk.sh
0
10.獲取文件磁盤掛載點(diǎn)
[root@centos2 /]# stat -c %m disk.sh
/
11.獲取文件訪問時(shí)間
[root@centos2 /]# stat -c %x disk.sh
2023-06-12 15:48:08.482270985 +0800
12.獲取文件修改時(shí)間
[root@centos2 /]# stat -c %y disk.sh
2023-06-12 15:48:08.482270985 +0800
13.獲取文件更改時(shí)間
[root@centos2 /]# stat -c %z disk.sh
2023-06-12 16:03:15.402715086 +0800
14.獲取文件權(quán)限
[root@centos2 /]# stat -c %A disk.sh
-rw-r--r--
[root@centos2 /]# ?stat -c %a disk.sh
644
15.時(shí)間更新演示
(1)使用vim命令打開文件,即使不做任何修改,也會(huì)更新mtime
[root@centos2 /]# stat -c %y disk.sh
2023-06-12 16:26:39.579676612 +0800
(2)修改文件權(quán)限或?qū)僦?,ctime更新,atime和mtime不會(huì)更新文章來源:http://www.zghlxwxcb.cn/news/detail-480242.html
[root@centos2 /]# chown david:david disk.sh
[root@centos2 /]# stat disk.sh
文件:"disk.sh"
大小:358 塊:8 IO 塊:4096 普通文件
設(shè)備:fd00h/64768d Inode:409342 硬鏈接:1
權(quán)限:(0644/-rw-r--r--) Uid:( 1005/ david) Gid:( 1005/ david)
環(huán)境:unconfined_u:object_r:etc_runtime_t:s0
最近訪問:2023-06-12 16:26:39.579676612 +0800
最近更改:2023-06-12 16:26:39.579676612 +0800
最近改動(dòng):2023-06-12 16:28:17.400003431 +0800
創(chuàng)建時(shí)間:-
(3)查看文件,atime更新,mtime和ctime不會(huì)更新文章來源地址http://www.zghlxwxcb.cn/news/detail-480242.html
[root@centos2 /]# cat disk.sh
1 time=$(date "+%Y-%m-%d %H:%M:%S")
2 diskuse=`df -h | sed -n '2p' | awk '{print $5}'`
3 disk=`echo "$diskuse" | cut -d "%" -f 1`
4 echo "當(dāng)前硬盤使用率為:$disk%"
5 if [ $disk -gt 15 ]
6 then
7 echo "$time 您當(dāng)前磁盤使用率$disk%,已嚴(yán)重超標(biāo)" | mail -s "email" 7jjw@163.com
8 else
9 exit
10 fi
[root@centos2 /]# stat disk.sh
文件:"disk.sh"
大?。?58 塊:8 IO 塊:4096 普通文件
設(shè)備:fd00h/64768d Inode:409342 硬鏈接:1
權(quán)限:(0644/-rw-r--r--) Uid:( 1005/ david) Gid:( 1005/ david)
環(huán)境:unconfined_u:object_r:etc_runtime_t:s0
最近訪問:2023-06-12 16:31:49.265545417 +0800
最近更改:2023-06-12 16:26:39.579676612 +0800
最近改動(dòng):2023-06-12 16:28:17.400003431 +0800
創(chuàng)建時(shí)間:-
到了這里,關(guān)于Linux命令:stat的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!