在 CentOS(本质是 Linux)里,“比较时间戳”通常指比较文件的 mtime/ctime/atime,或者比较日期时间字符串 / 秒级时间戳。下面按常见场景说明。
ls 看时间戳ls -l file1 file2 # mtime
ls -lu file1 file2 # atime
ls -lc file1 file2 # ctime
test / [ ] 比较文件新旧if [ file1 -nt file2 ]; then
echo "file1 比 file2 新"
fi
if [ file1 -ot file2 ]; then
echo "file1 比 file2 旧"
fi
if [ file1 -ef file2 ]; then
echo "是同一个文件"
fi
-nt:newer than-ot:older thanfind 按时间戳筛选# 修改时间在 1 天以内
find /path -mtime -1
# 修改时间在 1 天以前
find /path -mtime +1
# 30 分钟以内修改过
find /path -mmin -30
date +%s
stat -c %Y file
t1=$(stat -c %Y file1)
t2=$(stat -c %Y file2)
if [ "$t1" -gt "$t2" ]; then
echo "file1 更新"
fi
t1=$(date -d "2024-01-01 10:00:00" +%s)
t2=$(date -d "2024-01-02 10:00:00" +%s)
if [ "$t1" -lt "$t2" ]; then
echo "t1 更早"
fi
if [[ "2024-01-02" > "2024-01-01" ]]; then
echo "前面更大"
fi
⚠️ 仅适用于 YYYY-MM-DD HH:MM:SS 这种字典序相等的格式。
find /data -type f ! -newer reference.txt
如果你能说明:
我可以给你更精确的写法。