Remove specific versions of old kernels (Ubuntu/Debian)

apt purge linux*{14..18}*
If, for example, you want to remove all kernels and headers but the last three versions, you can't use one of that magic all-in-one "remove old stuff" commands. With this simple but elegant command you can remove a range of versions, or a list of versions with e.g. {14,16,20}.
Sample Output
# apt purge linux*{14..18}*
Paketlisten werden gelesen... Fertig
Abh?ngigkeitsbaum wird aufgebaut.       
Statusinformationen werden eingelesen.... Fertig
Note, selecting 'linux-headers-4.4.0-14-generic' for glob 'linux*14*'
Note, selecting 'linux-headers-4.4.0-14' for glob 'linux*14*'
Note, selecting 'linux-signed-image-4.4.0-14-generic' for glob 'linux*14*'
Note, selecting 'linux-image-4.4.0-14-generic' for glob 'linux*14*'
Note, selecting 'linux-image-extra-4.4.0-14-generic' for glob 'linux*14*'
Note, selecting 'linux-wlan-ng-0.1.15-modules' for glob 'linux*15*'
Note, selecting 'linux-headers-4.4.0-15-generic' for glob 'linux*15*'
Note, selecting 'linux-headers-4.4.0-15' for glob 'linux*15*'
Note, selecting 'linux-signed-image-4.4.0-15-generic' for glob 'linux*15*'
Note, selecting 'linux-image-4.4.0-15-generic' for glob 'linux*15*'
Note, selecting 'linux-image-extra-4.4.0-15-generic' for glob 'linux*15*'
Paket ?linux-wlan-ng-0.1.15-modules? ist nicht installiert, wird also auch nicht entfernt.
Note, selecting 'linux-image-extra-4.4.0-16-generic' for glob 'linux*16*'
Note, selecting 'linux-headers-4.4.0-16-generic' for glob 'linux*16*'
Note, selecting 'linux-headers-4.4.0-16' for glob 'linux*16*'
Note, selecting 'linux-signed-image-4.4.0-16-generic' for glob 'linux*16*'
Note, selecting 'linux-image-4.4.0-16-generic' for glob 'linux*16*'
Note, selecting 'linux-image-4.4.0-17-generic' for glob 'linux*17*'
Note, selecting 'linux-image-extra-4.4.0-17-generic' for glob 'linux*17*'
Note, selecting 'linux-headers-4.4.0-17-generic' for glob 'linux*17*'
Note, selecting 'linux-headers-4.4.0-17' for glob 'linux*17*'
Note, selecting 'linux-signed-image-4.4.0-17-generic' for glob 'linux*17*'
Note, selecting 'linux-tools-4.4.0-18' for glob 'linux*18*'
Note, selecting 'linux-signed-image-4.4.0-18-lowlatency' for glob 'linux*18*'
Note, selecting 'linux-image-extra-4.4.0-18-generic' for glob 'linux*18*'
Note, selecting 'linux-cloud-tools-4.4.0-18' for glob 'linux*18*'
Note, selecting 'linux-tools-4.4.0-18-lowlatency' for glob 'linux*18*'
Note, selecting 'linux-headers-4.4.0-18-generic' for glob 'linux*18*'
Note, selecting 'linux-tools-4.4.0-18-generic' for glob 'linux*18*'
Note, selecting 'linux-headers-4.4.0-18' for glob 'linux*18*'
Note, selecting 'linux-cloud-tools-4.4.0-18-generic' for glob 'linux*18*'
Note, selecting 'linux-image-4.4.0-18-lowlatency' for glob 'linux*18*'
Note, selecting 'linux-cloud-tools-4.4.0-18-lowlatency' for glob 'linux*18*'
Note, selecting 'linux-headers-4.4.0-18-lowlatency' for glob 'linux*18*'
Note, selecting 'linux-signed-image-4.4.0-18-generic' for glob 'linux*18*'
Note, selecting 'linux-image-4.4.0-18-generic' for glob 'linux*18*'
Paket ?linux-cloud-tools-4.4.0-18? ist nicht installiert, wird also auch nicht entfernt.
Paket ?linux-cloud-tools-4.4.0-18-generic? ist nicht installiert, wird also auch nicht entfernt.
Paket ?linux-cloud-tools-4.4.0-18-lowlatency? ist nicht installiert, wird also auch nicht entfernt.
Paket ?linux-headers-4.4.0-18-lowlatency? ist nicht installiert, wird also auch nicht entfernt.
Paket ?linux-image-4.4.0-18-lowlatency? ist nicht installiert, wird also auch nicht entfernt.
Paket ?linux-signed-image-4.4.0-18-lowlatency? ist nicht installiert, wird also auch nicht entfernt.
Paket ?linux-tools-4.4.0-18? ist nicht installiert, wird also auch nicht entfernt.
Paket ?linux-tools-4.4.0-18-generic? ist nicht installiert, wird also auch nicht entfernt.
Paket ?linux-tools-4.4.0-18-lowlatency? ist nicht installiert, wird also auch nicht entfernt.
Die folgenden Pakete werden ENTFERNT:
  linux-headers-4.4.0-14* linux-headers-4.4.0-14-generic* linux-headers-4.4.0-15* linux-headers-4.4.0-15-generic* linux-headers-4.4.0-16* linux-headers-4.4.0-16-generic* linux-headers-4.4.0-17* linux-headers-4.4.0-17-generic*
  linux-headers-4.4.0-18* linux-headers-4.4.0-18-generic* linux-image-4.4.0-14-generic* linux-image-4.4.0-15-generic* linux-image-4.4.0-16-generic* linux-image-4.4.0-17-generic* linux-image-4.4.0-18-generic*
  linux-image-extra-4.4.0-14-generic* linux-image-extra-4.4.0-15-generic* linux-image-extra-4.4.0-16-generic* linux-image-extra-4.4.0-17-generic* linux-image-extra-4.4.0-18-generic* linux-signed-image-4.4.0-14-generic*
  linux-signed-image-4.4.0-15-generic* linux-signed-image-4.4.0-16-generic* linux-signed-image-4.4.0-17-generic* linux-signed-image-4.4.0-18-generic*
0 aktualisiert, 0 neu installiert, 25 zu entfernen und 0 nicht aktualisiert.
Nach dieser Operation werden 1.473 MB Plattenplatz freigegeben.
M?chten Sie fortfahren? [J/n] 

1
By: ppq
2016-04-20 07:44:55

These Might Interest You

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: