cleaning ubuntu old packages
میرزایی
1403/08/02 08:25:26 (38)
#!/bin/bash
# Function to remove old Snap revisions
cleanup_snaps() {
echo "Removing old Snap revisions..."
snap list --all | awk '/disabled/{print $1, $3}' |
while read snapname revision; do
sudo snap remove "$snapname" --revision="$revision"
done
}
# Function to remove old kernel images
cleanup_kernels() {
echo "Removing old kernel images..."
current_kernel=$(uname -r)
kernel_pkg="linux-image"
dpkg --list | grep "$kernel_pkg" | grep -v "$current_kernel" | awk '/linux-image-[0-9]+/{print $2}' |
while read -r kernel; do
sudo apt-get purge -y "$kernel"
done
}
# Function to clean package cache
cleanup_package_cache() {
echo "Cleaning package cache..."
sudo apt-get clean
}
# Function to remove unused packages and dependencies
cleanup_unused_packages() {
echo "Removing unused packages and dependencies..."
sudo apt-get autoclean -y
sudo apt-get autoremove -y
}
# Function to vacuum systemd journal logs older than 2 weeks
vacuum_journal_logs() {
echo "Vacuuming journal logs older than 1 days..."
sudo journalctl --vacuum-time=1days
}
# Function to clean up old log files in /var/log
cleanup_var_logs() {
echo "Cleaning up old log files in /var/log..."
find /var/log -type f -name "*.log" -exec sudo truncate -s 0 {} \;
find /var/log -type f -name "*.gz" -delete
sudo rm -f /var/log/*.old
sudo rm -f /var/log/*-[0-9]*.gz
sudo rm -f /var/log/*-[0-9]*.log
}
# Function to remove temporary files
cleanup_temp_files() {
echo "Removing temporary files..."
sudo rm -rf /tmp/*
}
# Run cleanup functions
cleanup_snaps
cleanup_kernels
cleanup_package_cache
cleanup_unused_packages
vacuum_journal_logs
cleanup_var_logs
cleanup_temp_files
echo "System cleanup completed."