Clean out /lib/modules on Debian systems

Debian

On Debian-based systems /lib/modules tends to fill up with obsolete files. The reason is that kernel packages have to be purged to remove those files, and that doesn't happen while normal upgrade/remove operations. This script is my solution to this problem. Deletion is disabled by default, verify the output of the script before commenting out the rm line.

Raw
#!/usr/bin/env bash

start=$(pwd)
target="/lib/modules/"

cd "$target" || exit 1
while IFS= read -r -d '' modules; do
  # echo "Checking $modules"
  delete=1
  while IFS= read -r -d '' kernel; do
    debianversion="${kernel/vmlinuz-/}"
    osmcversion="${kernel/kernel-/}"
    osmcversion="${osmcversion/%.img/}"
    if [[ "$debianversion" == "$modules" ]]; then
      echo "Keeping $modules (debian)"
      delete=0
    elif [[ "$osmcversion" == "$modules" ]]; then
      echo "Keeping $modules (osmc)"
      delete=0
    fi
  done < <(find /boot/ -maxdepth 1 \( -name "vmlinuz-*" -o -iname "kernel-*" \) -printf "%f\0")
  if [[ $delete -eq 1 ]]; then
    if [[ -d "$modules" ]]; then
      echo "Deleting $modules"
      # /bin/rm -rfv "$modules"
    fi
  fi
done < <(find "$target" -mindepth 1 -maxdepth 1 -type d -printf "%f\0")
cd "$start" || exit 1

0 comments

Reply

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.
DjangoPythonBitcoinTuxDebianHTML5 badgeSaltStackUpset confused bugMoneyHackerUpset confused bugX.OrggitFirefoxWindowMakerBashIs it worth the time?i3 window managerWagtailContainerIrssiNginxSilenceUse a maskWorldInternet securityPianoFontGnuPGThunderbirdJenkinshome-assistant-logo