Ubuntu 中获取与查看时间戳的常用方法
一、当前时间戳
date +%s。date +%s%3N。二、文件的时间戳
ls -l <文件路径>ls -lu <文件路径>ls -lc <文件路径>stat -c %Y <文件路径>(返回自纪元起的秒数)三、时间戳与可读时间的相互转换
date -d @<timestamp>
date -d @1633072800date -d "YYYY-MM-DD HH:MM:SS" +%s
date -d "2021-10-01 12:00:00" +%sdate -d "1 day ago"、date -d "1 hour ago"。四、在脚本中快速使用
start=$(date +%s)end=$(date +%s)echo "Elapsed: $((end - start)) seconds"mtime=$(stat -c %Y /path/to/file)now=$(date +%s)if [ $((now - mtime)) -lt 86400 ]; then echo "Modified within 24h"; fi。