I have a list of packages installed via apt and would like to eliminate all packages that are dependencies of another package already in that list:


those are over 400 packages but most of them are installed due to some others in that list.

How can I minify that list and stick to the main real interesting programs from that list?

Somehow I have to use this: How to find the packages that depend on a certain package in apt?

Or is there an easier way to distinguish between interesting programs and small helper packages that are installed with the main programs?

  • How do you want to determine whether package1 depends on package2? Is this based on your system's APT database? By querying packages.ubuntu.com? Something else? Commented Nov 10, 2014 at 23:06
  • Maybe I could create a loop over each line of that list and query all its dependencies, then delete those packets from the list itself and iterate this all over again
    – rubo77
    Commented Nov 10, 2014 at 23:09
  • There are two solutions, and both can be used. 1) Remove the already installed packages comparing both lists using diff or any other method to compare files. 2) Using aptitude/dselect and mark all packages to install and append the auto-installed flag, then check which packages will be "automatically removed" and there you have your list of the top of dependencies. There is a tool that can generate software dependencies graph, but I don't remember the name or that it would be useful for this particular case.
    – Braiam
    Commented Nov 10, 2014 at 23:20

3 Answers 3


The following code prints the list of packages that some package in the list $package_name depends on. You can pass input from dpkg -s to use data from the list of installed packages, or from apt-cache show to use data from the list of available packages. This code skips all or-dependencies (PACKAGE1 | PACKAGE2), because determining which one to pull in would be a lot more complicated and that probably doesn't make much difference in practice.

dpkg -s $package_names | awk -F '[:,] *' '
    $1=="Depends" || $1=="Recommends" {
        for (i=2; i<=NF; i++) {
            if ($i ~ /\|/) continue;
            sub(/ .*/, "", $i);
            print $i

If you want to filter a Packages file, you can use the following awk snippet:

<Packages awk -v RS= -v packages="$package_names" '
    BEGIN {split(packages, pa)}
    $2 in pa
' | awk -F '[:,] *' '…'

Now, assuming you have a text file containing a list of packages, you can remove packages that are a dependency of another package like this:

comm -23 <(sort packages.txt) <(apt-cache show $(cat packages.txt) |
                                awk -F '[:,] *' ' '…' |

This outputs the packages that are in the input list but are not found as dependencies.


apt-get as well aptitude keep track of which packages were installed automatically via dependencies. At least in aptitude you can list all packages not installed via dependencies with aptitude search '!~M'.

Unfortunately this does usually not count for packages installed at system installation time. Also apt-get install removes the "automatically installed" flag for any package you pass as parameter to it as it thinks you want to have that package explicitly installed even if you only want to upgrade it that way.

See my blog posting about finding libraries not marked as automatically installed with aptitude for details on how to figure out on which packages the "automatically installed" flag is missing.


You could use apt-cache depends package (for dependencies) or apt-cache rdepends package (for reverse dependencies).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .