Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The following is what I found after googling:

dpkg --list 'linux-image-*' \
    | perl -ane 'BEGIN { $r = `uname -r` or die; chomp $r } print $F[1], "\n" if $F[0] eq "ii" && $F[1] !~ /\Q$r\E\b/' \
    | xargs -r aptitude purge -y

Could someone combine the above 3 lines into one? I find it easier to copy and paste a single line into a sudo command than three lines, one at a time.

Thanks in advance for your help.

For Hauke Laging: I have edited this original post to include the error message when I tried to copy the three split lines at once following the sudo command:

username@hostname:~$ sudo -i
[sudo] password for username: 
root@hostname:~# dpkg --list 'linux-image-*' \
> 
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version      Architecture Description
+++-==============-============-============-=================================
un  linux-image-2. <none>                    (no description available)
un  linux-image-2. <none>                    (no description available)
un  linux-image-2. <none>                    (no description available)
un  linux-image-2. <none>                    (no description available)
ii  linux-image-3. 3.14.4-1~bpo amd64        Linux 3.14 for 64-bit PCs
ii  linux-image-3. 3.2.57-3+deb amd64        Linux 3.2 for 64-bit PCs
ii  linux-image-am 3.14+57~bpo7 amd64        Linux for 64-bit PCs (meta-packag
root@hostname:~#     | perl -ane 'BEGIN { $r = `uname -r` or die; chomp $r } print $F[1], "\n" if $F[0] eq "ii" && $F[1] !~ /\Q$r\E\b/' \
-bash: syntax error near unexpected token `|'
root@hostname:~# 
root@hostname:~#     | xargs -r aptitude purge -y
share|improve this question
6  
Only the xargs above needs to run under sudo. –  Stéphane Chazelas May 29 at 15:16
2  
sudo aptitude purge -y "~i ^linux-image ! -$(uname -r)\$" –  Stéphane Chazelas May 29 at 18:47

3 Answers 3

up vote 1 down vote accepted

You can create a function too (see below).

You can put it in a file, e.g. ~/MyPersonalFunctions, in which you will add with time all the functions you will need to use.

You can load it only when you need with the command source ~/MyPersonalFunctions and you will have your extended set of functions.
You can even load it each time automatically writing source ~/MyPersonalFunctions in the file ~/.bashrc.

Note: You have to write the full path where you put this file. E.g if you put it in your ~/bin directory you will write source ~/bin/MyPersonalFunctions).

function PurgeKernels()
{
dpkg --list 'linux-image-*' \
    | perl -ane 'BEGIN { $r = `uname -r` or die; chomp $r } print $F[1], "\n" if $F[0] eq "ii" && $F[1] !~ /\Q$r\E\b/' \
    | xargs -r sudo aptitude purge -y
}

function Cat(){  # Just to show you can put in the same file 
  echo Meow      # more than one function... :-)
}

Of course you can use it with the shorter version of the code proposed by the other answer in this page.

share|improve this answer

There is no need to make one line out of those three. The shell removes the newline if it is escaped by \. In other words: A command with arguments over three lines of that kind "sees" only one line, not three.

The next problem is:

sudo cmd1 | cmd2 | cmd3

causes only cmd1 to run as root. You could do something like

sudo bash -c 'cmd1 | cmd2 | cmd3'

but that may cause quoting annoyances.

The probably easiest solution is: Create an empty file, copy the three lines into it and then run it with:

sudo bash file

use | for safe line breaks

You can put the | at the end of a line. That way there can be no problems with (additional) newlines. Try this:

dpkg --list 'linux-image-*' |
  perl -ane 'BEGIN { $r = `uname -r` or die; chomp $r } print $F[1], "\n" if $F[0] eq "ii" && $F[1] !~ /\Q$r\E\b/' |
  sudo xargs -r aptitude purge -y
share|improve this answer
2  
Also sudo sh << \E, paste and enter E. –  Stéphane Chazelas May 29 at 15:40
    
@Huake Laging I know that a command with arguments over three lines of that kind "sees" only one line. The problem I have is to have to copy three lines, one at a time, into the sudo command. –  user66229 May 29 at 16:58
    
@Patrick If you have the time, please help me out on this one. Thanks in advance. –  user66229 May 29 at 16:58
    
@user66229 I don't know what you're talking about: I can easily copy three lines at once. –  Hauke Laging May 29 at 18:16
2  
@user66229 That looks strange to me. The second line in the shell is > instead of > | perl -ane .... Thus it seems that there are two newlines between the commands, not just one. If you paste that into a file: Are there empty lines between the commands then? But this problem can be avoided by moving the |. I will add that to my answer. –  Hauke Laging May 30 at 12:40

Backslash-newline is a way to split lines in a place where the syntax would not normally allow a line break, so you could just remove that.

dpkg --list 'linux-image-*' | perl -ane 'BEGIN { $r = `uname -r` or die; chomp $r } print $F[1], "\n" if $F[0] eq "ii" && $F[1] !~ /\Q$r\E\b/' | xargs -r aptitude purge -y

If you want to run this under sudo, you can't just copy-paste that line after sudo, because only the dpkg command would then run as root. In fact, only aptitude needs to run as root.

dpkg --list 'linux-image-*' | perl -ane 'BEGIN { $r = `uname -r` or die; chomp $r } print $F[1], "\n" if $F[0] eq "ii" && $F[1] !~ /\Q$r\E\b/' | xargs -r sudo aptitude purge -y

This command is overly complex for what it does. The perl script filters some installed packages, but aptitude is perfectly capable of doing that filtering.

sudo aptitude purge -y "~i ^linux-image !?exact-name(linux-image-$(uname -r))"
share|improve this answer
    
Thanks for your response. I don't know bash scripting at all and am new to Linux commands. Using your expertise, could you modify the 3 split lines, combine them into a single one and make it usable under the sudo command, ensuring that the single line gives the exact same result as the original 3 split lines? Thanks in advance. –  user66229 May 30 at 4:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.