linux文件的三个时间,修改文件时间为任意时间

时间:2020-11-08 17:02:45   收藏:0   阅读:34

一.文件的三个时间

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

[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: -
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!