Ubuntu: Is this command to remove old kernels safe to use?


While browsing the internet for Ubuntu articles, I came across this command:

sudo dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge  

The author said that this is a single line command which will delete all the previous versions of Linux, leaving only the current one!

I'm actually looking for such a command, but I'm not so sure on how safe this is. I'd like to know:

  • Whether it is safe to execute this command?
  • How does this command work? i.e. explanation of small parts of such a big command
  • If this command serves some different purpose, then what would be the correct command to achieve what the author claims it to do?

I become very confused and frustrated when I try to infer out all by myself. How does this command work for it contains numerous /, |, \, *, and ^ characters which are hard to Google for.

I am looking for a step by step translation & explanation for this command which I was unable to find across the internet!


I'd say: don't use it in the current form

  1. It makes changes without asking you. The apt-get -y purge part allows the one-liner to start executing purging packages, without your confirmation. If any error in the script exists, then you could be screwed.

  2. No source, no author given. The source of it makes a difference here. In case it would come from a thoroughly tested system package we can trace the testing being done onto it. From a random source, we can't trust it.

  3. dpkg -l runs fine without sudo. I don't see why the original author thought this was necessary.

Use common sense

Remove the harmful parts and leave out anything that runs as root.

For example, cut it down to this:

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'  

which just only outputs stuff and runs with regular user permissions. Once you agree with these kernels to be removed, you can append | xargs sudo apt-get purge yourself. It's without the -y option, intentionally, so you'll be asked for confirmation on the changes about to be made to your system.


  • dpkg -l Outputs the list of all packages. In this case it will only list packages starting with linux- as the name.
  • | (a pipe) pipes the output of the command on the left (we call that stdout) to the input of the command on the right (we call that stdin).
  • sed is a tool to manipulate strings using regular expressions. In this case it manipulates the output of the command on the left of the pipe and filters for installed packages (relying on the ii as given by dpkg). It is even being nested in this case. It would be too hard to explain the whole use of sed here, as its use is very complicated with the complex regular expressions. (the \(.*\)-\([^0-9]\+\)" is an example of a regular expression.
  • Regular expressions are very widely used to find matches based on the expression they represent. \1 is the replacement reference to do a sort of universal search-and-replace (referencing to the first 'hit' with 1). The regular expression themselves can't do any harm. However, if they manipulate the input in a wrong way, they can have you remove the wrong packages or even do shell injection. In this case it looks like a way to find the name of the kernel package based on the version provided by uname -r.
  • uname -r outputs the current version of the kernel running.
  • xargs appends the lines of the input left of the pipe as arguments to the command. In this case, the kernel versions on each line are converted to a horizontal space-separated list and appended to the sudo apt-get command.
  • sudo apt-get -y purge [packagename] purges (removes everything) of the packages given (as arguments).


Quite some question are probably already asked about this. Relevant ones I found so far:


You asked for a step-by-step explanation so here goes:

sudo dpkg -l 'linux-*'  

Lists packages starting with linux- in the package name

| sed  

and pipe the list into sed

"s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'  

which will use a very complicated regular expression to edit the list

| xargs  

which will pipe the new list into xargs, which will send it as an argument to

sudo apt-get -y purge  

which will purge those packages without giving you a chance to change your mind.

Or perhaps it's more accurate to say it will send that list into the purge command and leave it at that. Whether or not anything is purged â€" and importantly â€" exactly what is purged depends on the ouput of the previous commands.

Is it safe? In this case that all depends on how well the author of the post where you found it understands regular expressions and sed syntax. And there are whole books on both of those topics.


I started by dissecting the commands, reading the man page for each.

  • dpkg -l: list pacakges, so dpkg -l linux-* would list all packages that started with linux- (usually kernels).

  • sed: The output of dpkg -l linux-* is piped to sed with several regular expressions which sed decodes.

  • uname -r unameprints system information

uname - print system information

The -r handle specifically prints kernel releases:

-r, --kernel-release print the kernel release

The output of uname -r is then piped to sed with more regular expressions, the output of which is passed to xargs

So xargs translates the sed output into package names and passes them onto sudo apt-get purge -y which automatically answers 'yes' to all prompts:

-y, --yes, --assume-yes Automatic yes to prompts; assume "yes" as answer to all prompts and run non-interactively. If an undesirable situation, such as changing a held package, trying to install a unauthenticated package or removing an essential package occurs then apt-get will abort. Configuration Item: APT::Get::Assume-Yes.

Altogether it seems this command will do what you want, though to know for sure we'd have to translate sed's regular expressions.

I just ran:

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'    

here is a screenshot:

enter image description here

All old kernel versions iirc.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »