请访问原文链接:https://sysin.org/article/how-to-change-file-date,查看最新版。转载请保留出处。
作者:gc(at)http://sysin.org,主页:www.sysin.org
1. Windows(创建时间,修改时间,访问时间)
图形界面
点击一个文件右键“属性”即可查看文件的时间属性,可以看到有“创建时间”、“修改时间”和“访问时间”三个属性。

- 创建时间:该文件在本载体本地址上创建的时间
- 修改时间:在属性中保存的最后一次修改的时间
- 访问时间:在属性中保存的最后一次访问的时间
“创建时间”和“修改时间”比较好理解,但“访问时间”似乎有点特殊,查看文件属性、打开文件查看,甚至设置“只读”、“隐藏”属性都不会改变“访问时间”。
修改时间的工具:
- Attribute Changer(免费,推荐!)
- BulkFileChanger(免费)
- NewFileTime(免费,UI 略逊)

命令行修改
CMD
#修改当前系统时间
date 2021/01/01
time 10:59:30
#修改时间,注意是连续两个英文逗号
copy 文件名+,,
#修改时间和访问时间,注意是连续两个英文句号
copy 文件名+..
# 注意修改完毕需要将系统时间修改过来(或者等待 NTP 同步)
小技巧:在文件夹上添加“命令提示符”右键快捷访问菜单
reg add "HKEY_CLASSES_ROOTFoldershellCmdPrompt" /v "" /t REG_SZ /d "Command Prompt" /f
reg add "HKEY_CLASSES_ROOTFoldershellCmdPromptcommand" /v "" /t REG_SZ /d "cmd.exe /k cd %l" /f
Powershell(推荐)
set t '01/01/2021 01:01:01'
# 时间格式:MM/DD/YYYY hh:mm:ss
echo $t
ls 'sysin.txt' | foreach-object { $_.LastWriteTime = $t; $_.CreationTime = $t; $_.LastAccessTime = $t}
# 也可以单独设置不同的时间
ls 'sysin.txt' | foreach-object { $_.LastWriteTime = '01/01/2021 01:01:01'; $_.CreationTime = '02/02/2021 01:01:01'; $_.LastAccessTime = '03/03/2021 01:01:01' }
小技巧:在文件夹上添加“PowerShell”右键快捷访问菜单
reg add "HKEY_CLASSES_ROOTFoldershellPowerShellMenu" /v "" /t REG_SZ /d "PowerShell" /f
reg add "HKEY_CLASSES_ROOTFoldershellPowerShellMenucommand" /v "" /t REG_SZ /d "powershell.exe -NoExit -Command Set-Location -LiteralPath '%L'" /f
2. Linux(修改时间,访问时间)
注意:Linux(包括传统 Unix)中没有“创建(creation)”时间的概念(除非使用 debugfs)
查看文件时间信息(stat 命令)
# 创建一个测试文件
$ touch sysin.txt
# 使用 stat 命令查看文件时间信息
$ stat sysin.txt
File: sysin.txt
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 405343 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2021-01-08 13:22:44.792348339 +0800
Modify: 2021-01-08 13:22:44.792348339 +0800
Change: 2021-01-08 13:22:44.792348339 +0800
Birth: -
有如下三种时间
- Access: ATime —— 文件的最近访问时间。只要读取时间,ATime 就会更新。
- Modify: MTime —— 文件的内容最近修改的时间当文件进行被写的时候,CTime 就会更新。
- Change: CTime —— 文件属性最近修改的时间当文件的目录被修改,或者文件的所有者,权限等被修改时 CTime 也就会更新。
touch 修改时间
touch 不仅可以创建文件,还可以对其进行时间的一些修改
格式:touch 参数 文件名
参数:
-a: 修改访问时间,或 –time=atime 或 –time=access 或 –time=use
-c: 或 –no-creat,如果文件不存在则不创建文件
-d: 使用指定的日期时间,可以使用不同的格式
-m: 或 –time=mtime 或 –time=modify,改变修改时间
-r: 把指定的文件日期更设成和参考文档或目录日期相同的时间
-t: 使用指定的日期时间,格式与 date 指令相同
命令格式:
# 修改“修改时间”
touch -mt YYYYMMDDhhmm filename
# 修改“访问时间和修改时间”
touch -t YYYYMMDDhhmm filename
YYYY 年-四位数
MM 月-两位数
DD 日-两位数
hh 小时-两位数
mm 分钟-两位数
示例:
touch -mt 202005181505 sysin.txt
stat sysin.txt
...
Access: 2021-01-08 13:22:44.792348339 +0800 # 未改变
Modify: 2021-05-18 18:05:00.000000000 +0800 # 变为指定的时间
Change: 2021-01-08 16:09:46.929329358 +0800 # 自动变为当前操作的时间
touch -t 202005191505 sysin.txt
stat sysin.txt
...
Access: 2020-05-19 15:05:00.000000000 +0800 # 变为指定的时间
Modify: 2020-05-19 15:05:00.000000000 +0800 # 变为指定的时间
Change: 2021-01-08 16:25:45.453907718 +0800 # 自动变为当前操作的时间
3. macOS(创建时间,修改时间)
图形界面
在 macOS 中,点击文件右键“显示简介”,显示“创建时间”和“修改时间”。

在 Finder(访达)中,除了上述两个时间,同时会有“上次打开日期”和“添加日期”这两个特殊的文件属性。“上次打开日期”记录了上次打开这个文件的最后时间,无论是否编辑和更改文件内容。“添加日期”记录了文件在当前位置产生的时间,比如是新创建的一个文件,该时间等于创建时间,如果是复制的文件,或者通过网络下载的文件,该时间只是在当前位置产生的时间,与创建时间无关。这两个特殊的文件属性默认无法修改。
修改“创建时间”和“修改时间”
可以使用这个 App:A Better Finder Attributes(商业)

终端
在 Darwin 系统部分(Darwin 一般是指 macOS 的命令行部分),类似于 Linux 有 atime、mtime 和 ctime,但是还多了 birthtime 即创建时间。
直接使用 stat 命令可以查看文件时间属性,可以看到有 4 个时间,但是不友好,没法直接辨别。其实分别是:Access、Modify、Change 和 Creation。
但作为正统 Unix 系统,Darwin 的 stat 命令有额外的参数:
加上 -x 参数可以看到 Access、Modify 和 Change 三个时间,类似 Linux 中直接执行 stat 的效果。
加上 -s 参数,可以看到除了上述时间,还多了一个 birthtime 即创建时间
$ stat sysin.txt
16777220 3851534 -rw-r--r-- 1 user staff 0 0 "Jan 10 12:46:01 2021" "May 19 15:05:00 2020" "Jan 10 12:46:01 2021" "May 18 15:05:00 2020" 4096 0 0 sysin.txt
$ stat -x sysin.txt
File: "sysin.txt"
Size: 0 FileType: Regular File
Mode: (0644/-rw-r--r--) Uid: ( 501/ user) Gid: ( 20/ staff)
Device: 1,4 Inode: 3851534 Links: 1
Access: Sun Jan 10 12:46:01 2021
Modify: Tue May 19 15:05:00 2020
Change: Sun Jan 10 12:46:01 2021
$ stat -s sysin.txt
st_dev=16777220 st_ino=3851534 st_mode=0100644 st_nlink=1 st_uid=501 st_gid=20 st_rdev=0 st_size=0 st_atime=1610253961 st_mtime=1589871900 st_ctime=1610253961 st_birthtime=1589785500 st_blksize=4096 st_blocks=0 st_flags=0
修改时间可以使用 touch 和 setfile 命令
touch(同 Linux,修改时间和访问时间)
# 修改“修改时间”
touch -m -t YYYYMMDDhhmm filename
# 修改“访问时间和修改时间”
touch -t YYYYMMDDhhmm filename
YYYY 年-四位数
MM 月-两位数
DD 日-两位数
hh 小时-两位数
mm 分钟-两位数
touch -mt 202005181505 sysin.txt
touch -t 202005191505 sysin.txt
setfile(创建时间和修改时间)
需要安装 Xcode command line tools(命令:xcode-select --install)
setfile
Usage: SetFile [option...] file...
-a attributes # attributes (lowercase = 0, uppercase = 1)*
-c creator # file creator
-d date # creation date (mm/dd/[yy]yy [hh:mm[:ss] [AM | PM]])*
-m date # modification date (mm/dd/[yy]yy [hh:mm[:ss] [AM | PM]])*
-P # perform action on symlink instead of following it
-t type # file type
示例:
##修改创建日期:setfile -d mm/dd/yy hh:mm:ss filename
##修改修改日期:setfile -m mm/dd/yy hh:mm:ss filename
setfile -d '1/1/2021 18:18:0' sysin.txt
setfile -m '1/1/2021 18:18:0' sysin.txt