It's the same name, apt
is just a frontend to dpkg
. The issue here is that the commands don't do what you thing they do. As explained in man apt-cache
:
pkgnames [prefix]
This command prints the name of each package APT knows. The
optional argument is a prefix match to filter the name list. The
output is suitable for use in a shell tab complete function and the
output is generated extremely quickly. This command is best used
with the --generate option.
Note that a package which APT knows of is not necessarily available
to download, installable or installed, e.g. virtual packages are
also listed in the generated list.
So, apt-cache pkgnames
lists all packages available to the system, irrespective of whether those packages are installed or not.
If you want to list installed packages only, you could use dpkg -l
or dpkg-query -l
:
-l, --list [package-name-pattern...]
List packages matching given pattern. If no package-name-pattern
is given, list all packages in /var/lib/dpkg/status, excluding
the ones marked as not-installed (i.e. those which have been
previously purged).
This means that it will also list those packages that have been removed if their configuration files are still present, i.e. if they were not purged. The second column of the dpkg-query -l
output is the package status which can be any of
Package status:
n = Not-installed
c = Config-files
H = Half-installed
U = Unpacked
F = Half-configured
W = Triggers-awaiting
t = Triggers-pending
i = Installed
So, to find those packages that are currently installed, you will want to select those whose status is i
. To do that, simply grep
for lines whose 1st character is anything and whose second is i
:
dpkg-query -l | grep '^.i'
You can easily check that the two commands are different and that apt-cache
returns thousands more results than dpkg
. For example, on my system:
$ dpkg-query -l | grep '^.i' | wc -l
3938
$ apt-cache pkgnames | wc -l
39889