修改檔案時間與建置新檔


		touch 		 修改檔案時間與建置新檔
  1. 檔案變動時間
    1. atime (Access time):檔案內容上次被讀取的時間;
    2. ctime (Change status time):檔案狀態最近被更動的時間(不論是屬性、權限等);
    3. mtime (Modification time):檔案內容最近被修改的時間。
  2. 查詢檔案 /etc/man.conf 變動時間
    [root@dywHome2 ~]# ll /etc/man.config %*<==預設為 mtime*)
    -rw-r--r-- 1 root root 4831 Feb  3  2008 /etc/man.config
    [root@dywHome2 ~]# ll --time=atime /etc/man.config
    -rw-r--r-- 1 root root 4831 Sep  5 09:30 /etc/man.config
    [root@dywHome2 ~]# ll --time=ctime /etc/man.config
    -rw-r--r-- 1 root root 4831 Feb  3  2008 /etc/man.config
    [root@dywHome2 ~]# ll --time=mtime /etc/man.config
    
  3. 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
    

練習題

  1. 請說明檔案的三種時間參數。
    Sol. 1.Access time (atime)檔案內容上次被讀取的時間; 2.Change status time (ctime) 檔案狀態最近被更動的時間(不論是屬性、權限等); 3.Modification time(mtime) 檔案內容最近被修改的時間。
  2. 如何查閱 /etc/passwd 最近被存取的時間?
    Sol. ls -l –time=atime /etc/passwd
  3. 如何查閱 /etc/passwd 最近被內容被更動的時間?
    Sol. ls -l /etc/passwd
  4. 如何查閱 /etc/passwd 狀態最近被更動的時間?
    Sol. ls -l –time=ctime /etc/passwd
  5. 如何產生一個空的檔案 /tmp/testfile?
    Sol. touch /tmp/testfile
  6. 如何修改檔案 /etc/crontab 的 atime 及 mtime 調整為目前的時間?
    Sol. touch /etc/crontab
  7. 是否可以 touch 修改檔案 ctime 時間?
    Sol. 不可以
  8. 如何修改檔案 foo 的 atime 及 mtime 調整為一天前?
    Sol. touch -d "1 day ago" foo
  9. 如何修改檔案 foo 的 atime 及 mtime 調整為一分鐘前?
    Sol. touch -d "1 min ago" foo
  10. 如何修改檔案 foo 的 atime 及 mtime 調整 2008/10/02?
    Sol. touch –date="2008/10/02" foo
  11. 如何修改檔案 foo 的 atime 及 mtime 調整 2008/10/02 09:21?
    Sol. touch –date="2008/10/02 09:21" foo
  12. 如何修改檔案 foo 的 atime 及 mtime 調整 2008/10/02 09:15,且輸入格式為[YYMMDDhhmm]?
    Sol. touch -t 0810020915 foo