目錄
- 一.文件的三個(gè)時(shí)間
- 二.修改文件的三種時(shí)間為任意時(shí)間
一.文件的三個(gè)時(shí)間
當(dāng)我們?cè)趌inux中創(chuàng)建了文件或文件夾,文件/文件夾就有了時(shí)間屬性,而且linux中的文件具有三個(gè)時(shí)間,可以通過(guò)stat命令查看文件的三種時(shí)間:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-743975.html
- 訪問(wèn)時(shí)間(Access time):又簡(jiǎn)稱為atime,對(duì)文件進(jìn)行一次讀操作,它的訪問(wèn)時(shí)間就會(huì)改變。例如cat,more等操作,但是stat,ls命令對(duì)atime是不會(huì)有影響的。
- 修改時(shí)間(Modify time):又簡(jiǎn)稱為mtime,文件內(nèi)容最后一次修改的時(shí)間,我們經(jīng)常用的ls -l命令顯示出來(lái)的文件時(shí)間就是這個(gè)時(shí)間,當(dāng)對(duì)文件內(nèi)容修改后,它的mtime就會(huì)相應(yīng)的改變,例如vim操作。
- 改變時(shí)間(Change time):又簡(jiǎn)稱為ctime,當(dāng)文件的狀態(tài)被改變的時(shí)候,改變時(shí)間就會(huì)隨之改變。例如當(dāng)使用chmod、chown等改變文件屬性的操作是會(huì)改變文件的ctime。
[root@mast ~]# 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不會(huì)一直更新,只有當(dāng)mtime更新的時(shí)候,atime才會(huì)更新
二.修改文件的三種時(shí)間為任意時(shí)間
當(dāng)我們拿到一個(gè)文件,就可以隨心所欲的修改文件的三個(gè)時(shí)間。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-743975.html
[root@mast ~]# ll -h test.txt
-rw-r--r-- 1 root root 57 Oct 3 13:26 test.txt
# -d, --date=字符串 使用指定字符串表示時(shí)間而非當(dāng)前時(shí)間
#把test.txt文件的atime和mtime修改為20180605 21:00
[root@mast ~]# touch -d "20180605 21:00" test.txt
[root@mast ~]# ll -h test.txt
-rw-r--r-- 1 root root 57 Jun 5 2018 test.txt
#查看發(fā)現(xiàn)test.txt文件的atime和mtime已經(jīng)改變,但是ctime時(shí)間沒(méi)變
[root@mast ~]# 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時(shí)間只能通過(guò)修改系統(tǒng)時(shí)間來(lái)自定義,但是一般情況下修改系統(tǒng)時(shí)間需要root權(quán)限
[root@mast ~]# date -s 06/05/2018 >> test.txt
[root@mast ~]# 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: -
#此時(shí)發(fā)現(xiàn)系統(tǒng)時(shí)間已經(jīng)改變
[root@mast ~]# date "+%F %T"
2018-06-05 00:00:34
#現(xiàn)在需要更新系統(tǒng)時(shí)間為正常時(shí)間
[root@mast ~]# /usr/sbin/ntpdate ntp.api.bz
3 Oct 14:39:27 ntpdate[6973]: adjust time server 114.118.7.161 offset 0.068864 sec
#系統(tǒng)時(shí)間已經(jīng)更新正常
[root@mast ~]# date "+%F %T"
2020-10-03 14:39:46
#系統(tǒng)時(shí)間已經(jīng)改變,但是ctime沒(méi)變,此時(shí)文件的所有時(shí)間都已經(jīng)改變
[root@mast ~]# 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: -
到了這里,關(guān)于linux文件的三個(gè)時(shí)間,修改文件時(shí)間為任意時(shí)間的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!