linux下文件有3個時間的,分別是atime,mtime,ctime。有些博友對這3個時間還是比較迷茫和困惑的,我整理了下,寫下來希望對博友們有所幫助。
1 這三個time的含義
-
-
簡名 全名 中文名 含義 atime access time 訪問時間 文件中的數據庫最后被訪問的時間 mtime modify time 修改時間 文件內容被修改的最后時間 ctime change time 變化時間 文件的元數據發生變化。比如權限,所有者等
-
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 #列出文件的atime
total 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 #列出ctime
total 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 測試准備工作
測試前,我們需要先關閉文件系統的relatime特性。這個隨后在說,具體操作如下。
[root@centos7 time]# mount -o remount,strictatime /app # 重新掛載我們的/app,並修改文件系統工作在嚴格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發生了改變。其他的沒有改變。
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: -
通過上面的實驗,我們可以看出來,寫文件操作不會導致atime(訪問時間)的修改,但是mtime和ctime會發生修改。mtime修改了我們可以理解的,畢竟我們修改了文件的,
那為何ctime也修改了呢, 仔細可以發現我們文件的大小發生了變化,也就是元數據發生了變化,所以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 #修改權限 [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: -
通過上面的實驗,我們可以看出來,修改了權限后,文件ctime發生了變化。
4 說說relatime
常用命令對三個time的修改情況我們上面的測試,可以看出來,每次訪問文件都會更新atime,這是很耗時的,尤其在web服務器上,大量用戶只是訪問html頁面,完全沒有必要修改atime。
從kernel2.6.29開始,文件系統默認集成了一個relatime的屬性。
那么啥時候更新atime呢? 有2種情況會更新atime,第一種是mtime比atime新,第二種是上次訪問是1天前的了。
5 常用命令對三個time的修改情況
上面我們做了3個測試,我們也對atime,mtime,ctime有了一定的了解。網上有人已經做了好多測試如下表。
+-------------------------------------------------+ | | 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 | - | +-------------------------------------------------+
6 參考文章
http://blog.csdn.net/wodeqingtian1234/article/details/53975744
http://blog.chinaunix.net/uid-24500107-id-2602881.html
http://www.jianshu.com/p/781e2c144367