Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardBronosky/41e91e858fdf070720704179aa08ed62 to your computer and use it in GitHub Desktop.
Save RichardBronosky/41e91e858fdf070720704179aa08ed62 to your computer and use it in GitHub Desktop.
#!/bin/sh
#apt-whatprovides ver. 201801010101 Copyright alexx, MIT Licence
#rdfa:deps="[realpath,apt-file,grep,which,sh,echo]"
BINARY="$(realpath $(which $@) 2>/dev/null)"
[ -z "$BINARY" ] && BINARY="$@"
echo Searching for $BINARY
PACKAGE="$(apt-file search $BINARY|grep -E ":.*[^-.a-zA-Z0-9]${BINARY}$")"
echo "${PACKAGE}"

I was trying to track down what installed which on my system. After a little work I created /usr/local/bin/apt-whatprovides

#!/bin/sh
#apt-whatprovides ver. 201801010101 Copyright alexx, MIT Licence
#rdfa:deps="[realpath,apt-file,grep,which,sh,echo]"

BINARY="$(realpath $(which $@) 2>/dev/null)"
[ -z "$BINARY" ] && BINARY="$@"
echo Searching for $BINARY
PACKAGE="$(apt-file search $BINARY|grep -E ":.*[^-.a-zA-Z0-9]${BINARY}$")"
echo "${PACKAGE}"

Though for most THINGs that are installed you can just use:

apt-file search $(realpath $(which THING)) | grep 'THING$'

For THINGs that are not installed, you can use:

apt-file search THING | grep '/THING$'

The apt-whatprovides script works for files that are and are not on your system. For example, my system lacked dig but had ping so this it what resulted:

pi@raspberrypi:~ $ apt-whatprovides ping
Searching for /bin/ping
inetutils-ping: /bin/ping
iputils-ping: /bin/ping

pi@raspberrypi:~ $ apt-whatprovides dig
Searching for dig
dnsutils: /usr/bin/dig
epic4: /usr/share/epic4/script/dig
epic4-help: /usr/share/epic4/help/8_Scripts/dig
knot-dnsutils: /usr/bin/dig

Notice that Searching for is a complete path for ping (installed) and just the binary name for dig not installed. This helped me discover that I needed to install dnsutils without needing to go search https://packages.ubuntu.com/#search_contents

@RichardBronosky
Copy link
Author

Temporarily saving it here in case the OP doesn't accept my edit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment