在Linux系统中,文件通常有三种时间戳(timestamps),用于记录文件的不同状态时间。以下是它们的详细说明:
mtime
)bash
ls -l 文件名 # 默认显示mtime
stat 文件名 # 查看所有时间戳
atime
)cat
、less
)、程序加载文件等操作会更新atime
。bash
ls -lu 文件名 # 显示atime
stat 文件名
relatime
或noatime
挂载选项,会减少atime
更新频率。ctime
)chmod
、chown
),或文件内容修改时(因为mtime
变也会引发元数据变化)。bash
ls -lc 文件名 # 显示ctime
stat 文件名
ctime
并非“创建时间”,而是元数据变更时间。btime
)bash
stat 文件名 # 若文件系统支持,会显示"Birth"时间
ls
无法直接查看btime
,需依赖stat
命令和文件系统支持。时间类型 | 简称 | 触发条件 | 查看命令 |
---|---|---|---|
修改时间 | mtime | 文件内容修改 | ls -l 或 stat |
访问时间 | atime | 文件被读取 | ls -lu 或 stat |
变更时间 | ctime | 元数据或内容修改 | ls -lc 或 stat |
创建时间 | btime | 文件创建时记录 | stat (需文件系统支持) |
stat
命令)$ stat test.txt
File: test.txt
Size: 1024 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 12345678 Links: 1
Access: 2023-10-01 12:00:00.000000000 +0800 # atime
Modify: 2023-09-30 15:30:00.000000000 +0800 # mtime
Change: 2023-09-30 16:45:00.000000000 +0800 # ctime
Birth: 2023-09-28 10:00:00.000000000 +0800 # btime(若支持)
为什么ctime
在修改文件内容后会更新?
因为文件大小等元数据随内容修改而变更,触发了ctime
更新。
如何禁止atime
更新以提升性能?
在挂载文件系统时使用noatime
或relatime
选项(需修改/etc/fstab
)。
如何修改文件时间戳?
使用touch
命令:
touch -a 文件名 # 更新atime
touch -m 文件名 # 更新mtime
touch -t 202310011200 文件名 # 手动指定时间(格式:[[CC]YY]MMDDhhmm[.ss])
如果需要进一步操作或解释,请随时告知!