在 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 -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 -lt $target ]; then
echo "还没到目标时间"
fi
find /path -type f -newermt "2024-01-01"
或比某个文件新:
find /path -type f -newer oldfile
#!/bin/bash
file1=m1.txt
file2=m2.txt
if [ "$file1" -nt "$file2" ]; then
echo "$file1 更新"
else
echo "$file2 更新"
fi
如果你指的是:
可以告诉我具体场景,我可以给你更精确的示例。