如何修改文件的 “创建时间” 和 “修改时间” (macOS Linux Windows)

如何修改文件的 “创建时间” 和 “修改时间” (macOS, Linux, Windows)

2023 修正版

请访问原文链接:https://sysin.org/blog/how-to-change-file-date/,查看最新版。原创作品,转载请保留出处。

作者主页:www.sysin.org


1. macOS(创建时间,修改时间)

图形界面(GUI)

在 macOS 中,点击文件右键 “显示简介”,显示 “创建时间” 和 “修改时间”。

macOS 显示简介

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

修改 “创建时间” 和 “修改时间”

推荐使用这个 App:A Better Finder Attributes(商业软件,自行搜索),操作简单便捷。

A Better Finder Attributes

终端(Terminal)

在 Darwin 系统部分(Darwin 一般是指 macOS 的命令行部分),类似于 Linux 有 atime、mtime 和 ctime,但是还多了 birthtime 即创建时间。

直接使用 stat 命令可以查看文件时间属性,可以看到有 4 个时间,但是不友好,没法直接辨别。其实分别是:Access、Modify、Change 和 Birth。

但作为正统 Unix 系统,Darwin 的 stat 命令有额外的参数:

现在加上 -x 参数可以看到 Access、Modify、Change 和 Birth 四个时间。

之前的旧版系统中需要 -s 参数才能显示 birthtime。

# sysin @ macOS in ~/Desktop [17:32:20]
$ stat sysin.txt
16777225 9617704 -rw-r--r-- 1 sysin staff 0 0 "Feb  2 17:32:28 2023" "Feb  2 17:32:28 2023" "Feb  2 17:32:28 2023" "Feb  2 17:32:28 2023" 4096 0 0 sysin.txt

# sysin @ macOS in ~/Desktop [17:32:32]
$ stat -x sysin.txt
  File: "sysin.txt"
  Size: 0            FileType: Regular File
  Mode: (0644/-rw-r--r--)         Uid: (  501/      sysin)  Gid: (   20/   staff)
Device: 1,9   Inode: 9617704    Links: 1
Access: Thu Feb  2 17:32:28 2023
Modify: Thu Feb  2 17:32:28 2023
Change: Thu Feb  2 17:32:28 2023
 Birth: Thu Feb  2 17:32:28 2023

# sysin @ macOS in ~/Desktop [17:32:39]
$ stat -s sysin.txt
st_dev=16777225 st_ino=9617704 st_mode=0100644 st_nlink=1 st_uid=501 st_gid=20 st_rdev=0 st_size=0 st_atime=1675330348 st_mtime=1675330348 st_ctime=1675330348 st_birthtime=1675330348 st_blksize=4096 st_blocks=0 st_flags=0

修改时间可以使用 touch 和 setfile 命令

touch(同 Linux,修改时间 atime 和访问时间 mtime,无 “创建时间”)

# 修改 “修改时间”
touch -m -t YYYYMMDDhhmm filename
# 例如
touch -mt 202305181505 sysin.txt

# 修改 “访问时间和修改时间”
touch -t YYYYMMDDhhmm filename
# 例如
touch -t 202305191505 sysin.txt

# 释义
YYYY  年 - 四位数
MM    月 - 两位数
DD    日 - 两位数
hh    小时 - 两位数
mm    分钟 - 两位数

实例


# sysin @ macOS in ~/Desktop [17:32:50]
$ touch -mt 202305181505 sysin.txt

# sysin @ macOS in ~/Desktop [17:34:18]
$ stat -x sysin.txt
  File: "sysin.txt"
  Size: 0            FileType: Regular File
  Mode: (0644/-rw-r--r--)         Uid: (  501/      sysin)  Gid: (   20/   staff)
Device: 1,9   Inode: 9617704    Links: 1
Access: Thu Feb  2 17:32:28 2023 #没有变化
Modify: Thu May 18 15:05:00 2023
Change: Thu Feb  2 17:34:18 2023 #变更为命令执行的时间
 Birth: Thu Feb  2 17:32:28 2023 #没有变化

# sysin @ macOS in ~/Desktop [17:34:24]
$ touch -t 202305191505 sysin.txt

# sysin @ macOS in ~/Desktop [17:36:13]
$ stat -x sysin.txt
  File: "sysin.txt"
  Size: 0            FileType: Regular File
  Mode: (0644/-rw-r--r--)         Uid: (  501/      sysin)  Gid: (   20/   staff)
Device: 1,9   Inode: 9617704    Links: 1
Access: Fri May 19 15:05:00 2023
Modify: Fri May 19 15:05:00 2023
Change: Thu Feb  2 17:36:13 2023 #变更为命令执行的时间
 Birth: Thu Feb  2 17:32:28 2023 #没有变化

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

实例

# sysin @ macOS in ~/Desktop [17:44:44]
$ setfile -d '1/1/2023 18:18:0' sysin.txt

# sysin @ macOS in ~/Desktop [17:44:49]
$ stat -x sysin.txt
  File: "sysin.txt"
  Size: 0            FileType: Regular File
  Mode: (0644/-rw-r--r--)         Uid: (  501/      sysin)  Gid: (   20/   staff)
Device: 1,9   Inode: 9617704    Links: 1
Access: Fri May 19 15:05:00 2023 #无变化
Modify: Fri May 19 15:05:00 2023 #无变化
Change: Thu Feb  2 17:44:49 2023 #变更为命令执行的时间
 Birth: Sun Jan  1 18:18:00 2023

# sysin @ macOS in ~/Desktop [17:44:51]
$ setfile -m '1/1/2023 20:18:0' sysin.txt

# sysin @ macOS in ~/Desktop [17:45:09]
$ stat -x sysin.txt
  File: "sysin.txt"
  Size: 0            FileType: Regular File
  Mode: (0644/-rw-r--r--)         Uid: (  501/      sysin)  Gid: (   20/   staff)
Device: 1,9   Inode: 9617704    Links: 1
Access: Fri May 19 15:05:00 2023 #无变化
Modify: Sun Jan  1 20:18:00 2023
Change: Thu Feb  2 17:45:09 2023 #变更为命令执行的时间
 Birth: Sun Jan  1 18:18:00 2023 #无变化

2. Linux(修改时间,访问时间)

这里仅描述 shell 中的情形,图形界面中未有关注。

注意:Linux(包括传统 Unix)中没有 “创建(creation)” 时间的概念。

查看文件时间信息(stat 命令)

现代 Linux 已经可以显示 Birth,即 “创建(creation)” 时间。

# 创建一个测试文件
# root @ A9 in ~ [17:59:07]
$ touch sysin.txt

# 使用 stat 命令查看文件时间信息
# root @ A9 in ~ [17:59:12]
$ stat sysin.txt
  File: sysin.txt
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 201327432   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2023-02-02 17:59:12.688566820 +0800
Modify: 2023-02-02 17:59:12.688566820 +0800
Change: 2023-02-02 17:59:12.688566820 +0800
 Birth: 2023-02-02 17:59:12.688566820 +0800

有如下三种时间

touch 修改时间

......

请访问原文链接:https://sysin.org/blog/how-to-change-file-date/,查看最新版。

......

Change: CTime 没有直接修改方法,最简单的办法是修改系统时间:

......

请访问原文链接:https://sysin.org/blog/how-to-change-file-date/,查看最新版。

......

3. Windows(创建时间,修改时间,访问时间)

图形界面

点击一个文件右键 “属性” 即可查看文件的时间属性,可以看到有 “创建时间”、“修改时间” 和 “访问时间” 三个属性。

Windows 文件属性

“创建时间” 和 “修改时间” 比较好理解,但 “访问时间” 似乎有点特殊,查看文件属性、打开文件查看,甚至设置 “只读”、“隐藏” 属性都不会改变 “访问时间”。只有在对文件进行编辑后访问时间才会改变。这也就是我们会发现访问时间与修改时间是一样的原因。

修改时间的工具

......

请访问原文链接:https://sysin.org/blog/how-to-change-file-date/,查看最新版。

......

命令行修改

CMD

#修改当前系统时间
date 2021/01/01
time 10:59:30

#修改时间,注意是连续两个英文逗号
copy 文件名 +,,

#修改时间和访问时间,注意是连续两个英文句号
copy 文件名 +..

# 注意修改完毕需要将系统时间修改过来(或者等待 NTP 同步)

小技巧:在文件夹上添加 “命令提示符” 右键快捷访问菜单

......

请访问原文链接:https://sysin.org/blog/how-to-change-file-date/,查看最新版。

......

Powershell(推荐)

......

请访问原文链接:https://sysin.org/blog/how-to-change-file-date/,查看最新版。

......

小技巧:在文件夹上添加 “PowerShell” 右键快捷访问菜单

......

请访问原文链接:https://sysin.org/blog/how-to-change-file-date/,查看最新版。

......

讨论

讨论:文件的时间属性存储在哪里?

展开阅读全文

页面更新:2024-05-16

标签:文件   时间   最新版   原文   属性   文件属性   命令   日期   链接   系统

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top