LinuxCommandLibrary

dpkg-query

Query installed Debian package information

TLDR

List all installed packages

$ dpkg-query [[-l|--list]]
copy

List installed packages matching a pattern
$ dpkg-query [[-l|--list]] '[libc6*]'
copy

List all files installed by a package
$ dpkg-query [[-L|--listfiles]] [libc6]
copy

Show information about a package
$ dpkg-query [[-s|--status]] [libc6]
copy

Search for packages that own files matching a pattern
$ dpkg-query [[-S|--search]] [/etc/ld.so.conf.d]
copy

SYNOPSIS

dpkg-query [option ...] [package-name-pattern ...]

Common usage patterns include:
dpkg-query -l [package-name-pattern ...]
dpkg-query -s package-name-pattern ...
dpkg-query -L package-name ...
dpkg-query -S filename-pattern ...
dpkg-query -W [package-name-pattern ...]
dpkg-query -f format

PARAMETERS

-l
    Lists all known packages, or those matching the pattern, with their status (e.g., installed, removed).

--list
    Long form of -l.

-s
    Displays detailed status information for the specified packages, including version, architecture, and description.

--status
    Long form of -s.

-L
    Lists the files installed by the specified package(s). Requires exact package names.

--listfiles
    Long form of -L.

-S
    Searches for packages that own files matching the given pattern(s). Useful for identifying which package provided a specific file.

--search
    Long form of -S.

-W
    Shows the package name and version for packages matching the pattern. Offers a more concise output than -l.

--show
    Long form of -W.

-f
    Specifies the output format for --show or --status using printf-like syntax and field names.

--showformat=
    Long form of -f. Allows custom output.

--searchformat=
    Similar to --showformat, but for --search output.

--admindir=
    Sets the administrative directory where dpkg's database files are stored. Defaults to /var/lib/dpkg.

--root=
    Sets the root directory for all operations. This can be used to query packages on a different system or chroot.

DESCRIPTION

dpkg-query is a command-line utility integral to the Debian package management system. It provides a robust interface for querying information about packages installed on a system managed by dpkg. Unlike dpkg itself, dpkg-query is primarily used for reading information from the package database, meaning it doesn't modify the system's package state. This makes it a safe and frequently used tool for scripts and users needing to ascertain package presence, retrieve version numbers, list files belonging to an installed package, or identify which package owns a specific file. It's an indispensable component for system administrators and developers working within Debian-based environments, offering detailed insights into the installed software landscape without the overhead of higher-level tools like apt.

CAVEATS

dpkg-query operates solely on the local dpkg database (typically found in /var/lib/dpkg). It does not consult APT's caches, nor does it interact with remote package repositories. Therefore, it cannot list packages that are available but not installed, or provide information about package dependencies unless those dependencies are explicitly listed in the package's control file data within the local database. Its output relies entirely on the integrity and currency of the local dpkg database.

PACKAGE STATUS CODES

When using -l or -s, dpkg-query displays status flags. The first three characters indicate the desired, current, and installed status respectively.

Desired State:
u = Unknown
i = Install
h = Hold
r = Remove/Purge

Current State:
n = Not installed
i = Installed
c = Config-files (only config files exist)
u = Unpacked
f = Half-configured
h = Half-installed
w = Work-queue (awaiting processing)

Error State:
R = Reinst-required (package broken, needs reinstallation)

EXIT STATUS

dpkg-query returns an exit status of 0 on success. An exit status of 1 indicates that no packages matched the specified patterns or some requested information could not be retrieved. A status of 2 signifies a fatal error, such as invalid command line options or an inability to access the dpkg database.

HISTORY

dpkg-query is a core utility within the dpkg package management system, which was originally created for Debian GNU/Linux in 1993 by Ian Murdock and others. As Debian's foundational package tool, dpkg and its sub-utilities like dpkg-query have continuously evolved. dpkg-query was designed to provide a robust and efficient way to read the package database without altering its state, a functionality crucial for both system scripts and interactive user queries. Its development has focused on enhancing output flexibility (e.g., with --showformat) and improving query performance, ensuring it remains a staple for package information retrieval in Debian and its derivatives.

SEE ALSO

dpkg(8), apt(8), apt-cache(8), apt-file(1), debsums(1)

Copied to clipboard