30

Is there any command that prints only the name of the packages that apt-get autoremove selects? I'm creating a script that updates the kernel, removes the old kernel and the unnecessary packages (apt-get autoremove), but I want to print on the screen the list of packages that will be removed by apt-get autoremove, how can I do this?

kos
  • 35,535
  • 13
  • 101
  • 151
Afonso Sousa
  • 411
  • 1
  • 4
  • 4
  • You should just be able to get it to run `sudo apt-get autoremove -y` and it should autoremove anything needed to be removed... –  Jul 24 '15 at 20:33
  • Try reading here, [This may be of help.][1] [1]: http://serverfault.com/questions/433250/how-to-get-the-list-of-packages-present-in-apt-get-autoremove – Doug Jul 24 '15 at 20:35
  • To get the list of packages without removing them actually you can do `sudo apt-get --dry-run autoremove` – heemayl Jul 24 '15 at 20:36
  • 1
    I just want to get the name of the packages, not the entire output of the command.... – Afonso Sousa Jul 24 '15 at 20:53

4 Answers4

31

Since as per your comment you want to list only the packages that are going to be removed:

apt-get --dry-run autoremove | grep -Po '^Remv \K[^ ]+'

grep command breakdown:

  • -P: Interprets the given pattern as a PCRE (Perl Compatible Regular Expression) pattern
  • -o: Prints only the matched string instead of the whole line

Regex breakdown:

  • ^: matches the start of the line
  • Remv: matches a Remv string
  • \K: excludes the previously matched substring from the matched string
  • [^ ]+: matches one or more characters not
$ apt-get --dry-run autoremove | grep -Po 'Remv \K[^ ]+'
libapache2-mod-php5
php5-readline
php5-cli
libonig2
libqdbm14
php5-json
php5-common 
kos
  • 35,535
  • 13
  • 101
  • 151
5

Actually you only need to filter the output of your

sudo apt-get autoremove --dry-run 

command.

For instance you can do it with

sudo apt-get autoremove --dry-run  | head -n 5 | tail -n 1
A.B.
  • 89,123
  • 21
  • 245
  • 323
lemonslice
  • 576
  • 3
  • 11
2

No need for apt-get in modern apt versions. You can use

apt autoremove --dry-run
Abdull
  • 362
  • 2
  • 6
  • 13
0

Using apt-patterns (see man 7 apt-patterns) one can simply do:

apt list '?garbage'