Mostly, I don’t worry about the accumulation of old kernels building up in
sudo apt-get autoremove may scrub most of them, but sometimes it doesn’t when I’m doing something else and I must wade through the accumulation of old packages in Synaptic. Removing all those packages by hand gets tedious, but I’m reluctant to unleash a rarely used script on the clutter for fear of creating a worse problem.
The iterator in this burst of Bash line noise:
for f in $(ls /boot | grep vmlinuz | cut -d\- -f2,3 | sort | head -n -1) ; do dpkg -l | grep "^ii\ \ linux-" | grep $f | cut -d" " -f 3 >> /tmp/pkgs.txt ; done
… parses the list of kernels in
/boot into version numbers, finds the corresponding installed packages, sorts them in ascending order, discards the last entry so as to not uninstall the most recent kernel, and passes each line of the resulting list into the loop.
grep argument has two spaces after the
ii that WordPress would destroy without the escaping backslashes. You can try
"^ii linux-", but if the loop puts nothing in the file, that’s why.
Given each kernel version number, the loop extracts the package names from the installed kernel packages and glues the result onto a file that looks like this:
cat /tmp/pkgs.txt linux-headers-3.13.0-73 linux-headers-3.13.0-73-generic linux-image-3.13.0-73-generic linux-image-extra-3.13.0-73-generic linux-headers-3.13.0-74 linux-headers-3.13.0-74-generic linux-image-3.13.0-74-generic linux-image-extra-3.13.0-74-generic linux-headers-3.13.0-76 linux-headers-3.13.0-76-generic linux-image-3.13.0-76-generic linux-image-extra-3.13.0-76-generic
Convert that file into a one-line string of package names and verify what would happen:
paste -s -d " " /tmp/pkgs.txt | xargs sudo apt-get --dry-run purge
If everything looks good, change
--yes and blow ’em away.
No, I can’t possibly remember or type that gibberish by hand, but I do know where to find it…