Ubuntu apt get cache list search commands cheat sheet
In this article
Ubuntu apt-get cache list search commands provide a powerful way to manage, inspect, and search software packages in the system's package cache. These commands allow users to list available packages, search for specific ones, view detailed information, and manage local cache files without installing or modifying the system.
Understanding the APT Package Cache
The Advanced Package Tool (APT) maintains a local cache of package metadata downloaded from configured software repositories. This cache includes information like package names, versions, dependencies, descriptions, and download locations. When you run apt update, APT refreshes this cache by downloading updated metadata from the sources listed in /etc/apt/sources.list and files in /etc/apt/sources.list.d/. The cache is stored in /var/cache/apt/ and enables fast, offline-capable package searches and operations.
Without a refreshed cache, search and list commands may return outdated or incomplete results. Therefore, it’s standard practice to run sudo apt update before performing any search or list operation.
Basic Cache Listing Commands
APT provides several commands to list packages from the cache. These help identify what software is available for installation from your configured repositories.
- apt list – Displays all packages known to APT. Use with filters:
apt list --installed– Lists only installed packages.apt list --upgradable– Shows installed packages with newer versions available.apt list --all-versions– Lists all available versions of each package.
- apt-cache dumpavail – Outputs raw package records from the cache, useful for scripting or debugging.
- apt-cache stats – Shows summary statistics: number of packages, size of cache, and memory usage.
For example, to see all available versions of the nginx package, run:
apt list nginx --all-versions
This returns entries like nginx/focal 1.18.0-6ubuntu14 amd64, indicating version, release, and architecture.
Searching the Package Cache
The primary command for searching packages is apt-cache search, which scans package names and descriptions using keyword matching.
- apt-cache search <keyword> – Finds packages whose name or description contains the keyword.
- apt-cache show <package> – Displays detailed information about a specific package, including dependencies, size, and description.
- apt-cache showpkg <package> – Shows low-level dependency information, including reverse dependencies (packages that depend on it).
- apt-cache policy <package> – Displays version priorities and which repository a package will be installed from.
Example: To find all packages related to Python 3 development, use:
apt-cache search python3-dev
This might return:
python3-dev - header files and a static library for Python (default)
libpython3-dev - header files and a static library for Python (v3)
...
A common misconception is that apt search and apt-cache search differ significantly. In modern Ubuntu, apt is a user-friendly wrapper over apt-cache and apt-get, so apt search is functionally equivalent to apt-cache search but with better formatting.
Managing the Local Cache Files
APT stores downloaded package files (i.e., .deb binaries) in /var/cache/apt/archives/. These can accumulate over time and consume significant disk space.
- apt-get clean – Removes all
.debfiles from the cache. - apt-get autoclean – Removes only outdated package files (those no longer available in repositories).
- apt-get autoremove – Removes unused dependencies that were automatically installed and are no longer needed.
To view what’s in the cache:
ls /var/cache/apt/archives/
Use du -sh /var/cache/apt/archives/ to check disk usage. Regularly running sudo apt autoclean and sudo apt autoremove helps maintain system cleanliness.
Advanced Search and Filtering Techniques
For more precise searches, combine APT commands with shell tools like grep, awk, or sort.
- Filter installed packages containing “ssh” in the name:
apt list --installed | grep ssh - List packages by size (requires parsing
apt-cache showoutput or using external tools): - Find packages maintained by a specific group (e.g., Debian Python):
apt-cache search . | xargs apt-cache show | grep -i "maintainer:.*python" -B 5
You can also use regular expressions with apt-cache search, though it supports only basic pattern matching. For complex queries, redirect output to a file and process it externally.
Another useful trick is checking if a package provides a specific binary. Use:
apt-cache search --names-only ^package-name$
The --names-only flag limits search to package names, avoiding noise from descriptions. The ^ and $ ensure an exact name match.
Frequently Asked Questions
What is the difference between apt and apt-cache?
apt is a modern command-line interface designed for end users, combining the most common functions of apt-get and apt-cache with better progress reporting and output formatting. apt-cache is a lower-level tool focused specifically on querying package metadata. For searching and listing, apt search and apt list are user-friendly equivalents of apt-cache search and apt list.
Why does apt-cache search return too many results?
By default, apt-cache search matches keywords in both package names and descriptions, which can yield broad results. To narrow output, use apt search --names-only or pipe results through grep with stricter patterns. For example: apt search --names-only '^nginx$' returns only the exact package named nginx.
How do I search for a file inside packages?
apt-cache cannot search by file contents. Instead, use apt-file, which indexes files contained in packages. Install it with sudo apt install apt-file, update its database with sudo apt-file update, then search with apt-file search <filename>. For example: apt-file search /usr/bin/python3 shows which package provides that binary.
Can I search for packages without internet access?
Yes, as long as the package cache has been updated recently via apt update, you can perform searches offline using apt-cache search or apt list. However, you cannot refresh the cache or install packages without an internet connection (unless using local repositories or cached .deb files).
Is it safe to delete the apt cache?
Yes, it is safe to clear the /var/cache/apt/archives/ directory using apt-get clean or apt-get autoclean. This only removes downloaded .deb files, not installed software. The system will re-download packages if needed. However, keeping the cache can speed up reinstalls and is useful in offline environments.
How do I find which package provides a specific command?
Use apt-file as described above. Alternatively, if the command is already installed, run dpkg -S <command> to find the owning package. For example: dpkg -S /usr/bin/wget returns wget: /usr/bin/wget.