linux-server-cleaner-script/remove-heavy-packages.sh

53 lines
1.7 KiB
Bash

#!/bin/bash
# Display the 20 heaviest installed packages
echo "Top 20 heaviest installed packages:"
echo "-----------------------------------"
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -rn | awk '{printf "%d. %.3f MB\t%s\n", NR, $1/1024, $2}' | head -n 20
# Prompt user for input
echo -e "\nDo you want to remove any of these packages? (y/N): "
read -r confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
echo -e "\nEnter the numbers of the packages to remove (e.g., 1 3 5), or press Enter to skip: "
read -r choices
if [ -n "$choices" ]; then
# Get the package names for the chosen numbers
packages_to_remove=()
for choice in $choices; do
package=$(dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -rn | awk '{print $2}' | head -n 20 | sed -n "${choice}p")
if [ -n "$package" ]; then
packages_to_remove+=("$package")
else
echo "Invalid choice: $choice"
fi
done
# Confirm and remove the selected packages
if [ ${#packages_to_remove[@]} -gt 0 ]; then
echo -e "\nThe following packages will be removed: ${packages_to_remove[*]}"
echo -e "Are you sure? (y/N): "
read -r final_confirm
if [[ "$final_confirm" =~ ^[Yy]$ ]]; then
apt remove --purge "${packages_to_remove[@]}"
apt autoremove -y
echo "Selected packages removed successfully."
else
echo "Operation cancelled."
fi
else
echo "No valid packages selected for removal."
fi
else
echo "No packages selected. Exiting."
fi
else
echo "No action taken. Exiting."
fi