Remove Old Kernels In Ubuntu With One Command
来源:互联网 发布:万能五笔有mac版吗 编辑:程序博客网 时间:2024/04/30 04:03
A while back I wrote a post on how to remove old kernels from your Ubuntu system. While that process works just fine, it is a four step process. One person who read that post left a comment with a nice command line one-liner that removes all but the currently running kernel. And whilethat one-liner works quite well, I must admit that I don't understand all the regular expressions used in it, so I decided to try and come up with my own one-liner to remove the old kernels from my system.
I'm going to take you through this step by step so you can see how the individual commands in this one-liner tie together. If you're impatient, you canskip to the end to see the final command.
Step 1) List all packages that start with "linux-"
We'll use the dpkg command with the -l switch to list the packages, whether installed or not, that start with the stringlinux-.
Step 2) Filter that list to show only installed packages
To filter the list, I'm going to pipeline the output of the first command into theawk command. I'm also going to useawk to filter out everything but the package names.
Step 3) Filter out packages for the currently running kernel
OK, so now I'm down to a pretty limited number of packages, but I don't want to remove the packages for my currently running kernel. I'm going to use a few commands to do that. First off, I can determine my currently running kernel with theuname -r command. Currently on my system that command outputs:2.6.32-25-generic.
To do my package filtering, I only want the numeric portion of that output. I'll pipeline the output ofuname -r and use thecut command with a hyphen as the field delimiter. I'll cut fields 1 & 2.
Now I'm going to use this result as the filter for a grep command. In Linux, to use the result of one command as an argument in another command, you enclose the command in single back-quotes ( ` that's the key to the left of the 1 on a standard US keyboard). So here's my one-liner so far.
This is the output so far on my system:
linux-generic
linux-headers-2.6.32-24
linux-headers-2.6.32-24-generic
linux-headers-generic
linux-image-2.6.32-24-generic
linux-image-generic
linux-libc-dev
linux-sound-base
Step 4) Filter the list for only the kernel packages
So now I have a package list that excludes the packages for my current kernel. The only packages from the list above that I want to remove are:linux-headers-2.6.32-24, linux-headers-2.6.32-24-generic, linux-image-2.6.32-24-generic.
What makes these packages unique from the others in the list is that they all contain numbers. So I can usegrep again to filter the list down to only packages with numbers in their names. I'll pipeline the output of the previous command intogrep -e [0-9].
So now the output on my system is only the following:
linux-headers-2.6.32-24-generic
linux-image-2.6.32-24-generic
Step 5) Make sure we don't catch any stray packages
Some people have had problems with this one-liner catching the linux-libc-dev:amd64 package. Adding a
to the string fixes the problem. Now we have
Step 6) Putting it all together: Removing the packages
So now that I have a good list of packages I can use another pipe and the xargs command to invoke apt-get to remove the packages. First I'm going to show it using the--dry-run switch withapt-get. That way you can give it a try without actually changing your system.
For aptitude, use
dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | grep -E "(image|headers)" | xargs sudo aptitude --simulate purge
If everything looks good after the dry run, you can go ahead and remove the old kernels with:
For aptitude, use
dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | grep -E "(image|headers)" | xargs sudo aptitude -y purge
So there you have it. One command, albeit a long one, to remove the old kernels from your Ubuntu system. I imagine the same command should work on other Debian based systems as well, but I've only tested this on my 32 bit system. I'd be interested to know if it works just as well on a 64 bit system.
Update: It works just fine on 64 bit systems as well.
http://tuxtweaks.com/2010/10/remove-old-kernels-in-ubuntu-with-one-command/
- Remove Old Kernels In Ubuntu With One Command
- remove multi package with one command
- Remove one known host in ubuntu
- Container no booting with old config in ubuntu 14.04
- One Line Command to Install Adobe Flash Player in Ubuntu
- Cleanup Unused Linux Kernels in Ubuntu
- Install Lamp With 1 Command In Ubuntu 12.10
- Building Ubuntu Kernels with Debug Symbols (by quqi99)
- Supervised Hashing with Kernels
- svn command in Ubuntu
- aptitude command in Ubuntu
- aptitude command in Ubuntu
- Useful Command in Ubuntu
- 第三章 In Command with Command Bars
- Install postgres in my old ubuntu system
- Out With the Old and in With the New
- one command to delete all files with name "filename" in current dir and all its sub-dirs
- structed_structured output tracking with kernels
- sql中having用法
- 使用Spring注解来注入属性
- mysql数据库备份脚本
- 如何成为兼具创意和说服力的好架构师?
- AE开发 遇到未能加载文件或程序集的问题 FileNotFoundException
- Remove Old Kernels In Ubuntu With One Command
- appStore上传苹果应用程序软件发布流程
- javascript学习笔记五:javascript中的回调函数
- libQtCore.so: undefined reference to `clock_gettime'
- poj2239(二分图)
- Android Application的作用
- CvMat Mat IplImage 之间相互转化
- smart210 的Android源码编译
- web知识笔记:select 传值与赋值