歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> linux文件之touch命令及文件時間戳

linux文件之touch命令及文件時間戳

日期:2017/3/1 12:23:12   编辑:關於Linux
一,在將touch命名前先看看文件關於時間的屬性。通過stat命令查看文件如下: [root@localhost test]# stat f1 File: `f1' Size: 34 Blocks:8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 72757 Links: 1 Access: (0640/-rw-r-----) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2015-10-09 01:26:41.658883440 +0800 Modify: 2015-10-08 04:05:15.000000000 +0800 Change: 2015-10-08 20:20:32.811903158 +0800 查看上面信息看到文件三個屬性 Acess time:即文件存取時間,或者理解為“最後一次讀取時間”,如使用touch、cat、more等命令會修改此數值,但使用ls,stat查看不會改變。注意:若你使用的是虛擬機測試,會發現cat、more等命令時不會實時更新,可以使用實體機測試。 Modify time:修改時間,這裡是指文件內容最後一次修改時間。ls命令默認顯示就是這個時間。 Change time:改變時間,這裡是指文件屬性最後修改時間,如修改權限、名稱等,一定要與Modify time區分。 二、touch命令:用來修改文件時間戳,或者新建文件。 1,選項參數 -a:--time=atime Acess time; -m: --time=mtime Modify time; -c: --no-create不建立任何文檔。默認當文件不存在時會創建文件,-c就可以不創建文件。 -r:將文件設置和參考文件的日期時間相同。 -t:使用指定時間日期,而不是直接用系統時間。 三、實例 1,先來touch一個已存在的文件 [root@localhost test]# stat f1 File: `f1' Size: 34 Blocks:8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 71660 Links: 1 Access: (0640/-rw-r-----) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2015-10-09 17:28:43.853828458 +0800 Modify: 2015-10-08 04:05:15.000000000 +0800 Change: 2015-10-09 03:16:57.117877086 +0800 [root@localhost test]# touch f1 [root@localhost test]# stat f1 #可以看到所有文件時間都更新為當前系統時間。 File: `f1' Size: 34 Blocks:8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 71660 Links: 1 Access: (0640/-rw-r-----) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2015-10-09 18:17:22.936820504 +0800 Modify: 2015-10-09 18:17:22.936820504 +0800 Change: 2015-10-09 18:17:22.936820504 +0800 [root@localhost test]# date Fri Oct 9 18:17:29 CST 2015 2,通過touch命令創建一個文件 [root@localhost test]# touch -c f3 #-c不創建文件 [root@localhost test]# ls f1 f2 test [root@localhost test]# touch f3 #創建文件f3 [root@localhost test]# ls f1 f2 f3 test [root@localhost test]# file f3 #查看f3文件類型,顯示為空 f3: empty 3,修改文件時間為指定時間 通過man查看touch –t參數: -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of currenttime [root@localhost test]# touch -t201510081010.10 f3 [root@localhost test]# stat f3 #修改時間為指定時間日期 File: `f3' Size: 4 Blocks:8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 66037 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2015-10-08 10:10:10.000000000 +0800 Modify: 2015-10-08 10:10:10.000000000 +0800 Change: 2015-10-09 18:34:22.535825523 +0800#注意Ctime為當前時間,因為更改文件屬性。 4,將文件時間與指定參考文件時間對齊。 [root@localhost test]# touch -r f3 f1 #將f1時間改為與f3相同 [root@localhost test]# stat f1 File: `f1' Size: 34 Blocks:8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 71660 Links: 1 Access: (0640/-rw-r-----) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2015-10-08 10:10:10.000000000 +0800 Modify: 2015-10-08 10:10:10.000000000 +0800 Change: 2015-10-09 18:42:47.175819324 +0800#同樣Ctime依然當前時間。 總結:touch命令還是比較簡單的,主要是理解關於文件的三個時間屬性。
Copyright © Linux教程網 All Rights Reserved