How to List Installed Files from APT Packages and Track Package Dependencies in Debian/Ubuntu


3 views

To view all files installed by a specific package (whether currently installed or just candidate), use:

# For already installed packages
dpkg -L package_name

# For packages not yet installed
apt-get download package_name
dpkg -c package_name*.deb

For more detailed metadata including conffiles and checksums:

apt-file list package_name

To identify which package owns a specific file:

dpkg -S /path/to/file
# Or using faster alternatives:
apt-file search /path/to/file
dlocate /path/to/file

Scenario 1: Checking nginx installation impact before committing

apt-get download nginx-core
dpkg -c nginx-core*.deb | grep 'conf\|log\|cache'

Scenario 2: Debugging mysterious file ownership

dlocate /usr/lib/python3.8/site-packages/pip

For comprehensive package analysis:

# Show all package metadata
apt-cache show package_name

# Verify installed package integrity
debsums -s package_name

# Check reverse dependencies
apt-cache rdepends package_name

Create a pre-installation audit script:

#!/bin/bash
PKG=$1
TMPDIR=$(mktemp -d)
cd $TMPDIR
apt-get download $PKG 2>/dev/null || exit 1
dpkg -c $PKG*.deb
rm -rf $TMPDIR

When working with Debian-based systems, you can preview files that would be installed by a package using:

apt-get install --reinstall -d -o APT::Get::List-Cleanup=false <package-name>
dpkg -c /var/cache/apt/archives/<package-name>*.deb

For example, to examine the nginx package:

sudo apt-get install --reinstall -d -o APT::Get::List-Cleanup=false nginx
dpkg -c /var/cache/apt/archives/nginx_*.deb

To list files already installed by a package:

dpkg -L <package-name>

For a more detailed view including file metadata:

dpkg-query -L <package-name>

To identify which package installed a specific file:

dpkg -S /path/to/file

When you only know part of the filename:

apt-file search filename-pattern

First install apt-file if needed:

sudo apt-get install apt-file
sudo apt-file update

Case 1: Checking where Python3.8 executable came from:

dpkg -S $(which python3.8)

Case 2: Finding all configuration files from apache2:

dpkg -L apache2 | grep '/etc/'

Case 3: Previewing files in a not-yet-installed package:

apt-get download htop
dpkg -c htop_*.deb

Generate a complete file manifest for all installed packages:

sudo dpkg-query -W -f='${Package}\t${Version}\n' | \
while read pkg ver; do \
echo "$pkg ($ver)"; \
dpkg -L $pkg | while read file; do \
echo " $file"; \
done; \
done > package_manifest.txt

Create a script to monitor new file installations:

#!/bin/bash
# Monitor package installations and log file changes
PKG=$1
LOG_FILE="/var/log/pkg_${PKG}_install.log"

echo "Tracking files for ${PKG}..." > $LOG_FILE
apt-get install --reinstall -d -o APT::Get::List-Cleanup=false $PKG
dpkg -c /var/cache/apt/archives/${PKG}_*.deb >> $LOG_FILE
apt-get install $PKG
echo "Installation complete. File list saved to ${LOG_FILE}"