我的知识记录

批量修改文件的修改时间(图文)

批量修改文件的修改时间有多种方法,以下是常用的几种方式:

1. 使用 touch 命令(Linux/macOS)

修改当前目录所有文件


 
bash
# 修改所有文件的修改时间为指定时间 touch -m -t 202312251430 * # 修改所有文件的修改时间为当前时间 touch -m *

按文件类型批量修改


 
bash
# 只修改 .txt 文件 touch -m -t 202312251430 *.txt # 修改多种文件类型 touch -m -t 202312251430 *.txt *.pdf *.doc

递归修改子目录文件


 
bash
# 使用 find 命令递归修改 find . -type f -exec touch -m -t 202312251430 {} \; # 只修改特定类型的文件 find . -name "*.txt" -exec touch -m -t 202312251430 {} \;

2. Windows PowerShell 批量修改

修改当前目录所有文件


 
powershell
# 修改所有文件的最后写入时间 Get-ChildItem | ForEach-Object { $_.LastWriteTime = "2023-12-25 14:30:00" }

按条件筛选文件


 
powershell
# 只修改 .txt 文件 Get-ChildItem -Filter "*.txt" | ForEach-Object { $_.LastWriteTime = "2023-12-25 14:30:00" } # 递归修改子目录文件 Get-ChildItem -Recurse | ForEach-Object { $_.LastWriteTime = "2023-12-25 14:30:00" }

3. 使用脚本批量处理

Bash 脚本示例


 
bash
#!/bin/bash # batch_modify_time.sh TARGET_TIME="202312251430" TARGET_DIR="./" # 修改指定目录下所有文件 find "$TARGET_DIR" -type f -exec touch -m -t "$TARGET_TIME" {} \; echo "批量修改完成"

Python 脚本示例


 
python
#!/usr/bin/env python3 import os import time from datetime import datetime from pathlib import Path def batch_modify_time(directory, target_time_str): """ 批量修改目录下所有文件的修改时间 :param directory: 目标目录 :param target_time_str: 目标时间字符串,格式: "2023-12-25 14:30:00" """ # 转换时间格式 target_time = datetime.strptime(target_time_str, "%Y-%m-%d %H:%M:%S") timestamp = time.mktime(target_time.timetuple()) # 遍历目录下的所有文件 for root, dirs, files in os.walk(directory): for file in files: file_path = os.path.join(root, file) try: os.utime(file_path, (timestamp, timestamp)) print(f"已修改: {file_path}") except Exception as e: print(f"修改失败 {file_path}: {e}") # 使用示例 if __name__ == "__main__": batch_modify_time("./", "2023-12-25 14:30:00")

JavaScript (Node.js) 脚本示例


 
javascript
const fs = require('fs'); const path = require('path'); function batchModifyTime(directory, targetTime) { const timestamp = new Date(targetTime).getTime(); function processDirectory(dir) { const files = fs.readdirSync(dir); files.forEach(file => { const filePath = path.join(dir, file); const stat = fs.statSync(filePath); if (stat.isDirectory()) { processDirectory(filePath); // 递归处理子目录 } else { // 修改文件的修改时间 fs.utimesSync(filePath, timestamp/1000, timestamp/1000); console.log(`已修改: ${filePath}`); } }); } processDirectory(directory); } // 使用示例 batchModifyTime('./', '2023-12-25T14:30:00');

4. 高级批量操作

按文件大小筛选修改


 
bash
# 只修改大于1MB的文件 find . -type f -size +1M -exec touch -m -t 202312251430 {} \;

按文件名模式筛选


 
bash
# 使用正则表达式匹配文件名 find . -type f -regex ".*\.\(jpg\|png\|gif\)$" -exec touch -m -t 202312251430 {} \;

创建时间范围内的文件


 
bash
# 结合 stat 命令筛选特定时间范围的文件 find . -type f -newermt "2023-01-01" ! -newermt "2023-12-01" -exec touch -m -t 202312251430 {} \;

5. 注意事项

  1. 备份重要数据: 批量操作前建议备份重要文件
  2. 权限检查: 确保对目标文件有写权限
  3. 时间格式: 注意不同系统的时间格式要求
  4. 性能考虑: 处理大量文件时注意系统性能影响
选择适合您操作系统和需求的方法即可实现批量修改文件的修改时间。
 

批量修改文件的修改时间(图文)

标签:

更新时间:2025-12-15 13:15:26

上一篇:怎么修改上传到网络的时间和日期(图文)

下一篇:文件上传失败什么原因(图文)