温馨提示×

centos怎么查看文件时间戳

小樊
38
2026-08-03 04:44:24
栏目: 智能运维

在 CentOS(以及大多数 Linux 发行版)中,查看文件时间戳主要有 三种时间

  • atime(Access Time):最后访问时间
  • mtime(Modify Time):最后内容修改时间
  • ctime(Change Time):最后状态变更时间(权限、属主等)

下面是常用查看方式:


1️⃣ 使用 ls 命令(最常用)

查看修改时间(mtime)

ls -l 文件名

示例:

ls -l test.txt

输出类似:

-rw-r--r--. 1 root root 123 Apr  8 14:30 test.txt

显示的是 mtime(修改时间)


查看详细时间戳(含 atime / ctime)

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

2️⃣ 使用 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: -

✅ 三种时间戳一目了然
✅ 精度最高
✅ 最推荐用于排错 / 脚本


3️⃣ 使用 date + stat(查看某个时间)

stat -c %x 文件名   # atime
stat -c %y 文件名   # mtime
stat -c %z 文件名   # ctime

示例:

stat -c %y test.txt

4️⃣ 查看目录时间戳

目录同样适用:

ls -ld 目录名
stat 目录名

✅ 总结对比

需求 推荐命令
快速查看 ls -l
精确到秒 ls -l --time-style=full-iso
查看 atime / mtime / ctime stat
脚本获取 stat -c

如果你是想 判断文件是否被修改过、被访问过,或用于日志/备份分析,告诉我具体场景,我可以给你更针对性建议。

0