Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've been trying to figure out package management in debian a little better.

I figured out that you can list all the packages on your system by using the "apt-cache pkgnames" command:

root@t500:/etc# apt-cache pkgnames | less
aspell-ca
libglobus-rls-client5
rkhunter
libmpich-shmem1.0gf
meep
libspring-webmvc-portlet-2.5-java
libindicate-doc
libjetty-extra-java
python-decoratortools
tree-ppuzzle
darkice
cutter-testing-framework-bin
aspell-bg
libsmpeg0
libpng12-dev

However, when I try to look up additional information about the packages in dpkg, the same package names are not listed:

root@t500:/etc# dpkg-query -W meep
No packages found matching meep.

root@t500:/etc# dpkg-query -W darkice
No packages found matching darkice.

I had assumed that apt and dpkg they would at least utilize the same package names, is that not the case?

Is there a way of figuring out what the dpkg name of an apt package is?

share|improve this question
    
Just one comment, do not use root all the time... –  Braiam Dec 2 '13 at 21:29
add comment

2 Answers 2

up vote 3 down vote accepted

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
share|improve this answer
    
Super helpful, thanks. Your pkgnames comes up with nearly 40k results, while mine only comes up with around 28k. Would that be due to differences in our sources.list files? –  some1 Dec 2 '13 at 22:00
1  
@some1 yes, exactly. Different sources mean different packages. –  terdon Dec 2 '13 at 22:03
add comment

The problem is that you need to establish a 'pattern' (with quotes) instead of just writing the name:

$ dpkg-query -W 'samba'
samba   
$ dpkg-query -W 'samba*'
samba   
samba-client    
samba-common    2:3.6.9-1ubuntu1.1
samba-common-bin    2:3.6.9-1ubuntu1.1
samba-tools 
samba4-clients  
samba4-common   

That's why no package appear:

$ dpkg-query -W 'rkhunter' 
rkhunter    1.4.0-2

Other things that may work:

dpkg-query -W '*a'
dpkg-query -W 'a*b*'
dpkg-query -W '?a*'

etc.

Normal shell wildchars are allowed in package-name-pattern. Please note you will probably have to quote package-name-pattern to prevent the shell from performing filename expansion.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.