Skip to content

`pactree -r` but only show a partial tree if the last node is an explicitly installed package ? (Equivalent of `aptitude why`)

On Debian systems I very much like the command aptitude why <package> which lets me know exactly why a given package was installed on my system with regards to the commands I manually typed myself, by showing me a reverse dependency chain up to an explicitly installed package.

The best equivalent I found on Arch is pactree -r, which itself prints a full reverse dependency tree for the package.

It's cool, but you rapidly end up with a huge tree, and have to analyze the tree yourself to find packages you know you explicitly installed in order to get the same information as with aptitude why, which is:

"Why/how did this package get installed on my systemd?"

I read the full manpage for pactree in vain.

I was wondering if some of you had found a more efficient solution than I currently have.

Edited 12 Nov 2020. We only hold this version.

35 points12 comments
Open on Reddit
About this post
Posted
Archived
Edited
Score
35 · 93% up
Kind
Text
ID
jsvvif

Author

130
posts
1k
comments
4
media
View profile

12 comments

EDIT: /u/Moizac pointed out how to shorten a command.

You could try this command:

( pacman -Q --explicit --quiet; pactree --reverse --unique YOURPACKAGE ) | sort | uniq --repeated

It takes a list of all reverse dependencies of YOURPACKAGE and keeps only those which are explicitely installed.

​

In more detail:

pacman -Q --explicit --quiet

List all explicitely installed packages without version number.

pactree --reverse --unique YOURPACKAGE

List all reverse dependencies of the package without tree structure and don't repeat.

( … ) | sort | uniq --repeated

Put both command inside parentheses so they can be piped together into the sort command. sort and uniq is used to only show packages which are explicitely installed and a dependency.

EDIT:

You can go one step further to look inside /var/log/pacman.log for when the packages were installed:

grep "$( ( pacman -Q --explicit --quiet; pactree --reverse --unique YOURPACKAGE ) | sort | uniq --repeated | xargs echo | sed 's/ /\\\|/g;s/^\(.*\)$/ installed \\(\1\\) /' )" /var/log/pacman.log

​

EDIT2: Looking through the wiki I found that it already suggest the program whoneeds from pkgtools (AUR) which apparently does the same thing as my first suggested solution.

$ cat /usr/bin/whoneeds
#! /bin/bash

echo "Packages that depend on [$1]"
comm -12 <(pactree -ru $1|sort) <(pacman -Qqe|sort) | grep -v ^$1$ | sed 's/^/  /'
5 points

pacman -Q --explicit | awk '{print $1}'

This can be shortened to pacman -Qqe or pacman -Q --explicit --quiet

2 points

Thanks a lot! feel kinda dumb that I didn't think about comparing with Pacman -Qe, but your way of achieving the goal it is very smart. If I was alone, I believe I would have gone with a rather sizable script entangling everything when you've done it in a one-liner :D, thanks to that clever use of sort and uniq.

Then, the shortest way to type this becomes:

( pacman -Qqe ; pactree -rlu <package> ) | sort | uniq -d
1 points

Yep, also have a look at my second edit.

Furthermore I noticed that --unique implies --linear for pactree, so you can skip that, too.

1 points

i did't understood really well but you can just use pacman -Qi packagename to get which packages needs it

1 points

There is a package installed on their machine.

They know they didn't install it.

They want to know what package they installed (explicitly) either directly or indirectly lead to that package being installed.

For example: I didn't install Egg, but I installed Cake. Cake depends on Cake Batter and Cake Batter depends on Egg.

They want a way to determine that Cake was the installation responsible for installing Egg.

5 points
pacman -Qi "Egg"

See that Egg is required by Cake Batter.

pacman -Qi "Cake Batter"

See that Cake Batter is required by Cake. If you don't remember installing Cake explicitly, you can check that by running:

pacman -Qi "Cake"
0 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.