天天看點

linux檔案時間的檢視和修改touch

1. linux檔案的時間

linux下檔案時間主要有下面三種:

1.1 modification time(mtime)

檔案修改時間,即檔案内容的修改時,更新這個時間,不包括檔案權限和屬性的修改。 使用ls -l檢視,預設顯示時間為mtime

$ ls -l uconv.h
-rw-rw-r--  1 work work 1808 Jul 23  2013 uconv.h
           

1.2 status time(ctime)

檔案狀态status的修改時間,如檔案的權限和屬性修改時更新這個時間。 使用 ls --time=ctime 檢視

$ ls -l --time=ctime uconv.h 
-rw-rw-r--  1 work work 1808 Jul 23  2013 uconv.h
           

1.3 access time(atime)

檔案通路時間,當檔案内容被擷取時,更新這個時間。 使用 ls --time=actime 檢視

$ ls -l --time=atime uconv.h
-rw-rw-r--  1 work work 1808 Dec 12  2013 uconv.h
           

2. 修改檔案的時間

如果需要修改上述三個時間,使用touch指令來修改。 touch filename ,如果檔案不存在,則建立一個檔案。

$ touch --help
Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.

  -a                     change only the access time
                         修改通路時間
  -c, --no-create        do not create any files
                         修改檔案三個時間,不存在則不建立
  -d, --date=STRING      parse STRING and use it instead of current time
                         指定時間代替目前時間
  -f                     (ignored)
  -m                     change only the modification time
                         修改mtime
  -r, --reference=FILE   use this file's times instead of current time
  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time
                         指定修改時間
           

例如:

$ touch -d "2 days ago" uconv.h
$ ll uconv.h ; ll --time=atime uconv.h ; ll --time=ctime uconv.h ;
-rw-rw-r--  1 work work 1808 Jun 13 18:17 uconv.h
-rw-rw-r--  1 work work 1808 Jun 13 18:17 uconv.h
-rw-rw-r--  1 work work 1808 Jun 15 18:17 uconv.h
           

将mtime和atime修改為兩天前,ctime沒變。

$ touch -t 201406142020 uconv.h   

$ ll uconv.h ; ll --time=atime uconv.h ; ll --time=ctime uconv.h ;
-rw-rw-r--  1 work work 1808 Jun 14 20:20 uconv.h
-rw-rw-r--  1 work work 1808 Jun 14 20:20 uconv.h
-rw-rw-r--  1 work work 1808 Jun 15 18:23 uconv.h
           

atime和mtime都變了,但是ctime變成了目前時間。

使用cp指令,-a保持原屬性。

$ cp -a uconv.h uconv.h1

$ ll uconv.h1 ; ll --time=atime uconv.h1 ; ll --time=ctime uconv.h1 ;
-rw-rw-r--  1 work work 1808 Jun 14 20:20 uconv.h1
-rw-rw-r--  1 work work 1808 Jun 15 18:25 uconv.h1
-rw-rw-r--  1 work work 1808 Jun 15 18:27 uconv.h1
           

mtime和atime都保持原檔案不變,但是ctime變成目前時間

位址:http://blog.csdn.net/yonggang7/article/details/31008607

繼續閱讀