banner
zzzhizhi

Hi, zzzhizhi

Coding is love, coding to the world is full of love!
github
x
follow
bilibili
telegram
discord user
email

CentOS Cleanup Script

#!/bin/bash
 
# Record available space before cleanup
before=$(df -h / | awk '/\// {print $4}')
 
# Clean yum cache
yum clean all
 
# Clean old log files
find /var/log -type f -name "*.log" -exec truncate --size 0 {} \;
 
# Empty trash
echo "Emptying trash..."
rm -rf /root/.local/share/Trash/*/** &> /dev/null
 
# Clear command history
history -c
history -w
 
# Clean temporary files
rm -rf /tmp/*
rm -rf /var/tmp/*
 
# Clean old system backups
rm -rf /var/backups/*
 
# Clean unused packages and dependencies
yum autoremove -y
 
# Clean old kernels
package-cleanup --oldkernels --count=1 -y
 
# Clean cache files
find /var/cache -type f -exec rm -rf {} \;
 
# Clean user caches
for user in $(ls /home); do
  rm -rf /home/$user/.cache/*
done
 
# Clean mail logs
find /var/mail -type f -exec truncate --size 0 {} \;
 
# Clean core files
find / -name "core" -delete
 
# Clean old session files
find /var/lib/php/session -type f -delete
 
# Clean system mail queue
service postfix stop
rm -rf /var/spool/postfix/*
service postfix start
 
# Clean unused package cache
dnf clean packages -y
 
# Clean system crash logs
rm -rf /var/crash/*
 
# Clean journalctl logs
journalctl --rotate
journalctl --vacuum-time=1d
 
# Clean system cache
sync && echo 3 > /proc/sys/vm/drop_caches
 
# Clear command history
# history -c
# history -w
 
# Clean Docker container log files
docker rm -v $(docker ps -a -q)
rm -rf /var/lib/docker/containers/*/*-json.log
 
# Clean Docker image cache
docker image prune -a --force
 
# Clean unused Docker volumes
docker volume prune --force
 
# Clean old Docker images
docker rmi $(docker images -f "dangling=true" -q)
 
# Calculate how much file size was cleared
cleared=$(df -h / | awk '/\// {print $4}' | awk -v before="$before" '{print before - $1}')
 
# Record available space after cleanup
after=$(df -h / | awk '/\// {print $4}')
 
echo "Available space before cleanup: $before"
echo "Available space after cleanup: $after"
 
echo "Disk cleanup completed."
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.