【Linux常見命令】touch命令


touch - change file timestamps

touch [文件名] 就是“摸”一下文件,如果文件不存在,就建立新文件;如果文件存在,就改變文件的訪問時間atime等時間戳信息。

 

語法:
  touch [OPTION]... FILE...

  touch [-acfm][-d<日期時間>][-r<參考文件或目錄>] [-t<日期時間>][--help][--version][文件或目錄…]

 

參數:

  • -a 改變檔案的讀取時間記錄。
    • [root@oldboy oldboy]# stat new.txt
        File: `new.txt'
        Size: 120             Blocks: 8          IO Block: 4096   regular file
      Device: 803h/2051d      Inode: 275539      Links: 1
      Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
      Access: 2019-11-06 22:10:48.234818898 +0800
      Modify: 2019-11-07 18:48:17.389776861 +0800
      Change: 2019-11-07 18:48:17.390776816 +0800
      
      [root@oldboy oldboy]# touch new.txt -a
      
      # touch -a修改了atime和ctime
      [root@oldboy oldboy]# stat new.txt    
        File: `new.txt'
        Size: 120             Blocks: 8          IO Block: 4096   regular file
      Device: 803h/2051d      Inode: 275539      Links: 1
      Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
      Access: 2019-11-07 19:49:23.350646538 +0800
      Modify: 2019-11-07 18:48:17.389776861 +0800
      Change: 2019-11-07 19:49:23.350646538 +0800
      touch -a的效果

       

  • -m 改變檔案的修改時間記錄。
    • [root@oldboy oldboy]# stat new.txt    
        File: `new.txt'
        Size: 120             Blocks: 8          IO Block: 4096   regular file
      Device: 803h/2051d      Inode: 275539      Links: 1
      Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
      Access: 2019-11-07 19:49:23.350646538 +0800
      Modify: 2019-11-07 18:48:17.389776861 +0800
      Change: 2019-11-07 19:49:23.350646538 +0800
      
      [root@oldboy oldboy]# touch new.txt -m
      
      # touch -m 修改了mtime和ctime
      [root@oldboy oldboy]# stat new.txt 
        File: `new.txt'
        Size: 120             Blocks: 8          IO Block: 4096   regular file
      Device: 803h/2051d      Inode: 275539      Links: 1
      Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
      Access: 2019-11-07 19:49:23.350646538 +0800
      Modify: 2019-11-07 19:51:19.971719215 +0800
      Change: 2019-11-07 19:51:19.971719215 +0800
      touch -m的效果

       

  • -c,--no-create 
    • do not create any files
    • 假如目的檔案不存在,不會建立新的檔案。
    • [root@oldboy oldboy]# touch -c nn
      [root@oldboy oldboy]# ls nn
      ls: cannot access nn: No such file or directory
      touch -c不創建文件

       

  • -f
    • ignored
    • 不使用,是為了與其他 unix 系統的相容性而保留。
  • -r,--reference=FILE
    • use this file's times instead of current time
    • 使用參考檔的時間記錄,與 --file 的效果一樣。
  • -d,--date=STRING
    • parse STRING and use it instead of current time
    • 設定時間與日期,可以使用各種不同的格式。
  • -t
    • use [[CC]YY]MMDDhhmm[.ss] instead of current time
    • 設定檔案的時間記錄,格式與 date 指令相同。

 

示例:

1. 查看touch file的前后結果,發現修改了atime,ctime和mtime,修改了文件的時間屬性。

[root@oldboy oldboy]# stat name.txt
  File: `name.txt'
  Size: 35              Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 275549      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-11-07 19:46:37.014647959 +0800
Modify: 2019-11-07 19:46:37.014647959 +0800
Change: 2019-11-07 19:46:37.014647959 +0800

[root@oldboy oldboy]# touch name.txt

[root@oldboy oldboy]# stat name.txt 
  File: `name.txt'
  Size: 35              Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 275549      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-11-07 19:47:19.574647813 +0800
Modify: 2019-11-07 19:47:19.574647813 +0800
Change: 2019-11-07 19:47:19.574647813 +0800

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM