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

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?

Asked By: Afonso Sousa


Actually you only need to filter the output of your

sudo apt-get autoremove --dry-run 


For instance you can do it with

sudo apt-get autoremove --dry-run  | head -n 5 | tail -n 1
Answered By: lemonslice

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[^ ]+'
Answered By: kos

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

apt autoremove --dry-run
Answered By: Abdull

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

apt list '?garbage'