在 CentOS(以及大多数 Linux 发行版)中,查看文件时间戳主要有 三种时间:
下面是常用查看方式:
ls 命令(最常用)ls -l 文件名
示例:
ls -l test.txt
输出类似:
-rw-r--r--. 1 root root 123 Apr 8 14:30 test.txt
显示的是 mtime(修改时间)
ls -l --time=atime 文件名 # 访问时间
ls -l --time=ctime 文件名 # 状态变更时间
示例:
ls -l --time=atime test.txt
ls -l --time=ctime test.txt
ls -l --time-style=full-iso 文件名
示例:
ls -l --time-style=full-iso test.txt
输出:
-rw-r--r-- 1 root root 123 2024-04-08 14:30:12.123456789 +0800 test.txt
stat 命令(最详细 ✅ 推荐)stat 文件名
示例:
stat test.txt
输出示例:
File: test.txt
Size: 123 Blocks: 8 IO Block: 4096 regular file
Device: fd02h/64770d Inode: 131072 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/root) Gid: ( 0/root)
Access: 2024-04-08 14:25:10.123456789 +0800
Modify: 2024-04-08 14:30:12.123456789 +0800
Change: 2024-04-08 14:31:00.123456789 +0800
Birth: -
✅ 三种时间戳一目了然
✅ 精度最高
✅ 最推荐用于排错 / 脚本
date + stat(查看某个时间)stat -c %x 文件名 # atime
stat -c %y 文件名 # mtime
stat -c %z 文件名 # ctime
示例:
stat -c %y test.txt
目录同样适用:
ls -ld 目录名
stat 目录名
| 需求 | 推荐命令 |
|---|---|
| 快速查看 | ls -l |
| 精确到秒 | ls -l --time-style=full-iso |
| 查看 atime / mtime / ctime | stat ✅ |
| 脚本获取 | stat -c |
如果你是想 判断文件是否被修改过、被访问过,或用于日志/备份分析,告诉我具体场景,我可以给你更针对性建议。