1、stat命令介绍
获取文件的状态信息,比如:大小、修改时间、所有者······
dai@ubuntu:~$ stat ./test.sh
File: ‘./test.sh’
Size: 745 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 2367314 Links: 1
Access: (0775/-rwxrwxr-x) Uid: ( 1000/ dai) Gid: ( 1000/ dai)
Access: 2022-03-13 00:00:37.136017199 +0800
Modify: 2022-03-13 00:00:31.056017314 +0800
Change: 2022-03-13 00:00:33.200017274 +0800
Birth: -
2、获取文件大小
stat --format=%s ./test.sh
3、获取文件的所有者
stat --format=%U ./test.sh
4、显示最后修改时间
stat --format=%y ./test.sh
5、示例用法
dai@ubuntu:~$ stat ./test.sh
File: ‘./test.sh’
Size: 745 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 2367314 Links: 1
Access: (0775/-rwxrwxr-x) Uid: ( 1000/ dai) Gid: ( 1000/ dai)
Access: 2022-03-13 00:00:37.136017199 +0800
Modify: 2022-03-13 00:00:31.056017314 +0800
Change: 2022-03-13 00:00:33.200017274 +0800
Birth: -
dai@ubuntu:~$
dai@ubuntu:~$ stat --format=%s ./test.sh
745
dai@ubuntu:~$
dai@ubuntu:~$ stat --format=%U ./test.sh
dai
dai@ubuntu:~$
dai@ubuntu:~$ stat --format=%y ./test.sh
2022-03-13 00:00:31.056017314 +0800
dai@ubuntu:~$
6、在脚本里的常见用法
#获取文件大小并判断是否超过允许的最大值
TARGET_FILE=./DependInfo.xml
MAX_FILE_SIZE=1000
FILE_SIZE=$(stat --format=%s TARGET_FILE)
if [ $FILE_SIZE -gt $MAX_FILE_SIZE ]; then
echo "$TARGET_FILE($FILE_SIZE) too large, must <= $MAX_FILE_SIZE ."
exit 1
fi