Skip to main content

apt: List Package Dependencies and Files

Problem

On Debian based systems, apt acts as a wrapper around dpkg to manage dependencies and conflicts when installing and removing an application.

You want to know which packages are going to be installed by a particular package when using apt.

Solution

List Dependencies

apt-cache depends package_name | grep Depends | sort -u | cut -d ':' -f 2 | sed -E 's/^\ |^$//g'

List packages to be installed as dependencies of package_name

#! /usr/bin/env bash
function packageType() {
  package_name="$1"
  package_type=$(
    /usr/bin/apt-cache pkgnames "$package_name" >/dev/null && 
    /usr/bin/apt-cache show "$package_name" 2>/dev/null | 
    /usr/bin/grep --color Section | 
    cut -d ' ' -f 2
  )
  echo "$package_type"
}

function recursePackage() {
  package_name="$1"
  /usr/bin/apt-cache depends "$package_name" | 
  /usr/bin/grep Depends | 
  /usr/bin/sort -u | 
  /usr/bin/cut -d ':' -f 2 |
  /usr/bin/sed -e 's/^\ //g' -e 's/^$//g' -e 's/<//g' -e 's/>//g' |
  xargs -P 10 -I {} /usr/bin/env bash -c \
  'packageType {} | /usr/bin/grep metapackages >/dev/null && recursePackage "{}" || /usr/bin/echo "{}"'
}

export -f packageType
export -f recursePackage

function lspkgdepends {
  echo -n -e "\nEnter a package name: "
  read package_name
  echo "" # Line break
  recursePackage "$package_name" | /usr/bin/sort -u
}

lspkgdepends

Recurses down through all metapackages showing the entire dependency chain

List File Paths

If package_name is a metapackage, you'll need to query each dependency of the metapackage to see which programs will be installed to specific paths.

#! /usr/bin/env bash

function packageType() {
  package_name="$1"
  package_type=$(
    /usr/bin/apt-cache pkgnames "$package_name" >/dev/null && 
    /usr/bin/apt-cache show "$package_name" 2>/dev/null | 
    /usr/bin/grep --color Section | 
    cut -d ' ' -f 2
  )
  echo "$package_type"
}

function recursePackage() {
  package_name="$1"
  /usr/bin/apt-cache depends "$package_name" | 
  /usr/bin/grep Depends | 
  /usr/bin/sort -u | 
  /usr/bin/cut -d ':' -f 2 |
  /usr/bin/sed -e 's/^\ //g' -e 's/^$//g' -e 's/<//g' -e 's/>//g' |
  xargs -P 10 -I {} /usr/bin/env bash -c \
  'packageType {} | /usr/bin//grep metapackages >/dev/null && recursePackage "{}" || /usr/bin/echo "{}"'
}

# Export functions to be called by nested bash command in recursePacakge function
export -f packageType
export -f recursePackage

function lspkgfiles {
  echo -n -e "\nEnter a package name: "
  read package_name
  echo "" # Line break
  recursePackage "$package_name" | /usr/bin/sort -u | 
  /usr/bin/xargs -P 10 -I {} /usr/bin/env bash -c '/usr/bin/apt-file list {} | grep -v "Searching,"'
}

lspkgfiles

This script will return any file path installed by any package -- metapackage or otherwise