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 touch 命令詳解:修改文件時間戳與創建空文件

編輯:Linux 教學

touch 不僅能創建空文件,更是修改文件訪問時間、修改時間的核心工具。本文系統整理參數含義、時間格式規範、引用文件時間戳及批量操作實例。

命令語法

touch [選項] 文件...

核心參數表

參數功能
-a僅修改訪問時間
-m僅修改修改時間
-c / --no-create文件不存在時不創建
-r file / --file=file引用指定文件的時間戳
-d time / --date=time設置指定時間(支持多種格式)
-t STAMP設置時間戳,格式 [[CC]YY]MMDDhhmm[.ss]
--time=WORD指定時間類型:atime/access/use 或 mtime/modify

時間格式示例

  • 標準格式:touch -t 050116082011 file(2011年5月1日 16:08)
  • 自然語言:
    touch -d "2011-05-01 16:08" file
    touch -d "4:08pm 05/01/2011" file
    touch -d "16:08" file           # 今日指定時間
    touch -d "20110501" file        # 日期默認 00:00:00
  • 引用文件:touch -r referencefile targetfile

常用場景

  1. 創建空文件:touch file1 file2
  2. 批量更新目錄下所有文件時間至當前:touch *
  3. 同步文件時間戳:touch -r source.txt dest.txt
  4. 腳本中判斷文件是否存在(配合 -c):touch -c flagfile

注意事項

  • 無參數時默認更新 atime 和 mtime 為當前時間
  • 配合 find 可批量修改:find /path -name "*.log" -exec touch -d "2023-01-01" {} \;
  • 文件系統需支持相應時間屬性(ext4/xfs 全支持)
Copyright © Windows教程網 All Rights Reserved