如何更改文件时间和日期(图文)
更改文件时间和日期有多种方法,主要包括修改三种时间属性:访问时间(atime)、修改时间(mtime)和状态改变时间(ctime)。以下是详细的修改方法:
使用
1. Linux/macOS 系统
使用 touch 命令(最常用)
基本语法
bash
touch [选项] [时间] 文件名修改不同时间属性
bash
# 修改访问时间 (-a) 和修改时间 (-m) touch -a -t 202312251430 filename.txt # 只修改访问时间 touch -m -t 202312251430 filename.txt # 只修改修改时间 touch -t 202312251430 filename.txt # 同时修改访问时间和修改时间 # 使用 -d 选项指定日期时间 touch -d "2023-12-25 14:30:00" filename.txt touch -d "Dec 25 2023" filename.txt touch -d "2 weeks ago" filename.txt时间格式说明
bash
# 格式: [[CC]YY]MMDDhhmm[.ss] # CC - 世纪 (可选) # YY - 年份后两位 (可选) # MM - 月份 (01-12) # DD - 日期 (01-31) # hh - 小时 (00-23) # mm - 分钟 (00-59) # ss - 秒数 (00-60, 可选) # 示例 touch -t 202312251430.45 filename.txt # 2023年12月25日14点30分45秒批量修改
bash
# 修改当前目录所有文件 touch -m -t 202312251430 * # 按文件类型批量修改 touch -m -t 202312251430 *.txt *.pdf # 递归修改子目录所有文件 find . -type f -exec touch -m -t 202312251430 {} \; # 按条件筛选修改 find . -name "*.jpg" -exec touch -m -t 202312251430 {} \;2. Windows 系统
使用 PowerShell
powershell
# 修改文件最后写入时间(相当于修改时间) (Get-Item "filename.txt").LastWriteTime = "2023-12-25 14:30:00" # 修改文件创建时间 (Get-Item "filename.txt").CreationTime = "2023-12-25 14:30:00" # 修改文件访问时间 (Get-Item "filename.txt").LastAccessTime = "2023-12-25 14:30:00" # 批量修改多个文件 Get-ChildItem "*.txt" | ForEach-Object { $_.LastWriteTime = "2023-12-25 14:30:00" } # 递归修改子目录文件 Get-ChildItem -Recurse | ForEach-Object { $_.LastWriteTime = "2023-12-25 14:30:00" }使用命令提示符
cmd
# 更新文件时间为当前时间 copy /b filename.txt +,, # 批量更新 for %f in (*.txt) do copy /b "%f" +,,3. 编程方式修改
Python 脚本
python
import os import time from datetime import datetime def change_file_times(filepath, access_time=None, modify_time=None): """ 修改文件的时间属性 :param filepath: 文件路径 :param access_time: 访问时间 (格式: "2023-12-25 14:30:00") :param modify_time: 修改时间 (格式: "2023-12-25 14:30:00") """ # 获取当前时间戳 current_atime = os.path.getatime(filepath) current_mtime = os.path.getmtime(filepath) # 转换指定时间 if access_time: atime = time.mktime(datetime.strptime(access_time, "%Y-%m-%d %H:%M:%S").timetuple()) else: atime = current_atime if modify_time: mtime = time.mktime(datetime.strptime(modify_time, "%Y-%m-%d %H:%M:%S").timetuple()) else: mtime = current_mtime # 修改文件时间 os.utime(filepath, (atime, mtime)) print(f"已修改 {filepath} 的时间") # 使用示例 change_file_times("filename.txt", access_time="2023-12-25 10:00:00", modify_time="2023-12-25 14:30:00") # 批量修改 import glob for file in glob.glob("*.txt"): change_file_times(file, modify_time="2023-12-25 14:30:00")Node.js 脚本
javascript
const fs = require('fs'); function changeFileTimes(filepath, times) { const { accessTime, modifyTime } = times; // 转换时间格式 const atime = accessTime ? new Date(accessTime).getTime() / 1000 : undefined; const mtime = modifyTime ? new Date(modifyTime).getTime() / 1000 : undefined; // 获取当前时间 const stats = fs.statSync(filepath); const currentAtime = stats.atime.getTime() / 1000; const currentMtime = stats.mtime.getTime() / 1000; // 修改文件时间 fs.utimesSync(filepath, atime || currentAtime, mtime || currentMtime); console.log(`已修改 ${filepath} 的时间`); } // 使用示例 changeFileTimes('filename.txt', { accessTime: '2023-12-25T10:00:00', modifyTime: '2023-12-25T14:30:00' });4. 查看文件时间信息
Linux/macOS
bash
# 查看详细时间信息 stat filename.txt # 只查看修改时间 stat -c %y filename.txt # 查看访问时间 stat -c %x filename.txt # 列表显示(简化格式) ls -l filename.txtWindows PowerShell
powershell
# 查看文件详细时间信息 Get-Item "filename.txt" | Select-Object Name, LastWriteTime, CreationTime, LastAccessTime5. 高级技巧
按条件批量修改
bash
# 只修改大于1MB的文件 find . -type f -size +1M -exec touch -m -t 202312251430 {} \; # 只修改一周内修改过的文件 find . -type f -mtime -7 -exec touch -m -t 202312251430 {} \; # 按文件扩展名筛选 find . \( -name "*.doc" -o -name "*.pdf" \) -exec touch -m -t 202312251430 {} \;保持时间间隔关系
bash
#!/bin/bash # 保持文件间原有时间间隔 REFERENCE_FILE="reference.txt" NEW_BASE_TIME="202312251430" # 计算时间偏移 OLD_TIME=$(stat -c %Y "$REFERENCE_FILE") NEW_TIME=$(date -d "2023-12-25 14:30:00" +%s) TIME_OFFSET=$((NEW_TIME - OLD_TIME)) # 应用偏移到所有文件 find . -type f -exec sh -c ' for file; do old_mtime=$(stat -c %Y "$file") new_mtime=$((old_mtime + TIME_OFFSET)) touch -m -d "@$new_mtime" "$file" done ' _ {} +注意事项
- 权限要求: 需要对目标文件具有写权限
- ctime 限制: 状态改变时间(ctime)通常不能直接修改,只有在文件属性改变时自动更新
- 备份重要文件: 批量操作前建议备份重要数据
- 系统审计: 修改系统关键文件时间戳可能影响审计追踪
- 跨平台差异: 不同操作系统的命令和行为可能略有差异

更新时间:2025-12-15 13:19:33
上一篇:上传日期修改怎么弄的啊(图文)
下一篇:上传日期修改怎么弄的出来(图文)
