Ubuntu has been used for a long time. After several kernel upgrades, it will inevitably need to clean up the old kernel. After all, a set of kernel will have more than 170 MB of space. However, it is also annoying to write a pile of words each time to clean up. Although Ubuntu command skills (here I would like to say a few more: If Ubuntu er hasn't read this yet, I strongly recommend you read it again .) There is a command to delete the old kernel, and there is a line: sudoaptitudepurge ~ Ilinux-image -.*\(\! 'Uname-R' \) but I am not
Ubuntu has been used for a long time. After several kernel upgrades, it will inevitably need to clean up the old kernel. After all, a set of kernel will have more than 170 MB of space. However, it is also annoying to write a pile of words each time to clean up. Although Ubuntu command skills (here I would like to say a few more: If Ubuntu er hasn't read this yet, I strongly recommend you read it again .) There is a command to delete the old kernel, and just one line:
sudo aptitude purge ~ilinux-image-.*\(\!`uname -r`\)
But I do not like it very much, because this does not delete linux-headers-xxx.
As a result, I wrote a complex one, and I can save what I need so that I don't need it.
#! /Bin/sh# Clearing old ubuntu kernels# By bones7456# Http://li2z.cnCURRENT="'Uname-r | awk-F "-" '{print $1 "-" $2 }''"HEADERS=""IMAGES=""ForHEADERIn `Dpkg -- Get-selections | Grep^ Linux-headers|\Grep -VE "(Generic | 386 | virtual )" | Awk '{Gsub (/linux-headers-/, "", $1); print $1 }'`Do If [[ "$ CURRENT" < "$ HEADER" ]] Then Echo "The running kernel is not up-to-date.$ CURRENT<$ HEADER" Echo "Running kernel is not the newest.$ CURRENT<$ HEADER" Exit 1 Else [[ "$ CURRENT" !="$ HEADER" ]] && { HEADERS="$ {HEADERS}Linux-headers-$ {HEADER}" IMAGE="'Dpkg -- get-selections | grep ^ linux-image | \ grep "$ {HEADER}" | awk '{print $1 }''" IMAGES="$ {IMAGES} $ IMAGE" } FiDone If [[X"$ HEADERS"= X"" ]]Then Echo "No old kernel to be cleaned up ." Echo "No old kernel need to clean ." Exit 0FiCMD="Sudo apt-get purge$ HEADERS $ IMAGES"Echo "$ CMD"If [ "$1"="-E" ]Then Sh -C "$ CMD"Else Echo "Check whether the preceding commands are correct and enter $0-e to execute the preceding commands. " Echo "Be sure this command is right, then type $0-e to execute ."Fi