新聞中心
Linux文檔的時間一般分三種:Access time-atime(訪問時間)、Modification time-mtime(內(nèi)容修改時間)、change-ctime(inode更改時間),下面為大家詳細講解一下Linux中文件的三種時間。

成都創(chuàng)新互聯(lián)公司于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站建設(shè)、成都網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元大石橋做網(wǎng)站,已為上家服務(wù),為大石橋各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220
1 這三個time的含義
-
-
簡名 全名 中文名 含義 atime access time 訪問時間 文件中的數(shù)據(jù)庫最后被訪問的時間 mtime modify time 修改時間 文件內(nèi)容被修改的最后時間 ctime change time 變化時間 文件的元數(shù)據(jù)發(fā)生變化。比如權(quán)限,所有者等
-
2 如何查看這3個time
[root@centos7 time]# pwd
/app/time
[root@centos7 time]# ll
total 8
-rw-------. 1 root root 1933 Nov 11 08:14 anaconda-ks.cfg
-rw-r--r--. 1 root root 59 Nov 11 08:15 issue
[root@centos7 time]# stat issue
File: ‘issue’
Size: 59 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 261123 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2017-11-11 08:15:05.650986739 +0800
Modify: 2017-11-11 08:15:05.650986739 +0800
Change: 2017-11-11 08:15:05.650986739 +0800
Birth: -[root@centos7 time]# ls -l #默認的ls -l顯示的是mtime total 8-rw-------. 1 root root 1933 Nov 11 08:14 anaconda-ks.cfg-rw-r--r--. 1 zhaojiedi root 71 Nov 11 09:05 issue[root@centos7 time]# ls -l --time=atime #列出文件的atimetotal 8-rw-------. 1 root root 1933 Nov 11 08:14 anaconda-ks.cfg-rw-r--r--. 1 zhaojiedi root 71 Nov 11 09:12 issue[root@centos7 time]# ls -l --time=ctime #列出ctimetotal 8-rw-------. 1 root root 1933 Nov 11 08:14 anaconda-ks.cfg-rw-r--r--. 1 zhaojiedi root 71 Nov 11 09:03 issue
3 三個time的測試
3.1 測試準備工作
測試前,我們需要先關(guān)閉文件系統(tǒng)的relatime特性。這個隨后在說,具體操作如下。
[root@centos7 time]# mount -o remount,strictatime /app # 重新掛載我們的/app,并修改文件系統(tǒng)工作在嚴格atime上,也就是不啟用了默認的relatime支持。
[root@centos7 time]# mount |grep /app #查看我們的修改
/dev/sda5 on /app type ext4 (rw,seclabel,data=ordered)
3.2 讀取文件測試
[root@centos7 time]# stat issue #先獲取3個時間
File: ‘issue’
Size: 59 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 261123 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2017-11-11 08:15:05.650986739 +0800
Modify: 2017-11-11 08:15:05.650986739 +0800
Change: 2017-11-11 08:15:05.650986739 +0800
Birth: -
[root@centos7 time]# cat issue #讀取下
\S
Kernel \r on an \m
tty: \l
hostname: \n
time: \t
[root@centos7 time]# stat issue #再次查看3個時間
File: ‘issue’
Size: 59 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 261123 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2017-11-11 08:57:40.858948780 +0800
Modify: 2017-11-11 08:15:05.650986739 +0800
Change: 2017-11-11 08:15:05.650986739 +0800
Birth: -
通過上面的分析,我們可以看出來,在使用cat讀取文件后,文件的atime發(fā)生了改變。其他的沒有改變。
3.3 修改文件測試
[root@centos7 time]# stat issue #先獲取下3個time
File: ‘issue’
Size: 65 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 261123 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2017-11-11 09:03:49.080931626 +0800
Modify: 2017-11-11 09:04:16.881930331 +0800
Change: 2017-11-11 09:04:16.881930331 +0800
Birth: -
[root@centos7 time]# echo "hello" >> issue #修改文件
[root@centos7 time]# stat issue #再次查看三個time
File: ‘issue’
Size: 71 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 261123 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2017-11-11 09:03:49.080931626 +0800
Modify: 2017-11-11 09:05:07.775927960 +0800
Change: 2017-11-11 09:05:07.775927960 +0800
Birth: -
通過上面的實驗,我們可以看出來,寫文件操作不會導(dǎo)致atime(訪問時間)的修改,但是mtime和ctime會發(fā)生修改。mtime修改了我們可以理解的,畢竟我們修改了文件的,
那為何ctime也修改了呢, 仔細可以發(fā)現(xiàn)我們文件的大小發(fā)生了變化,也就是元數(shù)據(jù)發(fā)生了變化,所以ctime也是要變化的。
3.4 修改文件所有者測試
[root@centos7 time]# stat issue #先查看下3個time
File: ‘issue’
Size: 71 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 261123 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2017-11-11 09:03:49.080931626 +0800
Modify: 2017-11-11 09:05:07.775927960 +0800
Change: 2017-11-11 09:05:07.775927960 +0800
Birth: -
[root@centos7 time]# chown zhaojiedi issue #修改權(quán)限
[root@centos7 time]# stat issue #再次查看3個時間
File: ‘issue’
Size: 71 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 261123 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/zhaojiedi) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2017-11-11 09:03:49.080931626 +0800
Modify: 2017-11-11 09:05:07.775927960 +0800
Change: 2017-11-11 09:12:42.076906795 +0800
Birth: -
通過上面的實驗,我們可以看出來,修改了權(quán)限后,文件ctime發(fā)生了變化。
4 說說relatime
常用命令對三個time的修改情況我們上面的測試,可以看出來,每次訪問文件都會更新atime,這是很耗時的,尤其在web服務(wù)器上,大量用戶只是訪問html頁面,完全沒有必要修改atime。
從kernel2.6.29開始,文件系統(tǒng)默認集成了一個relatime的屬性。
那么啥時候更新atime呢? 有2種情況會更新atime,第一種是mtime比atime新,第二種是上次訪問是1天前的了。
5 常用命令對三個time的修改情況
上面我們做了3個測試,我們也對atime,mtime,ctime有了一定的了解。網(wǎng)上有人已經(jīng)做了好多測試如下表。
+-------------------------------------------------+
| | timestamps marked for update |
| syscall |---------------------------------|
| | file | parent dir |
|---------------+-------------------+-------------|
| [2]chdir | | |
|---------------| - | - |
| [3]fchdir | | |
|---------------+-------------------+-------------|
| [4]chmod | | |
|---------------| ctime | - |
| [5]fchmod | | |
|---------------+-------------------+-------------|
| [6]chown | | |
|---------------| | |
| [7]fchown | ctime | - |
|---------------| | |
| [8]lchown | | |
|---------------+-------------------+-------------|
| [9]close | - | - |
|---------------+-------------------+-------------|
| [10]creat | atime,ctime,mtime | ctime,mtime |
|---------------+-------------------+-------------|
| [11]execve | atime | - |
|---------------+-------------------+-------------|
| [12]fcntl | - | - |
|---------------+-------------------+-------------|
| [13]ftruncate | | |
|---------------| ctime,mtime | - |
| [14]truncate | | |
|---------------+-------------------+-------------|
| [15]fstat | | |
|---------------| | |
| [16]stat | - | - |
|---------------| | |
| [17]lstat | | |
|---------------+-------------------+-------------|
| [18]fsync | | |
|---------------| - | - |
| [19]fdatasync | | |
|---------------+-------------------+-------------|
| [20]link | ctime | ctime,mtime |
|---------------+-------------------+-------------|
| [21]lseek | - | - |
|---------------+-------------------+-------------|
| [22]mknod | atime,ctime,mtime | ctime,mtime |
|---------------+-------------------+-------------|
| [23]mkdir | atime,ctime,mtime | ctime,mtime |
|---------------+-------------------+-------------|
| [24]mmap | * | - |
|---------------+-------------------+-------------|
| [25]munmap | - | - |
|---------------+-------------------+-------------|
| [26]msync | * | - |
|---------------+-------------------+-------------|
| [27]open | * | * |
|---------------+-------------------+-------------|
| [28]pread | | |
|---------------| | |
| [29]read | atime | - |
|---------------| | |
| [30]readv | | |
|---------------+-------------------+-------------|
| [31]pwrite | | |
|---------------| | |
| [32]write | ctime,mtime | - |
|---------------| | |
| [33]writev | | |
|---------------+-------------------+-------------|
| [34]rename | implementation | ctime,mtime |
|---------------+-------------------+-------------|
| [35]rmdir | - | ctime,mtime |
|---------------+-------------------+-------------|
| [36]readlink | * | - |
|---------------+-------------------+-------------|
| [37]readdir | atime | - |
|---------------+-------------------+-------------|
| readahead | ? | ? |
|---------------+-------------------+-------------|
| [38]symlink | * | * |
|---------------+-------------------+-------------|
| sendfile | ? | ? |
|---------------+-------------------+-------------|
| [39]unlink | - | ctime,mtime |
|---------------+-------------------+-------------|
| [40]utime | ctime | - |
+-------------------------------------------------+
標題名稱:詳解Linux文件的三個時間
轉(zhuǎn)載注明:http://www.dlmjj.cn/article/dhggojs.html


咨詢
建站咨詢
