Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Linux 精確查看檔案時間到秒:ls --full-time 與 stat 指令詳解

編輯:Linux 系統常見問題解答

問題背景

預設 ls -l 僅顯示「月 日 時:分」格式,若檔案在同年同月同日同分鐘內多次修改,無法分辨先後順序。需精確到秒(甚至奈秒)時,需使用額外參數或工具。

方法一:ls --full-time 參數

$ 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 指令 - 取得完整時間屬性

$ 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

三種時間含義:

  • Access (atime):最後讀取/存取時間
  • Modify (mtime):最後內容修改時間
  • Change (ctime):最後狀態改變時間(權限、擁有者、連結數等 metadata 變更)

進階應用:腳本中解析精確時間

# 取得修改時間 (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 精確設定時間

# 設定指定日期時間 (含秒)
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'秒腳本運算、時間比較
Copyright © Windows教程網 All Rights Reserved