touch  修改时间戳(常用于创建空文件)

1、命令格式


touch [options]File ...



2、命令功能

       用于修改文件的时间戳、访问时间、修改时间、改变时间、如果什么参数也不加默认创建一个空文件

Linux文件时间戳(timestamp):

       访问时间:最近一次被访问(被读) access

       修改时间:写数据  modify 改变的文件的内容

       改变时间:改变元数据(文件的属性)  change,metadata,元数据  改变的文件的属性

改变时间会自动变化,当文件访问时,或者修改文件的内容时,改变时间都会发生变化

3、常用选项

-a:修改文件的访问时间为当前时间,如果和-t连用可以修改为指定时间

-c:如果文件不存在,不创建空文件

-h:修改是链接文件的时间戳而非原文件

-m:修改修改时间

-t:修改为指定的时间而非现在时间; [[CC]YY]MMDDhhmm[.ss]

-r:使用指定文件的时间戳,为当前文件的时间戳

4、使用实例

1.创建空文件

命令:touch File



[root@server~]# ls
inittab  test test.tar.gz
[root@server~]# touch new
[root@server~]# ls
inittab  new test  test.tar.gz
[root@server~]#



2、修改文件的访问时间为当前时间

命令:touch -a File


[root@server~]# stat new 
  File: `new'
  Size: 0               Blocks:0          IO Block: 4096   regular empty file
Device:fd01h/64769d    Inode: 527572      Links: 1
Access:(0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access:2016-12-21 10:39:03.988012456 +0800
Modify:2016-12-21 10:39:03.988012456 +0800
Change:2016-12-21 10:39:03.988012456 +0800
[root@server~]# touch -a new 
[root@server~]# stat new
  File: `new'
  Size: 0               Blocks:0          IO Block: 4096   regular empty file
Device:fd01h/64769d    Inode: 527572      Links: 1
Access:(0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access:2016-12-21 10:40:26.673996182 +0800
Modify:2016-12-21 10:39:03.988012456 +0800
Change:2016-12-21 10:40:26.673996182 +0800
[root@server~]#

3、更新文件时间戳与另一个文件相同

命令:touch -r File1 File2



[root@server~]# stat test
  File: `test'
  Size: 8               Blocks:8          IO Block: 4096   regular file
Device:fd01h/64769d    Inode: 527575      Links: 1
Access:(0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access: 2016-12-21 10:30:40.233999427 +0800
Modify:2016-12-20 09:54:30.885994856 +0800
Change: 2016-12-21 10:30:40.233999427 +0800
[root@server~]# touch -r test new
 [root@server ~]# stat test
  File: `test'
  Size: 8               Blocks:8          IO Block: 4096   regular file
Device:fd01h/64769d    Inode: 527575      Links: 1
Access:(0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access:2016-12-21 10:30:40.233999427 +0800
Modify:2016-12-20 09:54:30.885994856 +0800
Change:2016-12-21 10:41:59.919007361 +0800



4、修改访问文件时间戳为指定的时间

命令:touch -at  [[CC]YY]MMDDhhmm[.ss] File ...


[root@server~]# touch -at 1612121212 new test
[root@server~]# stat new
  File: `new'
  Size: 0               Blocks:0          IO Block: 4096   regular empty file
Device:fd01h/64769d    Inode: 527572      Links: 1
Access:(0644/-rw-r--r--)  Uid: (    0/   root)   Gid: (    0/   root)
Access:2016-12-12 12:12:00.000000000 +0800
Modify:2016-12-21 10:14:42.864999624 +0800
Change:2016-12-21 10:48:16.047994796 +0800


说明:

-t  time 使用指定的时间值

       time作为指定文件相应时间戳记的新值.此处的 time规定为如下形式的十进制数:      

  [[CC]YY]MMDDhhmm[.SS]     

  CC为年数中的前两位,YY为年数的后两位;

如果不给出CC的值

则touch   将把年数设置为当前系统的上的年;

MM为月数,DD为天数,

hh 为小时数(几点),

mm为分钟数,

SS为秒数.

stat显示文件的时间戳

命令:stat File ...



转载于:https://blog.51cto.com/geekb0y/1884631