- Command to find the source package of a binary?
- 2 Answers 2
- Find what package a file belongs to in Ubuntu/Debian?
- 8 Answers 8
- How to query files, packages, and repositories in Linux
- Great Linux resources
- Classifying the file type
- Finding the package
- Revealing package dependencies
- Identifying the repository
- Wrapping up
Command to find the source package of a binary?
I know there’s a which command, that echoes the full name of a binary (e.g. which sh ). However, I’m fairly sure there’s a command that echoes the package that provides a particular binary. Is there such a command? If so, what is it? I’d like to be able to run this: commandName ls and get coreutils for example.
2 Answers 2
dpkg -S finds the name of the binary package name, which can differ from the name of the source (code) package.
Just for completeness, -S is the short form of the —search option, which is actually an option to the dpkg-query command (but plain dpkg works too)
If you want to find files in a package that you haven’t installed, use apt-file
apt-get install -y apt-file apt-file update
apt-file search /usr/bin/file
Where «file» is the name of whatever you’re searching for.
If you don’t feel like going through this on every debian system, you can use this script:
#!/bin/bash which apt-get >/dev/null || < echo apt-get not found >&2; exit 1; > which apt-file >/dev/null || < apt-get install -y apt-file; apt-file update; >unset i; IFS=$'\x0a'; select i in $( apt-file search "/$@" ); do test -n "$i" || break; apt-get install "$"; done
I just whipped that up then, but it seems to work well.
Note: «dpkg -S» only finds things that you’ve already installed.
Find what package a file belongs to in Ubuntu/Debian?
I frequently find myself missing a program, man page, or other file when working on my Ubuntu 8.04 system. Is there any simple way to look up what package contains a given file (whether it is installed already or not)? Maybe some obscure option for apt or dpkg?
8 Answers 8
apt-file search /path/to/file
sudo apt-get install apt-file
You will need to update its database before you can use it:
FYI: apt-file seems to depend on Contents-amd64.gz provided by repositories. And not all 3rd party repos provide this file.
It did not work for me. I updated the database but it does not return anything. dpkg -S works as described below.
In most cases you will not need to install a non-standard package (apt-file) and get away with using the pre-installed one (dpkg). See top voted answer.
‘dpkg -S’ just matches the string you supply it, so just using ‘ls’ as an argument matches any file from any package that has ‘ls’ anywhere in the filename. So usually it’s a good idea to use an absolute path. You can see in the second example that 12 thousand files that are known to dpkg match the bare string ‘ls’.
In contrast to apt-file , this also finds the package to a file if it was installed manually via dpkg -i !
If you get dpkg-query: no path found matching pattern try this: dpkg -S «$(readlink -fn «$(which rename)»)» .
Weird, this didn’t work for me: dpkg-query: no path found matching pattern /usr/bin/java , though there is a file at /usr/bin/java
packages.debian.org is what I always use to accomplish this task. It is superior over apt-file because it can find parts of filenames as well. It’s also linked up to the main packages list which will list descriptions, bugs, etc. All in all a good website. Not as useful from the command line, but still quite useful.
For speed, I bookmarked the url:
http://packages.debian.org/search?searchon=contents&keywords=%s&mode=filename&suite=unstable&arch=any
in Firefox, and added «debfind» as a keyword (click «more» in the bookmark manager with it hilighted), so I can just type «debfind » and it will work. You can change ‘suite’ it from unstable to stable or testing if you like, for other versions of the distribution.
You can also use dlocate. From the man page;
$ dlocate [ PATTERN ] List all records where either the package name or the filename matches PATTERN.
@Avio dlocate does not find a recently installed package unless you run sudo update-dlocatedb first. The command is automatically run daily in /etc/cron.daily/dlocate
wajig whichpackage /path/to/file
wajig whichpackage /etc/apt/sources.list
Wajig is a handy command line or console tool which wraps all the apt-get, dpkg goodness that you never wanted to learn. .
That command ‘whichpackage’ itself depends on apt-file
you will still need to update its database before you can use it: — I don’t know yet if wajig update whixh does an apt-get update also updates the file cache, but I expect it may.
sudo wajig update sudo apt-file update
( and Super Cow, Easter egss ? ! )
Simmilar to jamuraa’s answer for Debian, you can also go to http://packages.ubuntu.com/ for Ubuntu. You’ll have to scroll down a bit to find the «Search the contents of packages» searchbox where you can enter the path of a file.
The command-not-found package gives you hints about potential commands and the name of the debian package. It works by typing the command in the bash shell and looking at its output.
For example if the command name is known:
zer@ivy:~ 10:45 $ zsh5 The program 'zsh5' is currently not installed. To run 'zsh5' please ask your administrator to install the package 'zsh' zsh5: command not found
and if the command is not known, some guesses are applied:
zer@ivy:~ 09:46 $ zsh No command 'zsh' found, did you mean: Command 'lsh' from package 'lsh-client' (main) Command 'osh' from package 'omake' (main) Command 'ysh' from package 'libyaml-shell-perl' (main) Command 'ssh' from package 'openssh-client' (main) Command 'vsh' from package 'crystalspace' (main) Command 'dsh' from package 'dsh' (main) Command 'ash' from package 'ash' (main) Command 'msh' from package 'nmh' (main) Command 'zssh' from package 'zssh' (main) Command 'qsh' from package 'gridengine-client' (main) Command 'sh' from package 'dash' (main) Command 'bsh' from package 'bsh' (main) zsh: command not found
How to query files, packages, and repositories in Linux
Having some experience with Linux, you probably know that you can’t just share a command or a utility between systems. The reason that you can’t simply copy an executable from one system to another is because of dependencies, such as libraries and other supporting packages. Some utilities certainly can be copied but this isn’t universally so. If you’ve forgotten which package you installed that included the nslookup command, for example, you need a way to find it.
Great Linux resources
Furthermore, once you find the package that a command belongs to, you might want to find out from which repository you installed the package. The basic repositories of AppStream, BaseOS, and Extras contain a lot, but certainly not all of the packages you might need or encounter. In this article, I explore some methods of querying the system to find related repositories, packages, and commands.
- Packages contain commands, utilities, and libraries
- Packages have dependencies
- Repositories contain, store, and provide packages
Classifying the file type
Is the utility you’re using a script, plain text, or a command? Is it compiled? What is the file’s location? You might want some basic information about the utility. The which command locates a file if it’s in your path. You will need to locate a file before you can further identify it.
To display file type information, the file command is indispensable.
$ file mtr mtr: cannot open `mtr' (No such file or directory) $ file /usr/sbin/mtr /usr/sbin/mtr: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=d34479cefafe7299b65d8375769c63978c6c453f, stripped, too many notes (256)
The first attempt failed because the file command doesn’t know the location of the mtr command. After the full path is given to file , the information is displayed.
Here is an example of an executable file that is not compiled.
$ file /usr/sbin/ifcfg /usr/sbin/ifcfg: POSIX shell script, ASCII text executable
As you can see, the file command is an important one to know and one which you’re sure to use throughout your career.
Finding the package
The nslookup command is one that you use frequently but it isn’t installed on any of the systems at your new job. You want to install it but you can’t remember which package contains it and attempting to install it as nslookup fails.
# dnf -y install nslookup Last metadata expiration check: 2:44:51 ago on Tue 10 Nov 2020 03:27:27 PM CST. No match for argument: nslookup Error: Unable to find a match: nslookup
To find the package that contains nslookup , you can perform a «reverse» lookup using the rpm command.
# rpm -qf nslookup error: file /root/nslookup: No such file or directory
What’s wrong here? The rpm command can’t find nslookup but it’s installed because you’ve used it. Wait. Look at the path: /root/nslookup . There’s no nslookup command in your current (root’s home) directory. Locate nslookup using the which command and then issue the rpm command.
# which nslookup /usr/bin/nslookup # rpm -qf /usr/bin/nslookup bind-utils-9.11.13-6.el8_2.1.x86_64
You see that nslookup is part of the bind-utils package. Now you can copy it to your other systems and use it as you normally would.
# nslookup google.com /opt/nslookup: error while loading shared libraries: libdns.so.1107: cannot open shared object file: No such file or directory
The error tells you that the nslookup command has dependencies that haven’t been met on the new system you’ve copied it to. The solution is to install it individually onto each system using DNF/YUM.
Revealing package dependencies
You really like the nslookup command, and you’d like to have it available on all of your systems. You realize after a failed attempt that you can’t simply copy it to another system and expect it to work. After much research, you discover that the nslookup command has several dependencies that must be met. What are those dependencies?
# dnf deplist nslookup Last metadata expiration check: 3:00:24 ago on Tue 10 Nov 2020 03:27:27 PM CST.
Does this mean there are no dependencies for nslookup ? No. It means that you attempted to find dependencies for a command rather than for a package.
# dnf deplist bind-utils Last metadata expiration check: 0:00:14 ago on Tue 10 Nov 2020 06:29:37 PM CST. package: bind-utils-32:9.11.13-6.el8_2.1.x86_64 dependency: /usr/libexec/platform-python provider: platform-python-3.6.8-23.el8.i686 provider: platform-python-3.6.8-23.el8.x86_64 dependency: bind-libs(x86-64) = 32:9.11.13-6.el8_2.1 provider: bind-libs-32:9.11.13-6.el8_2.1.x86_64 *** longer list of dependencies *** dependency: rtld(GNU_HASH) provider: glibc-2.28-101.el8.i686 provider: glibc-2.28-101.el8.x86_64
Remember that packages have dependencies. Listing dependencies can be valuable if you want to satisfy them manually or if you think that new dependencies might interfere with or be incompatible with packages and dependencies already installed on your system.
Identifying the repository
You saw a fellow sysadmin using the hardware information tool ( hwinfo ) command on her system. You try the command on your system but you receive a «command not found» error. You then attempt to install it.
# dnf -y install hwinfo Last metadata expiration check: 0:09:34 ago on Tue 10 Nov 2020 06:42:15 PM CST. No match for argument: hwinfo Error: Unable to find a match: hwinfo
You ask your coworker to tell you where she got the hwinfo package. She performs the following query to find its source.
# dnf list hwinfo Last metadata expiration check: 0:11:17 ago on Tue 10 Nov 2020 06:42:15 PM CST. Installed Packages hwinfo.x86_64 21.47-9.el8 @epel
You note that the source appears to be a repository identified as epel . You further ask her to tell you how to set up the epel repository. She remembers that epel is actually an installable RPM package but doesn’t remember its full name. She performs the following query to identify it.
# rpm -qa |grep epel epel-release-8-8.el8.noarch
Now you can install the epel-release package and then install the hwinfo package.
Note: The epel-release repository is the Extra Packages for Enterprise Linux repository configuration and is the first thing I install on any new system I encounter that doesn’t have it.
Wrapping up
Linux provides its administrators with a lot of handy utilities and commands to find information about the system and its components. You need to add these commands and utilities to your sysadmin toolbox. As you can see from this article, you don’t need to know a command’s every obscure option to get what you need. Use man pages for those.