touch 作用:修改文件时间戳属性。若文件不存在则创建空文件。
 
参数选项:
-a 或--time=atime或--time=access  只更改指定文件的最后访问时间。
-d STRING 使用字符串STRING代表的时间作为模板设置指定文件的时间属性。
-m 或--time=mtime或--time=modify 只更改指定文件的最后修改时间。注意:modify time修改后,change time也被修改(因为状态发生了改变)。
-c 仅修改文件的时间参数(3 个时间参数都改变),如果文件不存在,则不建立新文件。
-r file 将指定文件的时间属性设置为与模板文件file的时间属性相同。
-t STAMP 使用[[CC]YY]MMDDhhmm[.ss] 格式的时间设置文件的时间属性。(CC、YY、ss是可选的)
这里,CC为年数中的前两位,即“世纪数”;YY为年数的后两位,即某世纪中的年数。
如果不给出CC的值,则touch 将把年数CCYY限定在1969--2068之内,MM为月数,DD为天数,hh 为小时数,mm为分钟数,SS为秒数。
此处秒的设定范围是0-61,这样可以处理闰秒。这些数字组成的时间是环境变量TZ指定的时区中的一个时间。
由于系统的限制,早于1970年1月1日的时间是错误的。我用的系统是经过修改的,所有会出现000001010000。
 
说明:stat 命令可以查看文件的时间戳属性,Access 为访问时间,Modify 为修改时间,Change 为状态改变时间。
mtime:最后修改时间(ls -lt),修改文件内容,文件的修改时间(modify time)会改变。
ctime:状态改变时间(ls -lc),修改文件内容,移动文件或改变文件属性等,文件的change 时间会改变。
atime:最后访问时间(ls -lu),查看文件内容时,文件的访问时间(access time)会改变。
说明:如果不加任何参数,直接touch 文件,将同时修改三个值,如下所示
[root@testdb62 ~]# stat 1.log
  File: ‘1.log’
  Size: 36036         Blocks: 72         IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 139869387   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-11-20 14:51:42.378307720 +0800
Modify: 2020-11-20 14:51:35.671279728 +0800
Change: 2020-11-20 14:51:35.673279736 +0800
 Birth: -
[root@testdb62 ~]# touch 1.log
[root@testdb62 ~]# stat 1.log
  File: ‘1.log’
  Size: 36036         Blocks: 72         IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 139869387   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-12-14 14:42:56.612288531 +0800
Modify: 2020-12-14 14:42:56.612288531 +0800
Change: 2020-12-14 14:42:56.612288531 +0800
 Birth: -
[root@testdb62 ~]# date
Mon Dec 14 14:43:02 CST 2020
 
 
范例. 更改文件的时间戳属性。
[root@testdb62 ~]# stat test.txt
  File: ‘test.txt’
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 137362373   Links: 1
Access: (4644/-rwSr--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-12-08 16:47:09.231428037 +0800
Modify: 2020-12-08 16:47:09.231428037 +0800
Change: 2020-12-08 16:47:34.945548057 +0800
 Birth: -
[root@testdb62 ~]# touch -a test.txt 
[root@testdb62 ~]# stat test.txt
  File: ‘test.txt’
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 137362373   Links: 1
Access: (4644/-rwSr--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-12-14 15:26:28.438551170 +0800
Modify: 2020-12-08 16:47:09.231428037 +0800
Change: 2020-12-14 15:26:28.438551170 +0800
 Birth: -
[root@testdb62 ~]# touch -m test.txt 
[root@testdb62 ~]# stat test.txt
  File: ‘test.txt’
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 137362373   Links: 1
Access: (4644/-rwSr--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-12-14 15:26:28.438551170 +0800
Modify: 2020-12-14 15:26:37.886595679 +0800
Change: 2020-12-14 15:26:37.886595679 +0800
 Birth: -
 
范例. 选项 -d 指定创建文件后的文件修改时间
[root@testdb62 ~]# ls -l --full-time scp.cmd 
-rw-r--r--. 1 root root 73 2020-10-12 18:25:47.615248867 +0800 scp.cmd
[root@testdb62 ~]# touch -d 20121212 scp.cmd 
[root@testdb62 ~]# ls -l --full-time scp.cmd 
-rw-r--r--. 1 root root 73 2012-12-12 00:00:00.000000000 +0800 scp.cmd
范例. 选项 -r 修改 cpdir.txt 时间属性,使其和 a.txt 的时间属性一致
[root@testdb62 ~]# ls -l --full-time scp.cmd 
-rw-r--r--. 1 root root 73 2012-12-12 00:00:00.000000000 +0800 scp.cmd
[root@testdb62 ~]# ls -l --full-time test.txt 
-rwSr--r-- 1 root root 0 2012-12-12 00:00:00.000000000 +0800 test.txt
[root@testdb62 ~]# touch -r scp.cmd test.txt 
[root@testdb62 ~]# ls -l --full-time test.txt 
-rwSr--r-- 1 root root 0 2012-12-12 00:00:00.000000000 +0800 test.txt
 
范例. 选项 -t 将文件设置为 201506242318.12 时间格式
[root@testdb62 ~]# ls -l --full-time scp.cmd 
-rw-r--r--. 1 root root 73 2012-12-12 00:00:00.000000000 +0800 scp.cmd
[root@testdb62 ~]# touch -t 201506242318.12 scp.cmd 
[root@testdb62 ~]# ls -l --full-time scp.cmd 
-rw-r--r--. 1 root root 73 2015-06-24 23:18:12.000000000 +0800 scp.cmd