Linux基础知识之文件的时间戳及touch的使用

简介:

文件时间戳概念介绍

  

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

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

    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其主要用法如下(有省略,只列举常用的功能项)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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文件

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

    前提条件准备完毕。

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

1
2
3
4
5
6
7
8
[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

1
2
3
4
5
6
7
8
9
[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

1
2
3
4
5
6
7
8
9
[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文件

1
2
3
4
5
6
7
8
9
[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文件内添加写内容

1
2
3
4
5
6
7
8
9
[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命令

1
touche -a  file

刷新mtime命令

1
touche -m  file

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


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










本文转自 紫色的茶碗 51CTO博客,原文链接:http://blog.51cto.com/chawan/1831385,如需转载请自行联系原作者
目录
相关文章
|
14小时前
|
Linux Go 数据安全/隐私保护
Linux 中的文件属性解析
在 Linux 系统中,每个文件和目录有一组属性控制其操作和访问权限。了解这些属性对有效管理文件至关重要。文件属性包括:文件类型(如 `-` 表示普通文件,`d` 表示目录),权限(如 `rwx` 表示所有者权限,`r-x` 表示组和其他用户权限),所有者,组,硬链接数,文件大小和最后修改时间。通过 `chown` 和 `chmod` 命令可更改文件所有者、所属组及权限。此外,还有特殊权限(如 SUID、SGID)和 ACL(访问控制列表)提供更精细的访问控制。
|
1天前
|
人工智能 Linux
Linux查找大文件的方法
Linux查找大文件的方法
|
4天前
|
固态存储 Ubuntu Linux
Linux(29) 多线程快速解压缩|删除|监视大型文件
Linux(29) 多线程快速解压缩|删除|监视大型文件
11 1
|
4天前
|
Ubuntu Linux 数据安全/隐私保护
Linux(24) 如何在Ubuntu中操作rootfs.img文件
Linux(24) 如何在Ubuntu中操作rootfs.img文件
9 0
|
8天前
|
安全 Linux 开发工具
Linux中可引起文件时间戳改变的相关命令
【4月更文挑战第12天】Linux中可引起文件时间戳改变的相关命令
18 0
|
10天前
|
Linux Shell 开发工具
Linux文件常用操作
Linux文件常用操作(几乎覆盖所有日常使用)
84 0
|
11天前
|
Linux 内存技术 Perl
【ZYNQ】制作从 QSPI Flash 启动 Linux 的启动文件
【ZYNQ】制作从 QSPI Flash 启动 Linux 的启动文件
|
14天前
|
Linux
Linux简单指令|cd|touch|mkdir|rmdir|rm
Linux简单指令|cd|touch|mkdir|rmdir|rm
|
17天前
|
Linux
linux 超过4个G的文件传不上去的解决办法
linux 超过4个G的文件传不上去的解决办法
9 0
|
17天前
|
Linux 索引
linux 文件查找 和文件管理常用命令
linux 文件查找 和文件管理常用命令
22 0