get package name for file in ubuntu

How to Find Package For File in Ubuntu

Every Linux system offers tons of commands and utilities. Sometimes you may need to find the package name for a given file. In this article, we will look at the different ways to find package for file in Ubuntu.


How to Find Package For File in Ubuntu

Almost every Linux command is a binary file located somewhere on your system. When you run the command, Linux simply locates the command’s binary file and executes it. There are several ways to find package name for a given command or file. We will look at each of them one by one.


1. Using dpkg

You can simply use dpkg command with -S or –search option, to get package name for a given command’s binary file on your system. It maintains useful information about dpkg packages on your system. Here is the syntax to get package location for command in the dpkg database.

$ dpkg -S command_name

For example, open terminal and run the following command to get package name of ls command.

$ dpkg -S ls
coreutils: /bin/ls

The above command will immediately display the package name and binary location of given command. In the above output, we see that ls command is a part of coreutils package and its binary is located at /bin/ls. You can also use –search option for the same.

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

Now if you want to get more details about the given command’s package, use -s option. Here is an example to get detailed information about coreutils package.

$ dpkg -s coreutils
Package: coreutils
Essential: yes
Status: install ok installed
Priority: required
Section: utils
Installed-Size: 9040
Maintainer: Ubuntu Core Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: i386
Version: 5.97-5.3ubuntu3


2. Using dpkg-query

You can also use dpkg-query utility to get package name of a command. It will show information about packages listed in dpkg database on your system. It has the same options as dpkg command above.

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

OR
$ dpkg-query --search '/bin/ls'

In both the above commands, you will get the following output.

coreutils :/bin/ls


3. Using apt-file

You can use apt-file command to search for package information in APT package management system. But it is not installed by default in Ubuntu. So you will need to run the following commands to install it.

Please note, the command apt-file works only after you run the second command ‘sudo apt-file update’ below. It will install a database of 160+Mb on your system. So use apt-file only if you absolutely need to.

$ sudo apt-get install apt-file
$ sudo apt-file update

Once you have installed and updated apt-file command, you can look for the package name with ‘apt-file search’ command.

$ apt-file search kvm-ok
cpu-checker: /usr/sbin/kvm-ok
cpu-checker: /usr/share/man/man1/kvm-ok.1.gz

Please note, you may be able to get package information for most system utilities like rm and ls using dpkg or apt-file but if you have installed third-party utilities, then you need to use the appropriate command to get package name. For example, if you have installed the package via dpkg command, use dpkg or dpkg-query to get package name. If you use apt-file command, you may not get the required information. Similarly, if you have installed package using apt command, then use apt-file to get package name. If you use dpkg -S command then you will not get the required information.

Also read:

How to Prompt for User Input in Shell Script
How to Uninstall SQL Server in Ubuntu
How to Install or Upgrade Software from Unsupported Release
Shell Script to Check if Script is Already Running
How to Check if Input Argument Exists in Shell Script

Leave a Reply

Your email address will not be published.