Useful Commands/Date & Time

From Fundamental Ramen
Jump to navigation Jump to search
情境 指令
取得本地時間
date
取得 UTC 時間
date -u
取得 Timestamp
date +'%s'
產生 ISO 8601 時間字串
date -u +'%FT%TZ'
顯示目前的時區
date +'%Z'
以日期命名備份檔
BKFILE=`date +'wiki-%Y%m%d.sqlite.gz'`
取得上週的日期
date -r $((`date +%s`-86400*7))
crontab 裡面使用 date
date +"\%F \%T"
(注意 % 字元之前需要使用 \ 字元 escape)
取得檔案的 mtime 並且輸出成日期格式
stat -f '%Sm' -t '%Y%m%d' test.sql
取得檔案的 mtime 並且輸出成 ISO 8601 格式
stat -f '%Sm' -t '%F %T' test.sql
取得檔案的 mtime 並且輸出 Timestamp
stat -f '%Dm' test.sql
stat -c '%Y' test.sql
stat -c '%Y %n' *.gz
與時間伺服器對時(台灣)
sudo ntpdate time.stdtime.gov.tw
sudo ntpdate clock.stdtime.gov.tw
變更系統時區
sudo dpkg-reconfigure tzdata
sudo service cron restart    # 做完記得要重開 cron 服務,否則排程時間會亂掉