Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Overview

In Linux, knowing which installed package owns a particular file is important. This knowledge can be helpful in system maintenance, troubleshooting issues, checking dependencies, and understanding the structure of installed packages on our system.

In this tutorial, we’ll see how to determine which installed package owns a specific file in Linux.

2. Debian-Based Distributions

In Debian-based distributions such as Ubuntu and Linux Mint, we can use tools such as dpkg, apt-file, or dlocate to find out which package owns a particular file.

2.1. Using dpkg

The dpkg command interacts with the low-level package database, which contains information about every installed package and its associated files. To find out which package owns a specific file, we’ll use the -S option, which allows us to search through the package database.

For example, let’s find out which package owns the /bin/ls file:

$ dpkg -S /bin/ls
coreutils: /bin/ls

The above output indicates that the coreutils package owns the /bin/ls file.

2.2. Using apt-file

apt-file allows us to search for files in available packages, even if they aren’t installed. Additionally, this is useful when we want to know which package provides a particular file, even if that package isn’t yet installed on our system.

First, we need to install it:

$ sudo apt install apt-file

Once it’s installed, we need to update the apt-file database to ensure it has the latest information:

$ sudo apt-file update

Now, let’s search for a package that owns a specific file:

$ apt-file search /bin/nano
nano: /bin/nano
...

In this example, we search for which installed or available package owns the /bin/nano file. The above output indicates that the nano package owns the /bin/nano file.

2.3. Using dlocate

The dlocate tool allows us to list files installed by a package, find out which package owns a particular file, and provide various details about installed packages.

Since this tool isn’t installed by default in most Debian-based systems, we need to install it:

$ sudo apt install dlocate

To illustrate, let’s find out which package owns the /usr/bin/wget file:

$ dlocate /usr/bin/wget
wget: /usr/bin/wget

The above output indicates that the /usr/bin/wget file is owned by the wget package.

3. Red Hat-Based Distributions

In Red Hat-based distributions, such as CentOS, Fedora, and RHEL, we can use either the rpm, yum, or dnf commands to find out which installed package owns a specific file.

3.1. Using rpm

The rpm command interacts with the RPM database to retrieve information about installed packages. To determine which package owns a particular file, we use the -qf option:

$ rpm -qf /bin/ls
coreutils-9.4-6.fc40.x86_64

The above output indicates that the coreutils package owns the /bin/ls file.

3.2. Using yum or dnf

For versions of Red Hat-based systems that use yum or dnf as the package manager, we can use the provides option to find which package owns a file.

To demonstrate, let’s find the package that owns the /usr/bin/vim file using yum:

$ yum provides /usr/bin/vim
vim-enhanced-2:9.1.158-1.fc40.x86_64 : A version of the VIM editor which includes recent enhancements
Repo        : fedora
Matched from:
Filename    : /usr/bin/vim

In this example, vim-enhanced represents the package that owns the /usr/bin/vim file.

Alternatively, we can also use the dnf command:

$ dnf provides /usr/bin/git
git-core-2.44.0-1.fc40.x86_64 : Core package of git with minimal functionality
Repo        : fedora
Matched from:
Filename    : /usr/bin/git

The above command determines which package owns the /usr/bin/git file, which in this case is the git-core package.

4. Arch-Based Distributions

Arch-based distributions such as Arch Linux use pacman to install and manage packages. We can use the pacman command or the pkgfile tool to find which package owns a particular file.

4.1. Using pacman

Using the -Qo option with the pacman command, we can determine which package owns a specific file. This option checks the local package database for the specified file:

$ pacman -Qo /usr/bin/nano
/usr/bin/nano is owned by nano 7.2-1

The above output indicates the nano package, specifically version 7.2-1, owns the /usr/bin/nano file.

4.2. Using pkgfile

pkgfile allows us to search official repositories to find out which package contains a specific file. This enables us to find out which package provides a particular file, even if it’s not yet installed on our system.

First, we need to install it:

$ sudo pacman -S pkgfile

Next, we need to update the pkgfile database to ensure it has the latest information:

$ sudo pkgfile --update

Now, let’s search for the package that owns the /usr/bin/ls file:

$ pkgfile /usr/bin/ls
core/coreutils

This output indicates that the /usr/bin/ls file is part of the coreutils package in the core repository.

5. Conclusion

In this article, we explored how to determine which installed package owns a specific file in Linux. To achieve this, we used different tools and commands across multiple Linux distributions.

First, we discussed the dpkg and apt-file commands in Debian-based distributions. Then, we looked at the rpm, yum, and dnf commands in Red Hat-based distributions. Lastly, we discussed the pacman command and pkgfile tools in Arch-based distributions.