文件时间戳概念介绍

  

    文件的时间戳包含在它的元数据中,属于其本身属性信息。

    文件的时间戳包含有三种时间分别如下:

    acess time  访问时间 

    modify time 修改时间(更具体说是修改数据时的时间) 

    change time 改变时间 (修改元数据的时间)

    modify time以下简写为mtime,mtime与ctime是不同的,当文件的属性信息发生改变比如文件名,文件路径,文件属主等其改变的是ctime;当文件的内容发生改动则是mtime发生变化。



   科普

       元数据的概念:

元数据(Metadata),又称中介数据中继数据,为描述数据的数据(data about data),主要是描述数据属性(property)的信息,用来支持如指示存储位置、历史数据、资源查找、文件记录等功能。元数据算是一种电子式目录,为了达到编制目录的目的,必须在描述并收藏数据的内容或特色,进而达成协助数据检索的目的。都柏林核心集(Dublin Core Metadata Initiative,DCMI)是元数据的一种应用,是1995年2月由国际图书馆电脑中心(OCLC)和美国国家超级计算应用中心(National Center for Supercomputing Applications,NCSA)所联合赞助的研讨会,在邀请52位来自图书馆员、电脑专家,共同制定规格,创建一套描述网络上电子文件之特征。元数据是关于数据的组织、数据域及其关系的信息,简言之,元数据就是关于数据的数据



与文件时间戳相关的命令  

   为什么要了解时间戳:了解文件时间戳的概念对于发生故障迅速定位问题所在有一定帮助。

   如何查看文件的时间戳

   命令:stat 它是查看文件系统状态

   修改文件的时间戳

   命令:touch

   为了对touch有个更详细的了解我们man下touch其主要用法如下(有省略,只列举常用的功能项)

NAME
       touch - change file timestamps
SYNOPSIS
       touch [OPTION]... FILE...
DESCRIPTION
       Update  the  access  and modification times of each FILE to the current
       time.
       A FILE argument that does not exist is created empty, unless -c  or  -h
       is supplied.
       A  FILE  argument  string of - is handled specially and causes touch to
       change the times of the file associated with standard output.
       Mandatory arguments to long options are  mandatory  for  short  options
       too.
       -a     change only the access time   修改atime
       -d, --date=STRING                    
              parse STRING and use it instead of current time
       -h, --no-dereference                  只修改链接文件时间戳而对链接的源文件无影响
              affect each symbolic link instead of any referenced file (useful
              only on systems that can change the timestamps of a symlink)
       -m     change only the modification time  修改mtime
       -r, --reference=FILE             将此文件的时间戳与指定文件时间戳一致
              use this file’s times instead of current time
       -t STAMP                         修改时间戳
              use [[CC]YY]MMDDhhmm[.ss] instead of current time

    当然touch还有一个很主要的功能就是创建新文件,其格式为:

    touch filename 如果该file不存在则创建。 



如何修改文件的时间戳(附实验)

    

    如何修改文件时间戳,通过实验来查看上面选项的实际作用

    以下实验环境均在CentOS6.8环境,

    实验前准备:

    /test目录    /test/file1文件

[root@centos6 test]# pwd
/test
[root@centos6 test]# ll
总用量 0
-rw-r--r--. 1 root root 0 7月  28 21:43 file1

    前提条件准备完毕。

先查看下文件file1的文件属性信息,特别是时间戳

[root@centos6 test]# stat file1 
  File: "file1"
  Size: 0               Blocks: 0          IO Block: 4096   普通空文件
Device: 803h/2051d      Inode: 266584      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-07-28 21:43:53.554651380 +0800
Modify: 2016-07-28 21:43:53.554651380 +0800
Change: 2016-07-28 21:43:53.554651380 +0800

先修改atime

[root@centos6 test]# touch -a -t 201009200930 file1
[root@centos6 test]# stat file1 
  File: "file1"
  Size: 0               Blocks: 0          IO Block: 4096   普通空文件
Device: 803h/2051d      Inode: 266584      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2010-09-20 09:30:00.000000000 +0800
Modify: 2016-07-28 21:43:53.554651380 +0800
Change: 2016-07-28 21:48:15.589652240 +0800

  -a指定为atime -t指定要修改的具体时间 要修改atime,需要两者合用。

  由结果可以看到atime改变了,同时ctime也发生变化,因为修改文件file1的属性信息故只要修改关于时间戳的信息ctime均发生改变,其发生变化的时间即修改时系统当下时间。

下面修改mtime

[root@centos6 test]# touch -m -t 201607180830 file1 
[root@centos6 test]# stat file1 
  File: "file1"
  Size: 0               Blocks: 0          IO Block: 4096   普通空文件
Device: 803h/2051d      Inode: 266584      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2010-09-20 09:30:00.000000000 +0800
Modify: 2016-07-18 08:30:00.000000000 +0800
Change: 2016-07-28 21:51:21.598641893 +0800

  mtime发生改变,ctime也发生改变。

  下面我们使用命令cat查看下file1文件

[root@centos6 test]# cat file1 
[root@centos6 test]# stat file1 
  File: "file1"
  Size: 0               Blocks: 0          IO Block: 4096   普通空文件
Device: 803h/2051d      Inode: 266584      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-07-28 21:53:00.418655120 +0800
Modify: 2016-07-18 08:30:00.000000000 +0800
Change: 2016-07-28 21:51:21.598641893 +0800

因为file1文件为空故什么也没显示,不过我们发现atime发生了变化,其变化的时间为当前系统时间。

atime时间发生变化,是因为触发了该文件的读属性。

下面我们在file1文件内添加写内容

[root@centos6 test]# echo www >> file1 
[root@centos6 test]# stat file1 
  File: "file1"
  Size: 4               Blocks: 8          IO Block: 4096   普通文件
Device: 803h/2051d      Inode: 266584      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-07-28 21:53:00.418655120 +0800
Modify: 2016-07-28 21:55:22.358651684 +0800
Change: 2016-07-28 21:55:22.358651684 +0800

由结果可知mtimectime均发生改变,因为文件数据被修改,数据内容及元数据都发生变化。


   


    时间戳的实际作用

    在实际生产环境中关于时间戳的问题不多,不过有时会因为系统异常导致atime时间比系统时间提前,也就是在系统看来atime是未来的时间,这种情况会导致该文件无法正常读取。这个时候就需要手动刷新下该文件的atime

刷新atime命令

touche -a file

刷新mtime命令

touche -m file

刷新后的file时间自动更新为系统当下时间。


    虽然时间戳的作用对于一般管理员来说无关紧要,不过***对其却很重视,如何成功***系统,并且在功成身退后又不被人发现其***的痕迹,合理的修改时间戳还是很关键的。