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
Active
Viewed 1.5k times
34
kos
- 41,378
Afonso Sousa
- 451
4 Answers
34
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 lineRemv: matches aRemvstring\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
-
2
-
1@jarno Makes sense, the fact that it was probably not needed didn't occur to me. Thanks – kos Dec 23 '15 at 10:41
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.
- 92,275
lemonslice
- 576
-
1Your command works too!! But I prefer the kos's command... But thanks in same!! :) – Afonso Sousa Jul 25 '15 at 14:23
3
No need for apt-get in modern apt versions. You can use
apt autoremove --dry-run
Abdull
- 734
-
Thats necro-bumping. At the time of the question you had to use apt-get ;-) – kanehekili Oct 22 '22 at 21:23
3
Using apt-patterns (see man 7 apt-patterns) one can simply do:
apt list '?garbage'
-
Nice pattern I didn't know ;-) To fit better the question the answer should be
apt list '?garbage' 2>/dev/null | grep -oP '^(.*)(?=/)'– NetVicious Aug 14 '24 at 11:06
sudo apt-get autoremove -yand it should autoremove anything needed to be removed... – Jul 24 '15 at 20:33sudo apt-get --dry-run autoremove– heemayl Jul 24 '15 at 20:36