linux 如何修改檔案日期?

touch:修改檔案時間,若檔案不存在,則產生新檔案。

[root@linux ~]# touch [-acdmt] 檔案
參數:
-a  :僅修訂 access time;
-c  :僅修改時間,而不建立檔案;
-d  :後面可以接日期,也可以使用 --date="日期或時間"
-m  :僅修改 mtime ;
-t  :後面可以接時間,格式為[YYMMDDhhmm]
#範例:

#範例一:新建一個空的檔案
[root@linux ~]# cd /tmp
[root@linux tmp]# touch testtouch
[root@linux tmp]# ls -l testtouch
-rw-r--r--  1 root root    0 Jul 19 20:49 testtouch

#範例二:將 ~/.bashrc 複製成為 bashrc,假設複製完全的屬性,檢查其日期
[root@linux tmp]# cp ~/.bashrc bashrc
[root@linux tmp]# ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc
-rwxr-xr-x  1 root root 395 Jul  4 11:45 bashrc  %*<==這是 mtime*)
-rwxr-xr-x  1 root root 395 Jul 19 20:44 bashrc  %*<==這是 atime*)
-rwxr-xr-x  1 root root 395 Jul 19 20:53 bashrc  %*<==這是 ctime*)

#範例三:修改案例二的 bashrc 檔案,將日期調整為兩天前
[root@linux tmp]# touch -d "2 days ago" bashrc
[root@linux tmp]# ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc
-rwxr-xr-x  1 root root 395 Jul 17 21:02 bashrc
-rwxr-xr-x  1 root root 395 Jul 17 21:02 bashrc
-rwxr-xr-x  1 root root 395 Jul 19 21:02 bashrc
# 日期在 atime 與 mtime 都改變了,但是 ctime 則是記錄目前的時間!

#範例四:將上個範例的 bashrc 日期改為 2005/07/15 2:02
[root@linux tmp]# touch -t 0507150202 bashrc
[root@linux tmp]# ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc
-rwxr-xr-x  1 root root 395 Jul 15 02:02 bashrc
-rwxr-xr-x  1 root root 395 Jul 15 02:02 bashrc
-rwxr-xr-x  1 root root 395 Jul 19 21:05 bashrc

留言

這個網誌中的熱門文章

c語言-關於#define用法

CMD常用網管指令

PHP 與 JavaScript 之間傳值利用 json