系统默认临时目录
/tmp
- 系统临时目录,重启后通常会被清空/var/tmp
- 长期临时文件,通常保留较长时间用户级临时目录
~/.cache
- 用户应用程序缓存~/.local/share/Trash
- 用户回收站~/.thumbnails
- 缩略图缓存应用程序特定临时文件
/var/log
- 系统日志文件/var/cache
- 应用程序缓存~/.mozilla
, ~/.cache/chromium
)# 清理/tmp目录(需要root权限)
sudo rm -rf /tmp/*
# 清理用户缓存
rm -rf ~/.cache/*
# 清理旧的内核版本(释放/boot空间)
sudo apt autoremove --purge
# 清理软件包缓存
sudo apt clean
sudo apt autoclean
# 查找并删除大文件
find / -type f -size +100M -exec ls -lh {} \;
使用tmpwatch/tmpreaper
# 安装tmpreaper
sudo apt install tmpreaper
# 清理超过30天未访问的/tmp文件
sudo tmpreaper 30d /tmp
使用logrotate管理日志
# 编辑配置文件
sudo nano /etc/logrotate.conf
使用cron定时任务
# 编辑crontab
crontab -e
# 添加每周清理任务示例
0 3 * * 0 /usr/bin/find /tmp -type f -atime +7 -delete
定期清理策略
安全注意事项
rm -rf /
等危险命令空间监控工具
# 查看磁盘使用情况
df -h
# 查看目录大小
du -sh /*
# 交互式磁盘使用分析
ncdu
特殊文件处理
lsof | grep deleted
查找已删除但仍被进程占用的文件通过合理管理临时文件,可以保持Linux系统高效运行并避免磁盘空间不足的问题。