1. Overview
When managing packages on a Linux system, we may need to identify which packages are installed. This can be useful in performing, auditing, system maintenance, and ensuring specific package versions are installed. For package management, we can use apt a package manager tool for Debian-based distributions such as Ubuntu.
In this tutorial, we’ll explore getting a list of installed packages matching a pattern using apt.
2. Listing All Installed Packages
apt is a command-line tool that simplifies the process of managing software packages. To list all installed packages we’ll use the apt list command with the –installed option:
$ apt list --installed
Listing... Done
7zip/jammy,now 21.07+dfsg-4 amd64 [installed,automatic]
accountsservice-ubuntu-schemas/jammy,jammy,now 0.0.7+21.10.20210712-0ubuntu2 all [installed,automatic]
accountsservice/jammy-updates,jammy-security,now 22.07.5-2ubuntu1.5 amd64 [installed,automatic]
acl/jammy,now 2.3.1-1 amd64 [installed,automatic]
...
Here, we list all the installed packages on our system. Each package is listed with its name, distribution, version, architecture, and status.
3. Pattern Matching With grep
grep is a command line tool used to search for a specific pattern in a text. By piping the apt list –installed output to grep, we can filter the list of installed packages to match a pattern.
For example, let’s list all installed packages that contain the word server:
$ apt list --installed | grep server
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
cups-server-common/jammy-updates,jammy-updates,jammy-security,jammy-security,now 2.4.1op1-1ubuntu4.10 all [installed,automatic]
evolution-data-server-common/jammy-updates,jammy-updates,jammy-security,jammy-security,now 3.44.4-0ubuntu1.1 all [installed,automatic]
evolution-data-server/jammy-updates,jammy-security,now 3.44.4-0ubuntu1.1 amd64 [installed,automatic]
gpg-wks-server/jammy-updates,jammy-security,now 2.2.27-3ubuntu2.1 amd64 [installed,automatic]
...
In the example above, we use grep to filter the list of installed packages and display only those whose package names contain the word server.
3.1. Excluding Packages
The -v option enables us to exclude lines that match a given pattern by inverting the match. It’s useful when we want to exclude certain packages from our results.
To demonstrate, let’s list all installed packages excluding those that contain a specific pattern in their names:
$ apt list --installed | grep -v fonts
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Listing...
7zip/jammy,now 21.07+dfsg-4 amd64 [installed,automatic]
accountsservice-ubuntu-schemas/jammy,jammy,now 0.0.7+21.10.20210712-0ubuntu2 all [installed,automatic]
accountsservice/jammy-updates,jammy-security,now 22.07.5-2ubuntu1.5 amd64 [installed,automatic]
acl/jammy,now 2.3.1-1 amd64 [installed,automatic]
...
The above command lists all installed packages on our system except those that contain fonts in their names excluding all fonts-related packages.
3.2. Using Regular Expressions
Since grep supports regular expressions, we can use them to create more complex patterns to filter the installed packages.
For instance, let’s list all installed packages that begin with lib:
$ apt list --installed | grep '^lib'
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
liba52-0.7.4/jammy,now 0.7.4-20 amd64 [installed,automatic]
libaa1/jammy,now 1.4p5-50build1 amd64 [installed,automatic]
libaa1/jammy,now 1.4p5-50build1 i386 [installed,automatic]
libaacs0/jammy,now 0.11.1-1 amd64 [installed,automatic]
...
Above, we filter the list of installed packages and display packages whose names begin with lib. Here, ^ represents a regular expression symbol used to match the beginning of a line.
Additionally, we can also list all installed packages that start and end with a specific pattern:
$ apt list --installed | grep '^lib.*dev'
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
libc-dev-bin/jammy-updates,jammy-security,now 2.35-0ubuntu3.8 amd64 [installed,automatic]
libc-devtools/jammy-updates,jammy-security,now 2.35-0ubuntu3.8 amd64 [installed,automatic]
libc6-dev/jammy-updates,jammy-security,now 2.35-0ubuntu3.8 amd64 [installed,automatic]
libcrypt-dev/jammy,now 1:4.4.27-1 amd64 [installed,automatic]
...
In the above command, we use grep ‘^lib.*dev’ to list all installed packages that begin with lib and end with dev.
3.3. Matching Multiple Patterns
Sometimes we may need to match multiple patterns at once. We can use grep with the -E option or multiple grep commands. The -E option enables extended regular expressions which supports the use of the OR operator to combine multiple patterns.
To demonstrate, let’s list all installed packages that match either perl or network:
$ apt list --installed | grep -E 'perl|network'
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
glib-networking-common/jammy,jammy,now 2.72.0-1 all [installed,automatic]
glib-networking-services/jammy,now 2.72.0-1 amd64 [installed,automatic]
libalgorithm-diff-perl/jammy,jammy,now 1.201-1 all [installed,automatic]
libalgorithm-diff-xs-perl/jammy,now 0.04-6build3 amd64 [installed,automatic]
...
The command above filters the list of installed packages and only displays those whose names contain either perl or network.
Alternatively, we can use multiple grep commands piped together to search for multiple patterns:
$ apt list --installed | grep python | grep -E 'tools|dev'
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
libpython3-dev/jammy-updates,jammy-security,now 3.10.6-1~22.04 amd64 [installed,automatic]
libpython3.10-dev/jammy-updates,jammy-security,now 3.10.12-1~22.04.3 amd64 [installed,automatic]
python3-dev/jammy-updates,jammy-security,now 3.10.6-1~22.04 amd64 [installed,automatic]
python3-more-itertools/jammy,jammy,now 8.10.0-2 all [installed,automatic]
...
Here, we list all installed packages related to Python and have tools or dev in their names.
4. Advanced Pattern Matching With awk
awk is a command-line utility used to process and analyze text files or streams of data. Unlike grep, it can perform more complex operations making it suitable to perform advanced filtering tasks. We can use it to extract specific parts of each line of output.
To illustrate, let’s extract just the name of each installed package that matches a pattern:
$ apt list --installed | grep node | awk -F/ '{print $1}'
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
libnode-dev
libnode72
node-abab
node-abbrev
...
Here, we use grep to filter the list of installed packages to include only those that contain node in their names. We then use the awk command to extract and print the package names of each matched pattern.
5. Conclusion
In this article, we discussed how to get a list of installed packages matching a pattern. Firstly, we used apt combined with grep to filter the list of installed packages based on a specific pattern. Next, we utilized apt with the awk command to format the output of the matched pattern.