用脚本实现自动删除历史记录

wen 实用脚本 3

本文目录导读:

用脚本实现自动删除历史记录

  1. Windows 系统 - 删除浏览器历史记录 (PowerShell)
  2. macOS 系统 - 删除浏览器历史记录 (Bash)
  3. Linux 系统 - 删除历史记录 (Bash)
  4. 定时任务设置
  5. 清理所有常见历史记录 (跨平台Python脚本)
  6. 注意事项

Windows 系统 - 删除浏览器历史记录 (PowerShell)

# 删除Chrome历史记录
$chromePath = "$env:LOCALAPPDATA\Google\Chrome\User Data\Default"
if (Test-Path "$chromePath\History") {
    Remove-Item "$chromePath\History" -Force
    Write-Host "Chrome历史记录已删除"
}
# 删除Edge历史记录
$edgePath = "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default"
if (Test-Path "$edgePath\History") {
    Remove-Item "$edgePath\History" -Force
    Write-Host "Edge历史记录已删除"
}
# 删除Firefox历史记录
$firefoxPath = "$env:APPDATA\Mozilla\Firefox\Profiles"
Get-ChildItem $firefoxPath -Directory | ForEach-Object {
    $historyFile = Join-Path $_.FullName "places.sqlite"
    if (Test-Path $historyFile) {
        Remove-Item $historyFile -Force
        Write-Host "Firefox历史记录已删除: $($_.Name)"
    }
}
# 清理Windows事件日志
wevtutil cl System
wevtutil cl Application
wevtutil cl Security
Write-Host "Windows事件日志已清理"
# 清理运行历史
Remove-Item "$env:APPDATA\Microsoft\Windows\Recent\*" -Recurse -Force
Write-Host "最近文件记录已清理"

macOS 系统 - 删除浏览器历史记录 (Bash)

#!/bin/bash
# 删除Safari历史记录
if [ -d ~/Library/Safari ]; then
    rm -f ~/Library/Safari/History.db
    rm -f ~/Library/Safari/History.db-lock
    rm -f ~/Library/Safari/LastSession.plist
    echo "Safari历史记录已删除"
fi
# 删除Chrome历史记录
if [ -d ~/Library/Application\ Support/Google/Chrome/Default ]; then
    rm -f ~/Library/Application\ Support/Google/Chrome/Default/History
    rm -f ~/Library/Application\ Support/Google/Chrome/Default/History-journal
    echo "Chrome历史记录已删除"
fi
# 删除Firefox历史记录
find ~/Library/Application\ Support/Firefox/Profiles/*.default-release/ -name "places.sqlite" -exec rm -f {} \;
echo "Firefox历史记录已删除"
# 清理系统日志
sudo rm -rf /private/var/log/*
echo "系统日志已清理"
# 清理最近文件
rm -rf ~/Library/Recent\ Items/*
echo "最近文件记录已清理"

Linux 系统 - 删除历史记录 (Bash)

#!/bin/bash
# 清理bash历史
> ~/.bash_history
history -c
echo "Bash历史已清理"
# 清理浏览器历史(需要先关闭浏览器)
# Chrome/Chromium
rm -f ~/.config/google-chrome/Default/History
rm -f ~/.config/chromium/Default/History
echo "Chrome/Chromium历史已清理"
# Firefox
find ~/.mozilla/firefox/*.default/ -name "places.sqlite" -exec rm -f {} \;
echo "Firefox历史已清理"
# 清理系统日志
sudo journalctl --vacuum-time=1s
sudo rm -rf /var/log/*.log
echo "系统日志已清理"
# 清理最近文件记录
> ~/.recently-used
echo "最近文件记录已清理"

定时任务设置

Windows (任务计划程序)

# 创建定时任务(每天执行一次)
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File C:\Scripts\clear_history.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At "03:00AM"
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
Register-ScheduledTask -TaskName "ClearHistory" -Action $action -Trigger $trigger -Principal $principal

Linux/Mac (Crontab)

# 编辑crontab
crontab -e
# 添加定时任务(每天凌晨3点执行)
0 3 * * * /path/to/clear_history.sh
# 或者每隔2小时执行
0 */2 * * * /path/to/clear_history.sh

清理所有常见历史记录 (跨平台Python脚本)

#!/usr/bin/env python3
import os
import platform
import shutil
import sqlite3
from pathlib import Path
def clear_browser_history():
    system = platform.system()
    if system == "Windows":
        # Chrome
        chrome_path = Path(os.environ['LOCALAPPDATA']) / "Google" / "Chrome" / "User Data" / "Default"
        if (chrome_path / "History").exists():
            (chrome_path / "History").unlink()
            print("Chrome历史已清理")
        # Edge
        edge_path = Path(os.environ['LOCALAPPDATA']) / "Microsoft" / "Edge" / "User Data" / "Default"
        if (edge_path / "History").exists():
            (edge_path / "History").unlink()
            print("Edge历史已清理")
    elif system == "Darwin":  # macOS
        # Safari
        safari_path = Path.home() / "Library" / "Safari"
        for file in ["History.db", "History.db-lock", "LastSession.plist"]:
            if (safari_path / file).exists():
                (safari_path / file).unlink()
        print("Safari历史已清理")
    elif system == "Linux":
        # Chrome
        chrome_path = Path.home() / ".config" / "google-chrome" / "Default"
        if (chrome_path / "History").exists():
            (chrome_path / "History").unlink()
            print("Chrome历史已清理")
def clear_terminal_history():
    history_file = Path.home() / ".bash_history"
    if history_file.exists():
        history_file.write_text("")
        print("终端历史已清理")
def main():
    print("开始清理历史记录...")
    clear_browser_history()
    clear_terminal_history()
    print("清理完成!")
if __name__ == "__main__":
    main()

注意事项

  1. 备份重要数据:删除前确保没有重要数据
  2. 关闭浏览器:清理浏览器历史时需要先关闭浏览器
  3. 管理员权限:某些操作需要管理员/root权限
  4. 系统差异:不同系统的路径可能不同,需要适当调整
  5. 定时任务:设置定时任务时注意执行权限

选择合适的脚本并根据你的操作系统进行相应调整即可实现自动删除历史记录。

抱歉,评论功能暂时关闭!