【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
原文地址:https://www.cnblogs.com/zoe233/p/11815178.html