How can I get a list with the packages selected by apt-get autoremove?

Solution 1:

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 

Solution 2:

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