在 CentOS(或其他 Linux 系统)中,“比对时间戳”通常有以下几种常见场景,下面分别说明:
ls -l filename # 修改时间(mtime)
ls -lu filename # 访问时间(atime)
ls -lc filename # 状态改变时间(ctime)
更详细:
stat filename
if [ file1 -nt file2 ]; then
echo "file1 比 file2 新"
fi
if [ file1 -ot file2 ]; then
echo "file1 比 file2 旧"
fi
find . -newer filename
date -d "2024-01-01 12:00:00" +%s
示例比对:
t1=$(date -d "2024-01-01 12:00:00" +%s)
t2=$(date -d "2024-06-01 12:00:00" +%s)
if [ $t1 -lt $t2 ]; then
echo "t1 早于 t2"
fi
now=$(date +%s)
target=$(date -d "2024-12-31 23:59:59" +%s)
if [ $now -gt $target ]; then
echo "已过期"
fi
ls -lt # 按修改时间倒序
ls -ltr # 正序
touch 设置/对比时间戳(高级)touch -d "2024-01-01" ref_file
find . -newer ref_file
如果你指的是某个具体场景(比如:日志时间戳比对、cron 时间、数据库时间、Ansible、Shell 脚本等),可以告诉我,我可以给你更精确的示例。