一键清理CentOS系统磁盘空间,清理缓存,清理磁盘,清理垃圾,清理内存,清理硬盘,一键清理系统缓存,日记,不必要的内容,容器日记缓存,等系统缓存日记
一键清理CentOS系统磁盘空间,清理缓存,清理磁盘,清理垃圾,清理内存,清理硬盘,一键清理系统缓存,日记,不必要的内容,容器日记缓存,等系统缓存日记
系统:CentOS Stream,CentOS 9,CentOS 8,CentOS 10,CentOS 7
软件:清理
功能:清理缓存,清理磁盘,清理垃圾
下载地址一:

建立文件:ql.sh
赋予脚本执行权限
sudo chmod +x /root/ql.sh
-------------------------------------------------------
#!/bin/bash
# cleanup.sh - CentOS 7.9 一键清理脚本
# 运行前请备份重要数据并在测试环境验证
# 以 root 身份运行
set -euo pipefail
# 显示当前磁盘使用
echo "=== 当前磁盘使用情况 ==="
df -hT --total
read -p "确认要继续执行清理操作吗?这会删除临时文件并清理缓存/日志。输入 yes 继续: " confirm
if [[ "$confirm" != "yes" ]]; then
echo "已取消。"
exit 0
fi
echo
echo "=== 1) 清理 yum 缓存 ==="
yum clean all || true
rm -rf /var/cache/yum/* || true
echo "yum 缓存已清理。"
echo
echo "=== 2) 清理 package manager orphaned packages (可选) ==="
read -p "是否尝试删除孤立包(autoremove)?输入 yes 执行,否则跳过: " autoremove
if [[ "$autoremove" == "yes" ]]; then
# CentOS 7 默认没有 dnf autoremove;使用 yum-plugin-remove-with-leaves 或手动处理
if command -v package-cleanup >/dev/null 2>&1; then
package-cleanup --leaves --all || true
else
echo "未检测到 package-cleanup,跳过自动删除孤立包。"
fi
fi
echo
echo "=== 3) 清空系统缓存(pagecache、dentries、inodes) ==="
# 先同步磁盘
sync
# 0 = free pagecache, 1 = free dentries and inodes, 2 = free pagecache, dentries and inodes
echo 3 > /proc/sys/vm/drop_caches || true
echo "已释放 pagecache、dentries、inodes(短期内可能影响性能)。"
echo
echo "=== 4) 清理 /tmp 和 /var/tmp ==="
# 仅删除 7 天未访问的临时文件(可调整 -mtime 值)
find /tmp -mindepth 1 -maxdepth 3 -type f -atime +7 -print0 | xargs -0 -r rm -f --
find /tmp -mindepth 1 -maxdepth 3 -type d -empty -print0 | xargs -0 -r rmdir --
find /var/tmp -mindepth 1 -type f -atime +7 -print0 | xargs -0 -r rm -f --
echo "/tmp 和 /var/tmp 中 7 天未访问的文件已删除(保留最近文件)。"
echo
echo "=== 5) 清理 systemd journal 日志 ==="
# 显示当前 journal 占用
journalctl --disk-usage || true
read -p "是否将 journal 日志限制为 200M 并立即清理?输入 yes 执行: " clean_journal
if [[ "$clean_journal" == "yes" ]]; then
# 永久限制(写入配置)
mkdir -p /etc/systemd/journald.conf.d
cat > /etc/systemd/journald.conf.d/limit.conf <<'EOF'
[Journal]
SystemMaxUse=200M
RuntimeMaxUse=200M
EOF
# 立即清理到目标大小
journalctl --vacuum-size=200M || true
systemctl restart systemd-journald || true
echo "journal 已限制并清理到 200M。"
else
echo "跳过 journal 清理。"
fi
echo
echo "=== 6) 清理 /var/log 下的大文件与旧日志 ==="
# 列出最大的 20 个日志文件供人工确认
echo "下面列出 /var/log 下最大的 20 个文件:"
find /var/log -type f -printf '%s %p\n' 2>/dev/null | sort -nr | head -n 20 | awk '{printf "%10.2f MB\t%s\n",$1/1024/1024,$2}'
echo
read -p "是否自动压缩超过 50M 的日志文件并保留原始(压缩后删除原文件)?输入 yes 执行: " compress_logs
if [[ "$compress_logs" == "yes" ]]; then
find /var/log -type f -size +50M -print0 | while IFS= read -r -d '' file; do
echo "压缩并删除原文件: $file"
gzip -9 "$file" && echo "已压缩 $file"
done
fi
# 可选:清空特定日志文件(谨慎)
read -p "是否清空常见大日志文件(/var/log/messages, /var/log/secure, /var/log/maillog)?输入 yes 清空: " truncate_logs
if [[ "$truncate_logs" == "yes" ]]; then
for f in /var/log/messages /var/log/secure /var/log/maillog; do
if [[ -f "$f" ]]; then
: > "$f"
echo "已清空 $f"
fi
done
systemctl restart rsyslog || true
fi
echo
echo "=== 7) 清理 yum 日志和临时缓存(可选) ==="
read -p "是否删除 /var/log/yum.log(会丢失 yum 历史)?输入 yes 删除: " del_yumlog
if [[ "$del_yumlog" == "yes" ]]; then
rm -f /var/log/yum.log || true
echo "/var/log/yum.log 已删除。"
fi
echo
echo "=== 8) 清理用户回收站(root 和普通用户) ==="
# root 回收站
rm -rf /root/.local/share/Trash/* 2>/dev/null || true
# 所有用户回收站(/home/*/.local/share/Trash)
for d in /home/*/.local/share/Trash 2>/dev/null; do
rm -rf "$d"/* 2>/dev/null || true
done
echo "用户回收站已清理(如存在)。"
echo
echo "=== 9) 可选:清理 docker 镜像/容器(如果安装并使用 Docker) ==="
if command -v docker >/dev/null 2>&1; then
read -p "检测到 docker,是否执行 docker system prune -af(删除未使用的容器/镜像/网络)?输入 yes 执行: " docker_prune
if [[ "$docker_prune" == "yes" ]]; then
docker system prune -af || true
echo "docker 未使用资源已清理。"
fi
fi
echo
echo "=== 清理完成,显示磁盘使用情况 ==="
df -hT --total
echo
echo "建议:"
echo "- 若 /var 分区空间紧张,检查 /var/lib、/var/log、/var/cache 的具体占用并考虑迁移大数据目录到新磁盘或扩容。"
echo "- 对于长期解决方案,考虑添加磁盘或调整分区、使用 LVM 扩容、或将日志/数据目录迁移到独立分区。"
echo "脚本执行完毕。"
-------------------------------------------------------
参考来源:
项目地址:
官网地址:
参考地址:
延伸阅读:
推荐一键清理CentOS系统磁盘空间,清理内存,清理缓存,清理磁盘,清理垃圾,清理硬盘,一键清理系统缓存,日记,不必要的内容,容器日记缓存,等系统缓存日记
推荐一键清理CentOS系统磁盘空间,清理内存,清理缓存,清理磁盘,清理垃圾,清理硬盘,一键清理系统缓存,日记,不必要的…

CentOS设置虚拟内存,虚拟内存设置,CentOS 7.9怎么设置虚拟内存2G,CentOS内存不足死机优化教程,虚拟内存压缩设置
CentOS设置虚拟内存,虚拟内存设置,CentOS 7.9怎么设置虚拟内存2G,CentOS内存不足死机优化教程,虚拟…

CentOS设置虚拟内存,虚拟内存设置,CentOS 7.9怎么设置虚拟内存2G
CentOS设置虚拟内存,虚拟内存设置,,CentOS 7.9怎么设置虚拟内存2G系统:CentOS Stream,Ce…

CentOS关闭虚拟内存,虚拟内存,在 CentOS 7.9 中,删除或关闭虚拟内存(Swap)主要分为两步:临时关闭(立即生效)和永久删除(防止重启后恢复)
CentOS关闭虚拟内存,虚拟内存,在 CentOS 7.9 中,删除或关闭虚拟内存(Swap)主要分为两步:临时关闭(…

nginx,nginx反向代理,nginx端口映射,nginx端口转发,nginx中转,nginx代理,nginx转发,nginx编译版本端口转发,nginx安装
nginx,nginx反向代理,nginx端口映射,nginx端口转发,nginx中转,nginx代理,nginx转发,…





评论(0):