clear-root-2.sh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. function myclean {
  4. ## Show free space
  5. df -Th | grep -v fs
  6. # Will need English output for processing
  7. LANG=en_GB.UTF-8
  8. ## Clean apt cache
  9. apt-get update
  10. apt-get -f install
  11. apt-get -y autoremove
  12. apt-get clean
  13. ## Remove old versions of snap packages
  14. snap list --all | while read snapname ver rev trk pub notes; do
  15. if [[ $notes = *disabled* ]]; then
  16. snap remove "$snapname" --revision="$rev"
  17. fi
  18. done
  19. ## Set snap versions retain settings
  20. if [[ $(snap get system refresh.retain) -ne 2 ]]; then snap set system refresh.retain=2; fi
  21. rm -f /var/lib/snapd/cache/*
  22. ## Remove old versions of Linux Kernel
  23. # This one-liner is deprecated since 18.04
  24. # dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs apt-get -y purge
  25. # New 2 lines to remove old kernels
  26. dpkg --list | grep 'linux-image' | awk '{ print $2 }' | sort -V | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p' | xargs apt-get -y purge
  27. dpkg --list | grep 'linux-headers' | awk '{ print $2 }' | sort -V | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p' | xargs apt-get -y purge
  28. ## Rotate and delete old logs
  29. /etc/cron.daily/logrotate
  30. find /var/log -type f -iname *.gz -delete
  31. journalctl --rotate
  32. journalctl --vacuum-time=1s
  33. ## Show free space
  34. df -Th | grep -v fs
  35. }