歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux文件時間的查看和修改touch

Linux文件時間的查看和修改touch

日期:2017/2/28 14:30:42   编辑:Linux教程

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變成當前時間

Copyright © Linux教程網 All Rights Reserved