NAS 的系统磁盘空间告急了,当初用了一张就笔记本闲置的固态硬盘,只有 128G,心想光做系统盘应该足够了,结果还是经不住折腾,需要清理一下磁盘空间。本文记录了一些常用的清理磁盘空间的方法。

查看磁盘使用情况

df -h

清理软件包

# 清理旧版本缓存
apt-get autoclean
# 清理所有缓存
apt-get clean
# 移除孤立软件
apt-get autoremove

清理日志

# 查看 journal 日志占用的硬盘空间
journalctl -x --disk-usage
# 清理日志到只剩下 10M
journalctl --vacuum-size=10M
# 清理一天前的日志
journalctl --vacuum-time=1d
# 永久限制 journal 日志的大小:
[Journal]
SystemMaxUse=10M   # 硬盘中只保留最近 10M 的日志
RuntimeMaxUse=10M  # 内存中只保留最近 10M 的日志

清理 Docker

# 清理所有停止的容器
docker container prune
# 清理所有未使用的镜像
docker image prune
# 清理所有未使用的卷
docker volume prune
# 清理所有未使用的网络
docker network prune
# 清理所有未使用的镜像、容器、网络、卷,如果你使用docker的话,这一步会释放大量的空间
docker system prune --volumes
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all volumes not used by at least one container
  - all dangling images
  - all dangling build cache

清理 pip 缓存

# 清理 pip 缓存
pip cache purge
rm -r ~/.cache/pip

大文件查找清理

# 查找大于 100M 的文件
find / -type f -size +100M
# 删除大于 100M 的文件
find / -type f -size +100M -delete