How to Use the Command 'dpkg-query' (with examples)
The dpkg-query
command is a powerful tool on Debian-based Linux systems used to display information about installed packages. It provides users with detailed insights into the packages currently installed on their system, the files associated with those packages, and much more. This command is particularly useful for system administrators and advanced users who need to manage and audit installed software efficiently. Let’s explore how to use dpkg-query
through various practical examples.
Use Case 1: List All Installed Packages
Code:
dpkg-query --list
Motivation:
One of the essential tasks for any system administrator or user managing software on a Linux system is to know what software packages are installed. Listing all installed packages helps users in taking inventory of software, identifying unused ones for cleanup, or checking for required software for troubleshooting purposes.
Explanation:
dpkg-query
: Invokes thedpkg-query
command.--list
: This option tellsdpkg-query
to list all installed packages on the system. It provides a wide overview of the packages in an organized manner.
Example Output:
The output typically includes columns for package name, version, and architecture, such as:
ii adduser 3.118 all add and remove users and groups
ii apt 2.0.6 amd64 commandline package manager
Use Case 2: List Installed Packages Matching a Pattern
Code:
dpkg-query --list 'libc6*'
Motivation:
When managing packages, sometimes the need arises to find specific packages that match a particular pattern. It might be a part of a regular maintenance check or when you’re trying to find a specific library or tool suite starting with ’libc6’. This feature allows users to precisely identify related packages among potentially thousands of installed packages.
Explanation:
dpkg-query
: The command being executed.--list
: Directs the command to list packages.'libc6*'
: This is a shell pattern provided as an argument. The pattern ’libc6*’ will match any package name starting with ’libc6’, allowing for a targeted search.
Example Output:
You would see something like:
ii libc6:amd64 2.31-0ubuntu9.9 amd64 GNU C Library: Shared libraries
Use Case 3: List All Files Installed by a Package
Code:
dpkg-query --listfiles libc6
Motivation:
Knowing every file that a package installs can be very useful for system maintenance and debugging purposes. For example, if a process or application is facing issues, you may wish to verify the integrity of package files or locate a specific configuration file belonging to a package without manually searching the filesystem.
Explanation:
dpkg-query
: Executes the query operation.--listfiles
: This argument specifies that the command should return a list of all files installed by the named package.libc6
: The package for which you want to list the installed files.
Example Output:
This will provide a path list like:
/.
/lib
/lib/x86_64-linux-gnu
/lib/x86_64-linux-gnu/libc-2.31.so
/usr/share/doc/libc6/changelog.Debian.gz
Use Case 4: Show Information About a Package
Code:
dpkg-query --status libc6
Motivation:
Sometimes detailed information about a particular package is required, such as version, status (installed, removed), architecture, and description. This use case is especially important when troubleshooting or verifying that you are running the correct version of a package in compliance with dependencies.
Explanation:
dpkg-query
: The command keyword.--status
: This option is used for displaying the status and other detailed information about the specified package.libc6
: The target package name you want information about.
Example Output:
The command provides an output with information similar to:
Package: libc6
Status: install ok installed
Priority: required
Section: libs
Installed-Size: 12656
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Version: 2.31-0ubuntu9.9
Use Case 5: Search for Packages That Own Files Matching a Pattern
Code:
dpkg-query --search /etc/ld.so.conf.d
Motivation:
There are situations when you might come across a file on your system and need to know which package it belongs to. This is particularly useful in troubleshooting configuration issues or when removing or reinstalling software to ensure specific files are not left orphaned on the system.
Explanation:
dpkg-query
: Invokes the query.--search
: An option that allows searching for packages that have installed files matching the provided pattern./etc/ld.so.conf.d
: The file path or pattern you want to search for within installed packages.
Example Output:
The command outputs the package name and the path for each match:
libc-bin: /etc/ld.so.conf.d/x86_64-linux-gnu.conf
Conclusion:
The dpkg-query
command is an essential utility for package management in Debian-based systems. Its rich functionality allows users to list packages, get detailed package information, and understand package file distributions. By mastering this command, you can efficiently manage software packages, streamline troubleshooting processes, and enhance system reliability.