What a package manager keeps
A package is an archive of files plus metadata. The metadata includes a name, a version, an architecture, a list of dependencies, and often a list of the files it installs. The package manager keeps a local copy of that metadata for everything installed on the machine. It also keeps an index of the packages available from remote repositories, so it can find an update or a dependency that is not yet on disk.
Those two pieces explain most of the behavior you see. The installed database answers questions about this machine. The repository index answers questions about what could be installed. A command that changes the system needs both, and a command that only reads the installed database can work offline.
apk on Alpine
Alpine Linux uses apk. Its main subcommands are add to install, del to remove, upgrade to update, and info to query. Repositories are listed in /etc/apk/repositories. Installing and removing need administrative rights, so an ordinary user runs only the query commands.
The installed database is a plain text file, /lib/apk/db/installed. Each package is a block of lines. A line that starts with P: gives the package name, a V: line gives its version, an A: line gives the architecture, and an S: line gives the compressed size. You can read the block for one package directly, which is useful when a query command is not available.
grep -c "^P:" /lib/apk/db/installed
grep -A1 "^P:bash$" /lib/apk/db/installedThe first command counts the installed packages, one P: line each. The second prints the name line for bash and the line after it, which holds the version. The output is the same for every user, and it needs no network.
Some apk query subcommands consult the repository index, and when the machine has no network they print a warning about fetching it. Read the local database when you only need facts about installed software. Keep the repository commands for machines that are online and that you are allowed to change.
apt on Debian and Ubuntu
Debian and Ubuntu use apt-get and apt for the same kinds of work, with dpkg underneath as the low-level tool that unpacks a single package. The installed database is /var/lib/dpkg/status, which has a similar block format. The dpkg -S command finds the package that owns a file, and dpkg -L lists the files of a package. Those two commands answer the questions you usually ask of apk.
This course machine uses apk, so the commands above are the ones you can run. The apt commands are described here so that you recognize them on other systems. A command written for one distribution rarely runs unchanged on another, because the package names, the repository layout, and the database paths differ.
Ownership: which package owns this file
The most useful question to ask about a file is which package installed it. If a file is missing or changed, you need to know whether the package manager put it there, so that you can reinstall the package rather than guess. On Alpine the answer comes from the F: and R: lines of the installed database, where a directory line is followed by file lines. On Debian the same question is answered by dpkg -S.
You can find the package for bash in the database by reading its block. Its files are listed below its header, which is useful when you want to know what a package has placed on disk.
grep -A3 "^P:bash$" /lib/apk/db/installedThe block contains a few header lines, and the file lists appear further down. The exact lines depend on the package version, so read them in the lab instead of memorizing them.
It helps to think of the installed database as a ledger that the package manager writes to in a single careful step. When you install a package, the manager adds its file list, its version, and its dependency links in one transaction. When you remove one, it removes the entries in the reverse order. A half-finished change leaves the ledger inconsistent, which is why package managers take a lock while they work and why a second command may wait or refuse to run. If you find a lock file left behind after an interrupted run, do not delete it until you have checked that no manager process is still running.
Version numbers deserve a note too. A version such as 5.3.9-r1 has two parts: the upstream version of bash and a revision number that the Alpine maintainers add when they rebuild the package. Two installed packages with the same upstream version but different revisions are not the same build, so compare the whole string when you check whether a fix is present.
Dependencies and why removal is safe
A package can depend on other packages, and the package manager refuses to remove a package that other installed packages need. That is the reason a removal may report that a second package must go too. The database records each dependency, so the manager can answer the question before it changes anything. Reading the database gives you the same picture, although you will need to follow the links by hand.
Before you remove a package on a real system, ask the manager what depends on it. The answer is cheaper than repairing a system that lost a library it needed.
Hands-on: read the installed database
Work through the database from the outside in. Count the packages, read the block for one package, and then save the count and the bash version to files. The checks in the lab read those files, so run the commands in /workspace.
grep -c "^P:" /lib/apk/db/installed > count.txt
grep -A1 "^P:bash$" /lib/apk/db/installed > bash-pkg.txt
cat count.txt bash-pkg.txtThe first file holds one number, which is the count of installed packages. The second holds the package name and the line that starts with V:. Together they give you a small, offline inventory of the machine, which is the first step of most package investigations.