在 CentOS(Linux)里“对比时间戳”通常有几种常见场景,下面按使用频率给你说明:
ls -l filename
或查看更详细的时间(包括访问、修改、状态改变):
stat filename
if [ file1 -nt file2 ]; then
echo "file1 比 file2 新"
else
echo "file1 不比 file2 新"
fi
-nt :newer than-ot :older thandate +%s # 秒
date +%s%3N # 毫秒(CentOS 6 可能不支持)
t1=1700000000
t2=1700003600
if [ "$t1" -lt "$t2" ]; then
echo "t1 早于 t2"
fi
例如日志格式:
2024-11-15 10:20:30
t1=$(date -d "2024-11-15 10:20:30" +%s)
t2=$(date -d "2024-11-15 11:00:00" +%s)
[ "$t1" -lt "$t2" ] && echo "t1 更早"
if [[ "2024-11-15 10:20:30" < "2024-11-15 11:00:00" ]]; then
echo "前面时间更早"
fi
# 找出 1 天前修改过的文件
find /path -type f -mtime +1
# 找出 1 小时内修改的文件
find /path -type f -mmin -60
date 不支持 %N(毫秒)如果你能说清楚:
我可以给你更精确的示例。