#!/bin/bash
# /usr/local/bin/cmd2pkg
# http://crystalfaeries.net/posix/bin/cmd2pkg
# Celeste Crystalfaery 2016-11-13 21:17:40+00:00
# from the command name find the package which installed it
# inspired by: http://www.commandlinefu.com/commands/view/17271/find-name-of-package-which-installed-a-given-shell-command
let result=0 # default success
while [ $# -ne 0 ]
do # iterate arguments
WHICH="$(which ${1})"
if [[ $? -ne 0 ]]
then # command not found in $PATH
echo "${0}: ${1} is not an executable command found within your PATH $PATH" 1>&2
let result=1 # command not found
else # command was found in $PATH
PACKAGE=$(dpkg -S "$(readlink -e ${WHICH} 2> /dev/null)" 2> /dev/null | cut -d ':' -f 1 2> /dev/null) || PACKAGE=""
if [ "${PACKAGE}" == "" ]
then # any of multiple error types finding command in a package
echo "${0}: ${WHICH} was not found in any package." 1>&2
let result=2 # package not found
else # found it! give all the details:
echo "$(file ${WHICH}): ${PACKAGE}"
fi
fi
shift # next command
done
exit ${result}
############################################################################################
# Alternate solution
############################################################################################
<http://www.commandlinefu.com/commands/view/18250/find-the-package-that-installed-a-command>
$ whatinstalled () { local cmdpath=$(realpath -eP $(which -a $1 | grep
-E "^/" | tail -n 1) 2>/dev/null) && [ -x "$cmdpath" ] && dpkg -S
$cmdpath 2>/dev/null | grep -E ": $cmdpath\$" | cut -d ":" -f 1; }
Put this one-line function somewhere in your shell init, re-login and
try whatinstalled <command>
This is an elaborate wrapper around "dpkg -S", with numerous safeguards.
Symlinks and command aliases are resolved. If the searched command is
not an existing executable file or was installed by some other means
than dpkg/apt, nothing is printed to stdout, otherwise the package name.
syntax highlighted by Code2HTML, v. 0.9.1