修改linux文件的mtime


一.  Linux 文件個時間信息 

  所有Unix 文件系統中的文件或文件夾有三個時間戳,分別為atime、ctime和mtime。

  1. atime 表示最后一次訪問(僅僅訪問,沒有改動)文件的時間;
  2. mtime 表示最后一次修改文件的時間;
  3. ctime 表示最后一次對文件屬性改變的時間,包括權限、大小、屬性等。

  區別:

區別 atime mtime ctime
僅讀取或訪問文件(cat)  改變 不變 不變
修改文件內容 不一定(vim 與echo就不一樣) 改變 改變
修改文件權限屬性(chmod,chown) 不變 不變 改變

 

二. 如何查看這些時間信息

dill@ubuntu-vm:~/test/20170224$ stat file1 File: 'file1' Size: 20              Blocks: 8          IO Block: 4096 regular file Device: fc00h/64512d    Inode: 1447275     Links: 1 Access: (0644/-rw-r--r--)  Uid: ( 1000/    dill)   Gid: (    0/ root) Access: 2017-02-24 16:22:59.158209413 +0800 Modify: 2017-02-24 16:22:31.134761578 +0800 Change: 2017-02-24 16:22:31.134761578 +0800 Birth: -

 

三.  修改mtime

dill@ubuntu-vm:~/test/20170224$ stat file1 File: 'file1' Size: 20              Blocks: 8          IO Block: 4096 regular file Device: fc00h/64512d    Inode: 1447275     Links: 1 Access: (0644/-rw-r--r--)  Uid: ( 1000/    dill)   Gid: (    0/ root) Access: 2017-02-24 16:22:59.158209413 +0800 Modify: 2017-02-24 16:22:31.134761578 +0800 Change: 2017-02-24 16:22:31.134761578 +0800 Birth: - dill@ubuntu-vm:~/test/20170224$ touch -mt 1802241622 file1 dill@ubuntu-vm:~/test/20170224$ stat file1 File: 'file1' Size: 20              Blocks: 8          IO Block: 4096 regular file Device: fc00h/64512d    Inode: 1447275     Links: 1 Access: (0644/-rw-r--r--)  Uid: ( 1000/    dill)   Gid: (    0/ root) Access: 2017-02-24 16:22:59.158209413 +0800 Modify: 2018-02-24 16:22:00.000000000 +0800 Change: 2017-02-24 18:21:21.319212091 +0800 Birth: -

  -m 參數指mtime,接下來的t和一串數字指我們想要更改成的timestamp

  1802241622 代表:

    18 ---> 2018年

    02 ---> 2月

    24 ---> 24號

    1622 --->時間16:22

 

 修改mtime到當前Linux時間

dill@ubuntu-vm:~/test/20170224$ stat file1 File: 'file1' Size: 20              Blocks: 8          IO Block: 4096 regular file Device: fc00h/64512d    Inode: 1447275     Links: 1 Access: (0644/-rw-r--r--)  Uid: ( 1000/    dill)   Gid: (    0/ root) Access: 2017-02-24 16:22:59.158209413 +0800 Modify: 2018-02-24 16:22:00.000000000 +0800 Change: 2017-02-24 18:21:21.319212091 +0800 Birth: - dill@ubuntu-vm:~/test/20170224$ touch file1  dill@ubuntu-vm:~/test/20170224$ stat file1 File: 'file1' Size: 20              Blocks: 8          IO Block: 4096 regular file Device: fc00h/64512d    Inode: 1447275     Links: 1 Access: (0644/-rw-r--r--)  Uid: ( 1000/    dill)   Gid: (    0/ root) Access: 2017-02-24 18:33:40.155812846 +0800 Modify: 2017-02-24 18:33:40.155812846 +0800 Change: 2017-02-24 18:33:40.155812846 +0800 Birth: -

touch 命令的默認行為就是更新一個文件的atime和mtime,改變到當前的系統時間。

 

參考資料

  1. How to update atime and mtime for file in unix 
  2. Linux 文件3個時間點  A_carat_tear


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM