1. 程式人生 > >linux下修改時間戳

linux下修改時間戳

file 結構 sta too hang 步驟 {} brush sys

Linux下touch是一個非常有用的命令。

touch語法結構如下:

touch [-acfm][-d <日期時間>][-r <參考文件或目錄>][-t <日期時間>][--help] [--version][文件或目錄...] 或 touch [-acfm][--help][--version][日期時間][文件或目錄...]

Usage: touch [OPTION]... FILE...
Mandatory arguments to long options are mandatory for short options too.
  -a    change only the access time
  -d, --date=STRING    parse STRING and use it instead of current time
  -m    change only the modification time
  -r, --reference=FILE    use this file‘s times instead of current time
  -t STAMP    use [[CC]YY]MMDDhhmm[.ss] instead of current time
  --time=WORD    change the specified time:
          WORD is access, atime, or use: equivalent to -a
          WORD is modify or mtime: equivalent to -m
Note that the -d and -t options accept different time-date formats.

使用舉例

--------------------------------------------

touch -m -d "2016-05-20 14:25:50" file
touch -d "2016-05-20 14:25:50" file
touch -d "2016-05-20" file
touch -d "14:25:50" file
或者
touch -t 201605201315.50 file
touch -t 05201315 file

命令參數:

-d使用指定的日期時間。
-a只更改存取時間access
-m只更改變動時間modify

設置日期(設置當前時間,只有root權限才能設置,其他只能查看):

#date //顯示當前日期
#date -s 20061010 //設置成20061010,這樣會把具體時間設置成空00:00:00
#date -s 12:23:23 //設置具體時間,不會對日期做更改
#date -s "2006-10-10 12:12:23" //這樣可以設置全部時間

--------------------------------------------

具體步驟

1、設置系統時間(能影響change time)

date -s "2010-10-10 10:10:10"

2、修改文件時間

#當前目錄下文件/文件夾(不能遞歸):

touch -m -d "2010-10-10 10:10:10" *

#遞歸修改當前目錄下所有文件/文件夾3個時間戳(Access、Modify、Change time):

find ./ * -exec touch {} \;

#遞歸修改當前目錄下所有文件/文件夾指定時間戳(Modify、Change time):

find ./ * -exec touch -m -d "2010-10-10 10:10:10" {} \;

3、還原系統時間

clock --hctosys

註意:

1、文件的Access time會隨著每次訪問而更新時間,所有這個參數意義不大,瀏覽器每打開一次這個文件,Access time均會更新。

2、Change time不能隨便修改,必須先修改系統時間才能改變這個值。

查看系統硬件時鐘

clock --show

硬件時鐘與系統時鐘同步:

clock --hctosys hc代表硬件時間,sys代表系統時間

PS:補充下查看文件時間:

stat file

時間顯示示例:

Access: 2010-05-02 01:22:11.000000000 +0800
Modify: 2010-05-02 01:22:11.000000000 +0800
Change: 2010-10-10 00:00:14.000000000 +0800

linux下修改時間戳