9

First, an introduction. I just found a development server with all kinds of GUI packages installed. I'd like to know why. Therefore, I'd like to know which software has been installed that requires X.

I can answer this by answering two closely related questions:

  • How can I list all installed packages that no other package depends on?
  • How can I list all installed packages that no other package depends on, and that, directly or indirectly, depend on a given package? (E.g., x11-common.)

For the first question, apt-mark showmanual is a useful approximation, but it may not be exactly right.

For the second question, what I'm using now postprocesses the apt-rdepends output to list only results for which no dependencies are listed that are listed as results.

Is this correct? Is there an easier way? I notice the result contains quite a few packages that aren't marked as manually installed.

I need this on Ubuntu 14.04, 16.04, and 18.04.

Reinier Post
  • 431
  • 4
  • 15
  • Why not look at `/var/log/apt`? You should be able to see the install history there. – waltinator Feb 01 '19 at 14:13
  • I don't want to pick logfiles apart by hand, I want those packages listed. Besides, by default, those logs don't last forever. – Reinier Post Feb 01 '19 at 14:15
  • I'm not sure that "all installed packages that no other package depends on" is going to be a very useful list (it's going to include essential "top level" packages such as `login` for example) but I guess you could parse the output of `aptitude why` e.g. `dpkg --get-selections | while read pkg status; do [[ "$status" == "install" ]] && { aptitude why "$pkg" | grep '^Unable to find a reason'; }; done` . It will be painfully slow (but you might not care, for a one-off). – steeldriver Feb 01 '19 at 16:26
  • Just an idea: Automatically installed packages that no other packages depends on, are removed as unused packages by aptitude, provided the options `APT::Install-Recommends` and `APT::AutoRemove::RecommendsImportant` are set to false. Or do you also want the manually installed packages that are not depended upon by any other package? – Stefan Hamcke Feb 01 '19 at 16:50
  • Basically, I want all software that was explicitly installed, rather than as a dependency of something else, even if that something else was installed later. I'm not sure just taking all packages resulting from `apt-mark showmanual` gives me that. – Reinier Post Apr 11 '19 at 13:35
  • like this ? https://askubuntu.com/questions/2389/generating-list-of-manually-installed-packages-and-querying-individual-packages – tatsu Apr 11 '19 at 13:57
  • Mmmm ... the original question is much the same, but the answer goes in a different direction, taking the `apt-mark show-manual` results and filtering out all initially installed packages. That's not what I want to do. – Reinier Post Apr 11 '19 at 14:01

1 Answers1

3

You can for example list all installed packages with dpkg-query and then pipe the package names to apt-cache to see whether they have any reverse depedencies. Here's a one way to do it:

#!/bin/sh

dpkg-query --show --showformat='${Package}\t${Status}\n' | sed --quiet '
    /installed$/{
        s/^/apt-cache rdepends --installed /
        e
        s/\n/ /g                                                        
        /:$/{                                                               
            s/\s.*// 
            p                                   
        }    
    }               
'

if you want to do the reverse and list packages with at least one reverse dependency, just change /:$/ to /:$/!

  • 3
    Very nice. The repeated calls to `apt-cache` make it slow though - I've amended it to `dpkg-query --show --showformat='${Package}\t${Status}\n' | tac | awk '/installed$/ {print $1}' | xargs apt-cache rdepends --installed | tac | awk '{ if (/^ /) ++deps; else if (!/:$/) { if (!deps) print; deps = 0 } }' ` – Reinier Post Mar 12 '21 at 20:12
  • 1
    Great job, that's way faster than the version in my answer; I forgot that `apt-cache` can accept multiple arguments. – Fritjof Larsson Mar 12 '21 at 20:33
  • I learnt some `sed` along the way - I never knew it has an `e` command ... – Reinier Post Mar 12 '21 at 20:44
  • 1
    As far as I know only the GNU version supports it. – Fritjof Larsson Mar 12 '21 at 21:04