預設 ls -l 僅顯示「月 日 時:分」格式,若檔案在同年同月同日同分鐘內多次修改,無法分辨先後順序。需精確到秒(甚至奈秒)時,需使用額外參數或工具。
$ ls -l --full-time
-rw------- 1 root root 1463 2012-07-25 12:56:42.000000000 +0800 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 2013-02-22 14:38:56.000000000 +0800 demo
drwxr-xr-x 2 root root 4096 2012-07-25 21:17:04.000000000 +0800 Desktop
輸出格式:YYYY-MM-DD HH:MM:SS.奈秒 時區,完整保留奈秒級精度。
$ stat jms
File: 'jms'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: fd00h/64768d Inode: 16253060 Links: 2
Access: (0755/drwxr-xr-x) Uid: (0/root) Gid: (0/root)
Access: 2013-02-22 15:15:24.000000000 +0800
Modify: 2013-02-22 15:15:24.000000000 +0800
Change: 2013-02-22 15:15:24.000000000 +0800
三種時間含義:
# 取得修改時間 (Unix timestamp,含奈秒)
stat -c '%Y.%N' file.txt
# 輸出範例: 1677000000.123456789
# 格式化輸出 (僅秒級)
stat -c '%y' file.txt | cut -d'.' -f1
# 輸出: 2023-02-22 14:38:56
# 批次處理:找出最近 60 秒內修改的檔案
find /path -type f -newermt "$(date -d '-60 seconds' '+%Y-%m-%d %H:%M:%S')"
# 設定指定日期時間 (含秒)
touch -d "2023-02-22 14:38:56" file.txt
# 使用 timestamp 格式
touch -t 202302221438.56 file.txt
| 指令 | 精度 | 適用場景 |
|---|---|---|
ls -l | 分鐘 | 一般檢視 |
ls -l --full-time | 奈秒 | 需完整時間戳記錄/比對 |
stat | 奈秒 | 需分析 atime/mtime/ctime 差異 |
stat -c '%Y' | 秒 | 腳本運算、時間比較 |