在 Linux 系统中,每个文件都有三种时间戳记录,理解这些时间对于系统管理和故障排查非常重要。以下是查看和分析文件时间的各种方法:
修改时间 (mtime, Modification time)
ls -l
默认显示的时间访问时间 (atime, Access time)
ls -lu
查看变更时间 (ctime, Change time)
ls -lc
查看ls
命令# 查看修改时间(mtime)
ls -l 文件名
# 查看访问时间(atime)
ls -lu 文件名
# 查看变更时间(ctime)
ls -lc 文件名
# 查看完整时间信息(包括毫秒)
ls --full-time 文件名
stat
命令(最详细)stat 文件名
输出示例:
File: test.txt
Size: 1024 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 12345678 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ user) Gid: ( 1000/ user)
Access: 2023-04-15 10:30:45.123456789 +0800
Modify: 2023-04-10 14:25:30.987654321 +0800
Change: 2023-04-10 14:26:05.456789123 +0800
Birth: 2023-04-01 09:00:00.000000000 +0800
find
命令按时间查找# 查找7天内修改过的文件
find /path -type f -mtime -7
# 查找超过30天未访问的文件
find /path -type f -atime +30
# 查找24小时内变更过的文件
find /path -type f -ctime -1
YYYY-MM-DD HH:MM:SS
是标准格式-mtime -n
:n天以内-mtime n
:正好n天前-mtime +n
:n天以前比较文件时间
[ file1 -nt file2 ] && echo "file1 is newer than file2"
[ file1 -ot file2 ] && echo "file1 is older than file2"
只显示时间而不显示其他信息
stat -c %y 文件名 # 修改时间
stat -c %x 文件名 # 访问时间
stat -c %z 文件名 # 变更时间
查看目录的时间
stat 目录名
批量查看多个文件时间
stat -c "%n %y" /path/to/files/*
noatime
挂载选项)掌握这些文件时间查看方法,可以帮助你更好地进行文件管理、日志分析和系统维护工作。