Last active
August 29, 2015 14:20
-
-
Save leahcim/d124105402a507b47780 to your computer and use it in GitHub Desktop.
Ubuntu: remove old kernels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## TL;DR | |
# Simulate purging all kernels apart from the two most recent ones: | |
dpkg-query -Wf'${binary:Package}\n' 'linux*-3.*.*-*' | sort -t. -k2 | head -n-8 | xargs apt-get -s purge | |
# Purge all kernels apart from the two most recent ones: | |
dpkg-query -Wf'${binary:Package}\n' 'linux*-3.*.*-*' | sort -t. -k2 | head -n-8 | xargs sudo apt-get -y purge | |
## Explanation | |
# dpkg -W = short for `dpkg --show` (see below) | |
# dpkg -f'FORMAT' = short for `dpkg --show-format='FORMAT'` (see below) | |
# sort -t. = use '.' as field separator instead of ' ' | |
# sort -k2 = sort starting from field 2 (counted from 1) and stopping at end of line (default) | |
# head -n-8 = each kernel consists of 4 binary packages - show all but the last 2*4 packages | |
# xargs = take standard input and pass to the end of the command that follows | |
## Other Related Commands | |
# List installed kernel images (here, of series 3.x.y): | |
# (show only relevant package names and nothing else) | |
dpkg-query --show --showformat='${binary:Package}\n' 'linux-image-3.*-generic' | |
# or shorter, using flags: | |
dpkg-query -Wf'${binary:Package}\n' 'linux-image-3.*-generic' | |
# or using default output format (package name + version): | |
dpkg-query -W 'linux-image-3.*-generic' | |
# or for a human-readable output (a terminal window sized table): | |
dpkg -l 'linux-image-3.*-generic' | |
# Example output from the first two commands above: | |
# linux-image-3.13.0-46-generic | |
# linux-image-3.13.0-48-generic | |
# linux-image-3.13.0-49-generic | |
# linux-image-3.13.0-51-generic | |
# linux-image-3.13.0-52-generic | |
# Simulate purging all kernels apart from the two arbitrary ones (51, 52) noted from above: | |
dpkg-query -Wf'${binary:Package}\n' 'linux*-3.*.*-*' | grep -Ev '51|52' | xargs apt-get -s purge | |
# Purge all kernels apart from the two arbitrary ones (51, 52) noted from above: | |
# (commented out, as potentially destructive... clearly!) | |
# dpkg-query -Wf'${binary:Package}\n' 'linux*-3.*.*-*' | grep -Ev '51|52' | xargs sudo apt-get -y purge | |
# Explanation | |
# grep -E REGEX_PATTERN = same as `egrep REGEX_PATTERN` | |
# grep -v PATTERN = an inversion of `grep PATTERN` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment