Linux文件組成部分:元數據(metadata)和數據(date)
元數據的查看方式:使用stat命令;
數據的查看方式:文本格式使用 cat nano查看及修改即可;
stat命令:
功能說明:顯示文件或文件系統的狀態
語法:
stat [OPTION] … FILE…
參數:
舉例:
[root@wCentos7 testdir]# stat stat.txt ====>顯示stat.的文件屬性 File: ‘stat.txt’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 805h/2053d Inode: 3140 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:etc_runtime_t:s0 Access: 2016-07-27 18:59:23.691076633 +0800 #文件訪問時間狀態(atime) Modify: 2016-07-27 18:59:23.691076633 +0800 #文件修改時間狀態(mtime) Change: 2016-07-27 18:59:23.691076633 +0800 #文件屬性改變狀態(ctime) Birth: - 注意:由于文件是剛剛建立的,三個時間是一樣的
驗證一下操作會對文件的屬性帶來三個時間上的變化
1. cat文件內容,看看文件屬性變化如何?
2. nano看看文件變化如何?
3. 給文件追加部分內容,看看文件屬性變化如何?
4. nano添加文件內容看看文件屬性變化如何?
5. 修改文件權限,看看文件屬性變化如何?
6. touch 下文件看看文件屬性變化如何?
測試一: cat文件內容,看看文件屬性變化如何?
cat文件內容,atime時間變為當前系統時間
(實際應用中,文件的訪問時間沒有什么太大的意義,可以設置文件的訪問時間不需要變化
注意:centos 6后以后atime 的時間不會由于 cat而改變時間:
atime 時間變化一天以前 時 當atime的時間比mtime 要舊 時
[root@wCentos7 testdir]# cat stat.txt [root@wCentos7 testdir]# stat stat.txt File: ‘stat.txt’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 805h/2053d Inode: 3140 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:etc_runtime_t:s0 Access: 2016-07-27 19:10:48.000064317 +0800 Modify: 2016-07-27 18:59:23.691076633 +0800 Change: 2016-07-27 18:59:23.691076633 +0800 Birth: -
測試二:nano看看文件變化如何?
nano沒有修改文件內容,文件屬性沒有任何的變化,所有的屬性都沒有變化
[root@wCentos7 testdir]# nano stat.txt [root@wCentos7 testdir]# stat stat.txt File: ‘stat.txt’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 805h/2053d Inode: 3140 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:etc_runtime_t:s0 Access: 2016-07-27 19:10:48.000064317 +0800 Modify: 2016-07-27 18:59:23.691076633 +0800 Change: 2016-07-27 18:59:23.691076633 +0800 Birth: -
測試三:給文件追加部分內容,看看文件屬性變化如何?
追加部分內容進入stat.txt文件,修改時間與改變時間都變成當前系統時間
(追加內容 導致文件大小變化,也導致文件 mtime時間變化,文件屬性本身有變化,所以文件改變時間也會更新)
[root@wCentos7 testdir]# echo "wo shi hao ren " > stat.txt [root@wCentos7 testdir]# stat stat.txt File: ‘stat.txt’ Size: 16 Blocks: 8 IO Block: 4096 regular file Device: 805h/2053d Inode: 3140 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:etc_runtime_t:s0 Access: 2016-07-27 19:10:48.000064317 +0800 Modify: 2016-07-27 19:22:33.231051624 +0800 Change: 2016-07-27 19:22:33.231051624 +0800 Birth: -
測試四:nano添加文件內容看看文件屬性變化如何?
[root@wCentos7 testdir]# nano stat.txt ->修改文件內容 [root@wCentos7 testdir]# stat stat.txt ->查看文件狀態 File: ‘stat.txt’ Size: 52 Blocks: 8 IO Block: 4096 regular file Device: 805h/2053d Inode: 3140 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:etc_runtime_t:s0 Access: 2016-07-27 19:27:08.464046670 +0800 Modify: 2016-07-27 19:27:27.441046329 +0800 Change: 2016-07-27 19:27:27.441046329 +0800 Birth: -
(修改文件內容后,三個文件時間都在變化,文件修改時間和文件改變時間是一致的,并且和大于文件訪問時間)
[root@wCentos7 testdir]# cat stat.txt -> 查看文件內容 wo shi hao ren ni shi bu shi de a dksajf aklsjf ;l [root@wCentos7 testdir]# stat stat.txt ->再來查看文件狀態 File: ‘stat.txt’ Size: 52 Blocks: 8 IO Block: 4096 regular file Device: 805h/2053d Inode: 3140 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:etc_runtime_t:s0 Access: 2016-07-27 19:28:08.989045581 +0800 Modify: 2016-07-27 19:27:27.441046329 +0800 Change: 2016-07-27 19:27:27.441046329 +0800 Birth: -
(cat文件內容后,文件訪問時間在變化,其它的文件時間沒有變化)
測試五:修改文件權限,看看文件屬性變化如何?
[root@wCentos7 testdir]# chmod 777 stat.txt -->修改文件權限 [root@wCentos7 testdir]# stat stat.txt -->查看其文件屬性 File: ‘stat.txt’ Size: 52 Blocks: 8 IO Block: 4096 regular file Device: 805h/2053d Inode: 3140 Links: 1 Access: (0777/-rwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:etc_runtime_t:s0 Access: 2016-07-27 19:28:08.989045581 +0800 Modify: 2016-07-27 19:27:27.441046329 +0800 Change: 2016-07-27 19:34:52.197038324 +0800 Birth: - (修改文件權限后,只改變了文件的屬性,故只有mtime的時間改變了)
測試六:touch 下文件看看文件屬性變化如何?
[root@wCentos7 testdir]# touch stat.txt [root@wCentos7 testdir]# stat stat.txt File: ‘stat.txt’ Size: 52 Blocks: 8 IO Block: 4096 regular file Device: 805h/2053d Inode: 3140 Links: 1 Access: (0777/-rwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:etc_runtime_t:s0 Access: 2016-07-27 19:39:29.247033338 +0800 Modify: 2016-07-27 19:39:29.247033338 +0800 Change: 2016-07-27 19:39:29.247033338 +0800 Birth: - (touch下文件后,文件的三個時間都改變了,由于touch命令的會修改文件mtime的時間,故ctime的時間也會改變)
進過以上測試,我可以得出以下理論:
1.修改atime的時間,只要涉及到打開文件的操作就會修改atime的時間,不過實際中atime的時間修改太過頻繁,所以在實際企業應用中可能會禁止修改atime的時間。
2.修改文件內容,追加新的內容,追加空白內容也會修改文件的修改時間 mtime,也會導致文件ctime的時間改變。
3.ctime,文件權限變化,文件大小變化,或其他文件狀態變化都會導致文件ctime時間變化,不過atime的時間變化不會影響ctime的時間變化。
原創文章,作者:linux_root,如若轉載,請注明出處:http://www.www58058.com/27682