13

I see that dpkg has a "Provides" field for packages.

$ apt-cache show vim-tiny | grep Provides
Provides: editor
$

How do I know which packages provide i.e. "editor"?

Janus Troelsen
  • 1,114
  • 1
  • 8
  • 18

5 Answers5

10

Aptitude provides this functionality as well. So a command like this will show all the packages that provide an editor.

aptitude search '~Peditor'

You can even add other constraints. Like show only installed editors.

aptitude search '~i~Peditor'
Zoredache
  • 130,897
  • 41
  • 276
  • 420
  • I prefer this because it includes many more packages than the other solutions. My solutions only showed the packages listed with "i" (installed) in the start of the line. This solution also shows lines with "p" (meaning that no trace of the package exists on the system). – Janus Troelsen Feb 28 '12 at 19:14
7
$ dpkg-query -W -f='Package: ${Package}\nProvides: ${Provides}\n' \
  | grep -B 1 -E "^Provides: .*editor"
Package: nano
Provides: editor
--
Package: vim-gnome
Provides: editor, gvim, vim, vim-perl, vim-python, vim-ruby, vim-tcl
--
Package: vim-tiny
Provides: editor
$
Sven
  • 98,649
  • 14
  • 180
  • 226
Janus Troelsen
  • 1,114
  • 1
  • 8
  • 18
6

You can achieve the desired effect without aptitude (which appears to be discouraged these days) by using apt-cache showpkg, which includes a listing of Reverse Provides. Piping it through a small sed script will get rid of the other things:

apt-cache showpkg <package> | sed '/Reverse Provides/,$!d'

A slightly prettier (but longer to type) example (lists package names only, not versions, and sorts them alphabetically) can be achieved with awk:

apt-cache showpkg httpd | awk '/Pa/, /Reverse P/ {next} {print $1 | "sort"}'

...and this can be piped through uniq to remove duplicates (which may exist due to multiple versions of package being reverse-provides). Note that the use of uniq won't help with the first version, as uniq only removes duplicates if they're on adjacent lines and the sed version doesn't sort the output.

Finally, one can define a function for easier use, as follows:

provides () { apt-cache showpkg $1 | awk '/Pa/, /Reverse P/ {next} {print $1 | "sort"}' | uniq;}

Stick this in (for example) .bashrc, so that it'll load when the shell does, and it becomes possible to run provides <package> to get a package's reverse-provides.

Darael
  • 176
  • 1
  • 2
  • `sort -u` will remove the duplicates, so the call to `uniq` isn't needed. But whatever, nice solution, it's much faster. `aptitude` will match `doom-wad-editor` too, so they're no completely equivalent. – Janus Troelsen Feb 27 '13 at 22:22
  • Ooh, good shout on `sort -u`. Didn't know that one. I wonder why it doesn't pick up `doom-wad-editor`...? That's particularly interesting since `apt-cache showpkg` shows it as having no depends at all. – Darael Feb 27 '13 at 22:33
  • Ah. Further inspection shows that `aptitude`'s search will match substrings in the `Provides:` field (thus picking up `deutex`), where the `apt-cache showpkg` method uses exact package names. Both, therefore, presumably have their place. – Darael Feb 27 '13 at 22:37
  • I believe that it must be made clear that `` is a virtual package here. Like, `editor`. – x-yuri Jun 20 '19 at 15:25
  • @x-yuri for there to be any results, sure, but that's only because a virtual package is a name for which there exists at least one `Provides:` line in the repository. In the same way as `apt-cache search`, then, the correct result when there are none is a lack of output - which is what these options provide. There can be concrete and virtual versions of the same package, and I suppose we could enhance these to show the concrete version where it exists, but that's all. – Darael Jun 20 '19 at 15:42
  • I mean, I was like, "`apt-cache showpkg `? Like, `apt-cache showpkg vim-tiny`? And then it shows reverse provides? What is that supposed to mean?" If I haven't given it another thought, I'd think your answer answers no useful question. – x-yuri Jun 20 '19 at 15:56
0

Reading the database directly:

#!/usr/bin/env python3
import sys
import shlex

def whichPkgsProvide(filter,l):
    for pkg in l:
            dic = {}
            pairs = [x.split(": ") for x in pkg.split("\n")]
            for j in pairs:
                    try:
                            dic[j[0]] = j[1]
                    except IndexError:
                            pass

            try:
                    if filter in dic["Provides"]:
                            yield dic["Package"]
            except KeyError:
                    pass


if __name__ == "__main__":
    l = sys.stdin.read().split("\n\n")
    print(list(whichPkgsProvide(sys.argv[1],l)))

Usage:

$ python3 whichPkgsProvide.py editor <  /var/lib/dpkg/available
['vim-gnome', 'nano', 'vim-tiny']
$
Janus Troelsen
  • 1,114
  • 1
  • 8
  • 18
-1

base on your example. I can list package provide editor by use apropos

hvn@lappy: ~ () $ apropos editor
atobm (1)            - bitmap editor and converter utilities for the X W...
bitmap (1)           - bitmap editor and converter utilities for the X W...
bmtoa (1)            - bitmap editor and converter utilities for the X W...
ed (1)               - text editor
editor (1)           - Vi IMproved, a programmers text editor
editres (1)          - a dynamic resource editor for X Toolkit applications
ex (1)               - Vi IMproved, a programmers text editor
gedit (1)            - text editor for the GNOME Desktop
gnome-text-editor (1) - text editor for the GNOME Desktop
gview (1)            - Vi IMproved, a programmers text editor
gvim (1)             - Vi IMproved, a programmers text editor
i3-sensible-editor (1) - launches $EDITOR with fallbacks
nano (1)             - Nano's ANOther editor, an enhanced free Pico clone
notepad (1)          - Wine text editor
pico (1)             - Nano's ANOther editor, an enhanced free Pico clone
psed (1)             - a stream editor
ptked (1p)           - an editor in Perl/Tk
red (1)              - text editor
regedit (1)          - Wine registry editor
rgview (1)           - Vi IMproved, a programmers text editor
rgvim (1)            - Vi IMproved, a programmers text editor
rnano (1)            - Restricted mode for Nano's ANOther editor, an enh...
rview (1)            - Vi IMproved, a programmers text editor
rvim (1)             - Vi IMproved, a programmers text editor
s2p (1)              - a stream editor
sdlBasic (1)         - sdlBasic program editor
sed (1)              - stream editor for filtering and transforming text
select-editor (1)    - select your default sensible-editor from all inst...
sensible-editor (1)  - sensible editing, paging, and web browsing
software-properties-gtk (1) - Software Sources List editor
Tk::ColorEditor (3pm) - a general purpose Tk widget Color Editor
vi (1)               - Vi IMproved, a programmers text editor
view (1)             - Vi IMproved, a programmers text editor
vim (1)              - Vi IMproved, a programmers text editor
winecfg (1)          - Wine Configuration Editor
xedit (1)            - simple text editor for X
zshzle (1)           - zsh command line editor
HVNSweeting
  • 534
  • 2
  • 10
  • 17
  • 1
    -1 : `apropos` search through manual pages, so this will list only installed *commands*... Plus, OP wants a list of *packages*. – MoonSweep Mar 12 '18 at 15:41