Skip to content

Instantly share code, notes, and snippets.

@jkehelwala
Created August 30, 2021 16:55
Show Gist options
  • Save jkehelwala/c54540cee0975a5e5c6b6a8a65bdcbb3 to your computer and use it in GitHub Desktop.
Save jkehelwala/c54540cee0975a5e5c6b6a8a65bdcbb3 to your computer and use it in GitHub Desktop.
Clean space in Ubuntu
#!/bin/bash
## References: https://itsfoss.com/free-up-space-ubuntu-linux/
function remove_snap_revs() {
set -eu
snap list --all | awk '/disabled/{print $1, $3}' |
while read snapname revision; do
snap remove "$snapname" --revision="$revision"
done
}
function run_command() {
case $1 in
autoremove) sudo apt-get autoremove ;;
autoclean) sudo apt-get autoclean ;; # clean outdated
allclean) sudo apt-get clean ;; # clean all
journalvacuum) sudo journalctl --vacuum-time=3d ;; # Remove logs older than 3 days
cleanthumbcache) rm -rf ~/.cache/thumbnails/* ;; # Remove thumnail cache
snapclean) remove_snap_revs ;; # Remove snap revisions
esac
}
function proceed_confirm() {
echo -e "\nDo you wish to $1 ?"
select yn in "Yes" "No"; do
case $yn in
Yes ) run_command $2; echo -e "Action completed\n"; break ;;
No ) break ;;
esac
done
}
# Apt Cache
proceed_confirm "run apt autoremove" autoremove
echo -e "\nCheck apt cache size"
sudo du -sh /var/cache/apt
proceed_confirm "clean outdated apt packages" autoclean
echo "Updated Size"
sudo du -sh /var/cache/apt
proceed_confirm "clean all apt packages" allclean
echo "Updated Size"
sudo du -sh /var/cache/apt
# Journal Logs
echo -e "\nCheck journal diskusage"
journalctl --disk-usage
proceed_confirm "remove logs older than 3 days" journalvacuum
echo "Updated Size"
journalctl --disk-usage
# Thumbnail cache
echo -e "\nCheck thumnail cache"
du -sh ~/.cache/thumbnails
proceed_confirm "clean thumnail cache" cleanthumbcache
echo "Updated Size"
du -sh ~/.cache/thumbnails
# Snap Version Removal
echo -e "\nCheck snap usage"
du -h /var/lib/snapd/snaps
echo -e "\n Please close all snaps before removing old revisions of snaps."
proceed_confirm "clean old revisions of snaps" cleanthumbcache
echo "Updated Size"
du -h /var/lib/snapd/snaps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment