Ubuntu – How to list commands a package provides?

executablepackage-management

I'm curious what commands a certain package provides my system with. By command, I mean an in-path executable that I can run from the command line (ls, grep, sed, etc).

I'm not trying to work out the package from the command, which can be done with:

dpkg -S `which command`

I want the opposite, a list of commands from a package.

Best Answer

The following little loop will handle this with installed packages.

$ for f in $(dpkg -L login); do [[ $(type -P "${f##*/}") == "$f" ]] && echo ${f##*/}; done
nologin
lastlog
newgrp
faillog
su
login
sg

How it works:

  • dpkg -L package generates a list of all the files in a package which we iterate through.
  • We strip off the directory name with a little bashism: ${f##*/} and
  • Using the bash-builtin type -P command we see if that command is both in the path and that its path equals the file we started with.
  • We finish by pumping out the shortened command.
  • [[ condition ]] && command is just a bash shorthand for an if..then statement.

It's important to note that not all packages contain the commands you would expect them to. Apache is split out over multiple packages (with -common and -bin subpackages) and the vlc command isn't in the vlc package, it's in vlc-nox. There are many examples like that.


This can be adapted per Gilles's idea, doing a string match instead of actually checking, but keeping it all in one bash process (and still using the whole path).

for f in $(dpkg -L login); do [[ $f =~ ^${PATH//:/|} ]] && echo ${f##*/}; done

The major difference here is the [[$f =~ ^${PATH//:/|} ]]. That's an in-Bash regex search. The ${PATH//:/|} part is taking the contents of $PATH and is hacking them into a dirty little regex. The condition should check the string begins with part of the path.