Packages and backups

The apt-file command

Searches which package contains a file, even when that package is not installed.

Syntax

apt-file [update|search archivo|list paquete]Try it in the terminal →

Most useful options

-xTreats the pattern as a Perl regular expression. Without it, the pattern is literal text
-iIgnores case when searching
-lShows only the package name, without the file path
-FDoes not add wildcards to the pattern. It is the default for list
-a arquitecturaSearches the indexes of an architecture other than the installed one
-I tiposSearches only the indexes of those types, for example deb
-f archivoReads the patterns from a file, one per line. With - it reads from standard input

Copy-ready examples

$ sudo apt-file updatedownloads the contents index files. It needs root because it calls apt update.
$ apt-file search mkfs.ext4finds which package provides that program.

Example output

e2fsprogs: sbin/mkfs.ext4
$ apt-file list e2fsprogs | head -n 5shows the first paths a package installs, without installing it.
$ apt-file -l search zlib.hfinds which development package ships that header.

Example output

zlib1g-dev
$ apt-file -x search '^usr/bin/ls$'searches for an exact path with a regular expression. It is slower than literal text.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
E: Cannot use -F together with -xYou asked for literal matching and regular expressions at the same time. They are incompatible options.Remove one of them: -x for regular expressions or -F for literal text.
E: Please pass an APT config file after -c/--config-fileThe value given to -c is a folder. -c used to point to a cache directory; now it expects an APT configuration file.Pass the path of a file, for example -c ~/.config/apt-file.conf.

Typical mistakes

  • apt-file uses downloaded indexes: if you do not run update, it will find nothing or give old results.
  • By default the pattern matches anywhere in the path: ls also finds tools.h. Give as full a path as you can; apt-file removes the leading / for you.
  • search only matches file names. Directories are not in the indexes, so a folder name will return nothing.
  • Exit code 1 means a search with no results, 3 an empty cache and 4 that no indexes match the filters.

Differences from similar commands

  • dpkg -S: finds the installed package that owns a path. apt-file also searches packages you do not have.
  • dpkg -L: lists the files of an installed package. apt-file list does the same without installing it.
  • which: finds a program in your PATH. apt-file searches the packages in the repositories.

FAQ

Why does it not find a file I know exists?

Usually the index is old, or the repository does not publish contents indexes. Run sudo apt-file update, and try -i if you are unsure about capital letters.

Do I need the package installed?

No. That is its purpose: it queries the repository indexes. For a package already installed, dpkg -L shows its files from what is on the machine.

Why is the search with -x so slow?

The manual warns that -x can be quite slow. If literal text is enough, use it and keep regular expressions for when you need them.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course