在 Linux 系统中,文件有三种主要的时间戳属性,可以通过多种命令精确查看:
stat
命令(最详细)stat filename
输出示例:
File: filename
Size: 1024 Blocks: 8 IO Block: 4096 regular file
Access: 2023-08-15 14:30:00.000000000 +0800
Modify: 2023-08-15 14:25:00.000000000 +0800
Change: 2023-08-15 14:35:00.000000000 +0800
Birth: 2023-08-15 14:20:00.000000000 +0800
ls
命令ls -l # 显示修改时间(mtime)
ls -lu # 显示访问时间(atime)
ls -lc # 显示变更时间(ctime)
添加 --full-time
获取纳秒级精度:
ls -l --full-time filename
将 Unix 时间戳转换为可读格式:
date -d @$(stat -c %Y filename) +"%Y-%m-%d %H:%M:%S"
stat -c %y /path/to/directory # 目录的mtime
stat -c %x filename # atime
stat -c %y filename # mtime
stat -c %z filename # ctime
stat -c %w filename # btime
批量查看文件时间:
find /path -type f -exec stat -c '%n %y' {} \;
查看时间戳的原始数值(Unix时间戳):
stat -c %X filename # atime (秒)
stat -c %Y filename # mtime (秒)
stat -c %Z filename # ctime (秒)
这些方法可以满足从简单查看到精确分析的各种需求。