Find dependencies and reverse-dependencies for all packages at once

Ok, so you can use rdepends {packagename} (if you've got rdepends installed) or sudo apt-cache depends {packagename} and sudo apt-cache rdepends {packagename} to get the dependencies and reverse-dependencies of any given package.

But what if you want all of the dependencies and reverse-dependencies for all your packages without having to cut-n-paste the package name more than a thousand times?

for packages in $(sudo apt list --installed 2>/dev/null | awk -F '/' '{if (NR!=1) print $1}'); do sudo apt-cache depends $packages; sudo apt-cache rdepends $packages; done

Gives output like this:

accountsservice
  Depends: dbus
    dbus:i386
  Depends: libaccountsservice0
  Depends: libc6
  Depends: libglib2.0-0
  Depends: libpolkit-gobject-1-0
  Suggests: gnome-control-center
accountsservice
Reverse Depends:
  unity-settings-daemon
  xdg-desktop-portal-gtk
  mugshot
  malcontent
  gnome-control-center
  gdm3
  gdm3
  liblightdm-gobject-1-0
  xdg-desktop-portal-gtk
  libaccountsservice0
  language-selector-common
  gnome-control-center
  gdm3
  user-manager
  language-selector-common
  unity-control-center
  mugshot
  mate-polkit
  liblightdm-gobject-1-0
  indicator-sound
  cinnamon-control-center
  accountsservice-ubuntu-schemas
  xdg-desktop-portal-gtk
  libaccountsservice0
  gdm3
  gnome-control-center
ack
  Depends: libfile-next-perl
  Depends: <perl:any>
    perl:i386
    perl
  Breaks: <ack-grep>
  Replaces: <ack-grep>
ack
Reverse Depends:
 |libtask-kensho-cli-perl
 |myrepos
  elpa-counsel
  elpa-helm-ag

If you want it in a .txt file:
sudo su

for packages in $(sudo apt list --installed 2>/dev/null | awk -F '/' '{if (NR!=1) print $1}'); do sudo apt-cache depends $packages | tee -a /home/owner/Desktop/dep.txt; sudo apt-cache rdepends $packages | tee -a /home/owner/Desktop/dep.txt; echo '-------------------------' | tee -a /home/owner/Desktop/dep.txt; done

Change /home/owner/Desktop/ to the path to your file, and dep.txt to the name of the resultant file that you want.

It'll take awhile to run... the resultant file for my system had 201684 lines.

Instead of using the tee command, couldn't you redirect the output with either
> or >> (create or append) to whatever file name you think appropriate in any directory that you wish?

Quote is modified to include this.

If this loop was in a bash script, you could also redirect the output.... installed&dependencies.sh >> ~/Desktop/dep.txt

Two issues:

  1. I'm new to bash scripting. I didn't know about '>>'. :crazy_face: So when I used '>', I ended up with just the output of the last command.

  2. I wanted it to also output to the screen, rather than it just sitting at a blinking cursor until it was done.

I suppose it could be done like this:
file="/home/owner/Desktop/dep.txt"; for packages in $(sudo apt list --installed 2>/dev/null | awk -F '/' '{if (NR!=1) print $1}'); do echo Writing $packages; aptitude why $packages >> $file; sudo apt-cache depends $packages >> $file; sudo apt-cache rdepends $packages >> $file; echo '-------------------------' >> $file; done

sudo apt list reports WARNING: apt does not have a stable CLI interface. Use with caution in scripts. when you chain it. The 2>/dev/null part strips that out.

The sudo apt list --installed 2>/dev/null | awk -F '/' '{print $1}' prints a "Listing..." header that's interpreted by the subsequent code as a package... the if (NR!=1) part of awk strips that header out.

The above also includes aptitude why, which gives the chain of dependency.

For instance:

i   vlc             Depends    vlc-plugin-base (= 3.0.9.2-1)
i A vlc-plugin-base Suggests   libdvdcss2                   
p   libdvd-pkg      Provides   libdvdcss2                   
p   libdvd-pkg      Depends    wget | devscripts            
p   devscripts      Suggests   diffoscope                   
p   diffoscope      Recommends python3-guestfs              
p   python3-guestfs Depends    libguestfs0 (>= 1:1.39.8)    
p   libguestfs0     Depends    diffutils                    
diffutils
  PreDepends: libc6
  Suggests: diffutils-doc
  Suggests: wdiff
  Replaces: <diff>
diffutils
Reverse Depends:
  libguestfs0
  guestfsd
 |dms-core
  arcanist-clang-format-linter
1 Like