Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux find 刪除 N 天前檔案指令詳解:mtime、類型過濾、stat/touch 時間操作

編輯:Linux 教學

核心指令

# 10 天前 (不含第 10 天) 的檔案
find -mtime +10 -exec rm -rfv {} \;

# 第 10 天當天的檔案
find -mtime 10 -exec rm -rfv {} \;

# 10 天內 (含今天,不含第 10 天) 的檔案
find -mtime -10 -exec rm -rfv {} \;

關鍵參數說明

  • -mtime +n:修改時間超過 n 天 (不含第 n 天)
  • -mtime n:修改時間恰好在第 n 天
  • -mtime -n:修改時間在 n 天內 (含今天,不含第 n 天)
  • -type f:僅匹配檔案 (排除目錄 d、連結 l)
  • -exec cmd {} \;:對每個匹配項執行命令

輔助時間工具

stat filename                    # 查看檔案 atime/mtime/ctime
touch -m -t "YYMMDDhhmm" filename # 修改 mtime 到指定時間

警告:rm -rfv 執行不可逆,建議先用 find ... -ls 預覽再執行刪除。

Copyright © Windows教程網 All Rights Reserved