How to Find Which Package a File Belongs in Linux

Linux package is a compressed file archive that contains all files belonging to specific applications. In some situations, you may need to find the package name belonging to a file.

In this tutorial, we learn how to find which package a file belongs or owns to in the Linux system.

Which package provides file on Ubuntu/Debian System

To find which package provides file on Ubuntu/Debian System we have a few methods.

Using dpkg

You can use dpkg and dpkg-query commands to find package that provides a file. This command search for a filename from the installed packages.

Syntax:

dpkg -S filename
dpkg-query -S filename

For example, to find to which package the /bin/ls file belongs, use the following command.

$ dpkg -S /bin/ls
dpkg -S command

Using dpkg-query

You can also use dpkg-query command to find the package name for the installed file.

$ dpkg-query -S /bin/ls
$ dpkg-query -S '/bin/ls'
$ dpkg-query -S 'passwd*'

Using apt-file

You can use apt-file to look for files inside DEB packages on your system, as well as packages that aren't installed on your Debian systems but are available via the repositories.

The apt-file package is not installed by default. To install apt-file on Ubuntu, and other Debian-based Linux distributions, type:

$ sudo apt install apt-file 

Make sure to update the database cache, type:

$ sudo apt-file update

Now let's search the package which provides the route command, use the following command.

$ apt-file search 'bin/route'
apt-file search command

On the left side, you can see the package name belonging to the route command.

Which package provides file on RHEL Based System

This is supported on Linux distributions such Redhat, Fedora, AlmaLinux, Rocky Linux, CentOS Stream, and Oracle Linux.

Using DNF Command

You can use provides option along with DNF to find the package of a file in RHEL based system such as Fedora. It matches with all file provides of any available package.

Syntax:

dnf provides filename
dnf provides *filename
dpkg-query --search '/path/to/file'

For example, if you want to find the package of a binary file, use the following command.

$ dnf provides *bin/ls
dnf provides command

Using RPM and Yum Command

You can use the rpm command to find the package that provides a specific file.

Syntax:

rpm -rf filename
rpm -q --whatprovides filename

This will find the package name for the installed package file

For example, if you want to find the rpm package that provides the /bin/ls file, use the following command.

$ rpm -qf /bin/ls
rpm -qf command

You may also use the following rpm command:

$ rpm -q --whatprovides /etc/nginx/nginx.conf
rpm -q --whatprovide command

Using the yum command, you can do the same as follows:

$ yum whatprovides netstat

You can also use a particular library file to search for the package it belongs to

Search for files not installed on Debian/Ubuntu

You can visit Debian packages and Ubuntu packages search and search for the file. The result will show the package name. Then you can use your apt command to install the respective package.

Ubuntu packages search

Conclusion

In this tutorial, we learned how to find what package a file belongs in Linux. Thanks for reading, please provide your feedback and suggestions.

Leave a Comment