zoukankan      html  css  js  c++  java
  • linux文件的三个时间,修改文件时间为任意时间

    一.文件的三个时间

    当我们在linux中创建了文件或文件夹,文件/文件夹就有了时间属性,而且linux中的文件具有三个时间,可以通过stat命令查看文件的三种时间:

    • ​ 访问时间(Access time):又简称为atime,对文件进行一次读操作,它的访问时间就会改变。例如cat,more等操作,但是stat,ls命令对atime是不会有影响的。
    • ​ 修改时间(Modify time):又简称为mtime,文件内容最后一次修改的时间,我们经常用的ls -l命令显示出来的文件时间就是这个时间,当对文件内容修改后,它的mtime就会相应的改变,例如vim操作。
    • ​ 改变时间(Change time):又简称为ctime,当文件的状态被改变的时候,改变时间就会随之改变。例如当使用chmod、chown等改变文件属性的操作是会改变文件的ctime。
    [root@node5 ~]# stat test.txt 
      File: ‘test.txt’
      Size: 57        	Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d	Inode: 34566832    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2020-10-03 13:26:27.884061279 +0800
    Modify: 2020-10-03 13:26:27.884061279 +0800
    Change: 2020-10-03 13:26:27.884061279 +0800
    
    #atime不会一直更新,只有当mtime更新的时候,atime才会更新
    

    二.修改文件的三种时间为任意时间

    当我们拿到一个文件,就可以随心所欲的修改文件的三个时间。

    [root@node5 ~]# ll -h test.txt 
    -rw-r--r-- 1 root root 57 Oct  3 13:26 test.txt
    
    # -d, --date=字符串 使用指定字符串表示时间而非当前时间
    #把test.txt文件的atime和mtime修改为20180605 21:00
    [root@node5 ~]# touch -d "20180605 21:00" test.txt 
    
    [root@node5 ~]# ll -h test.txt 
    -rw-r--r-- 1 root root 57 Jun  5  2018 test.txt
    #查看发现test.txt文件的atime和mtime已经改变,但是ctime时间没变
    [root@node5 ~]# stat test.txt 
      File: ‘test.txt’
      Size: 57        	Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d	Inode: 34566832    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2018-06-05 21:00:00.000000000 +0800
    Modify: 2018-06-05 21:00:00.000000000 +0800
    Change: 2020-10-03 14:21:30.465143168 +0800
     Birth: -
    
    #change time时间只能通过修改系统时间来自定义,但是一般情况下修改系统时间需要root权限
    [root@node5 ~]# date -s 06/05/2018 >> test.txt 
    [root@node5 ~]# stat test.txt 
      File: ‘test.txt’
      Size: 86        	Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d	Inode: 34566832    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2018-06-05 21:00:00.000000000 +0800
    Modify: 2018-06-05 00:00:00.000000000 +0800
    Change: 2018-06-05 00:00:00.000000000 +0800
     Birth: -
    
    #此时发现系统时间已经改变
    [root@node5 ~]# date "+%F %T"
    2018-06-05 00:00:34
    
    #现在需要更新系统时间为正常时间
    [root@node5 ~]# /usr/sbin/ntpdate ntp.api.bz
     3 Oct 14:39:27 ntpdate[6973]: adjust time server 114.118.7.161 offset 0.068864 sec
    
    #系统时间已经更新正常
    [root@node5 ~]# date "+%F %T"
    2020-10-03 14:39:46
    
    #系统时间已经改变,但是ctime没变,此时文件的所有时间都已经改变
    [root@node5 ~]# stat test.txt 
      File: ‘test.txt’
      Size: 86        	Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d	Inode: 34566832    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2018-06-05 21:00:00.000000000 +0800
    Modify: 2018-06-05 00:00:00.000000000 +0800
    Change: 2018-06-05 00:00:00.000000000 +0800
     Birth: -
    
  • 相关阅读:
    Spring Boot Test(转)
    volatile的可见性和有序性是什么(转)
    消息队列漫谈:如何使用消息队列实现分布式事务?(转)
    idea IntelliJ修改代码后自动重启(转)
    Spring Boot自动配置实现原理(转)
    Java注解(Annotation)原理详解(转)
    Spring中@Import注解的作用和使用(转)
    i春秋六周年庆丨欢乐相聚,暖心同行,未来可期!
    王者之战,志在巅峰丨2021互联网安全城市巡回赛•北京站圆满收官!
    2021互联网安全城市巡回赛——风起云涌,再战京城!
  • 原文地址:https://www.cnblogs.com/renshengdezheli/p/13941084.html
Copyright © 2011-2022 走看看