After the next update of Ubunt, I realized that I was completely tormented by the growing list of downloads in grub (this is especially true if the alternative OS remains somewhere at the bottom of the list). Manually deleting is somehow wrong, so I decided to bother about the automatic method.
I didn’t succeed in finding the right topic on Habré, because after rummaging through the Internet, I found a
recipe , the only drawback of which was the use of temporary files, which I didn’t like very much. Having made a couple of transformations, I got a slightly more simplified version (run through sudo).
Attention (UPD) : it is recommended not to use this code, but the code at the end of the post, since in some situations, it can delete the current kernel, it is left only to keep the topic in its original form.
#!/bin/bash
kernels=`dpkg-query -l linux-image-* | grep ^ii | grep -v eg | head -n -1 | gawk -F" " '{ print $2 }'`
for i in $kernels
do
echo "Removing kernel: "$i
# sudo apt-get -y remove $i
done
Since my knowledge of writing scripts under the shell can be considered null, I spontaneously commented out the line with deletion, in case I made a critical error somewhere :) Please ask the audience to express your opinion about the reliability of the code - and then I will remove the comment. In addition, it seems to me it is possible to request this code to one command (which, unfortunately, I did not succeed).
')
I hope someone will come in handy.
We kindly request : before the first launch of the command, run it without the tail "| xargs apt-get -y purge" and make sure that you get the correct list of cores for deletion.
UPD : Messie and
ValdikSS Habrayuzer prompted a more correct and one-line version:
dpkg -l linux-image-* | grep ii | grep -v [az]-generic | grep -v `uname -r` | awk '{ print $2 }' | xargs apt-get -y purge
UPD2 : Option from the ubucleaner script from
Steve_32 :
dpkg -l | grep ii | awk '{print $2}' | grep -E 'linux-(image|headers|ubuntu-modules|restricted-modules)' | grep -vE 'linux-(image|headers|restricted-modules)-(generic|i386|server|common|rt|xen)' | grep -v $(uname -r|sed 's/-*[az]//g'|sed 's/-386//g') | xargs apt-get -y purge