A command name can exist without proving how it was installed. A package database can contain a record for software that is no longer installed. A repository can offer a candidate version without that version being on your machine. Record these facts separately before deciding on any change.
You will inspect the named tree package and retain actual lookup, package
and cached-repository evidence in one owned note. Tree may already be
installed or may be absent. Both are valid observations; this lesson changes
neither state and never executes tree.
1. Separate program, package, repository and service
| Term | Meaning here | Evidence to avoid confusing with it |
|---|---|---|
| Program or command | Something the shell can resolve and potentially run. | Finding a name does not prove package ownership or installation state. |
| Package | A managed collection of software and metadata tracked by the package system. | A database record or successful query does not necessarily mean fully installed. |
| Repository candidate | The version APT's current policy selects from available local metadata. | A candidate is not proof of installation or current downloadability. |
| Service | An activity managed as a service, with its own state. | Installing a package does not prove a service is running. |
APT works with repository metadata and package operations. Dpkg's database
records local package state. Here dpkg-query inspects that database, while
apt-cache policy tree inspects the named package's cached policy information.
Their answers complement each other. No install, update, upgrade, removal,
repository edit or service operation is part of this lesson.
2. Check the guest and query scope
Complete Archive and restore a small owned file set first. Use the ordinary nonzero-UID account in the checked Ubuntu 24.04 guest and Bash. Run one command line at a time, read its result and stop after an unexpected error. Reuse the taught identity and origin checks:
id -u
id -un
ps -p $ -o comm=
type cd pwd printf set declare type mkdir cat dpkg-query apt-cache ls rm rmdir
Require the recorded nonzero account and active Bash. The shell commands
cd, pwd, printf, set, declare and type must be Bash builtins;
the other tools must use expected guest executables. For the listing alias:
type -a ls
type -P ls
Accept only the understood color-only alias, if present, and expected executable matches. Stop for hidden functions, other aliases or unexpected origins. Inspect options without changing them:
set -o
Require errexit off because a missing lookup or missing package record can
produce a nonzero result that needs interpretation. Preserve a different
setting and reconcile the baseline rather than changing it here.
DPKG_ROOT and DPKG_ADMINDIR can redirect dpkg's root/database when
inherited. APT_CONFIG can select APT configuration. Use the taught named
read-only declaration inspection before package queries:
declare -p DPKG_ROOT
declare -p DPKG_ADMINDIR
declare -p APT_CONFIG
Require the expected not-found result for every name: no declaration exists, including an empty declaration. This conservative course baseline is stricter than inheritance itself; an unexported local variable is not automatically passed to an external command. Preserve existing declarations and reconcile the practice environment. Do not unset them or select another package database.
3. Create one observation note
The task owns ~/linux-practice/inspect-package-state/. Inspect the known
parents and prospective exercise entry without trailing slashes:
ls -ld ~
ls -ld ~/linux-practice
ls -ld ~/linux-practice/inspect-package-state
Require ordinary parent directories owned by your account, without links or unknown identity. Require the exercise name absent for a fresh start; use the resume section if it already exists. After acceptance:
mkdir ~/linux-practice/inspect-package-state
ls -ld ~/linux-practice/inspect-package-state
Require the new ordinary owned directory, then enter and check separately:
cd ~/linux-practice/inspect-package-state
pwd
ls -a
Require the recorded shared-root path followed by /inspect-package-state
and only dot entries. Confirm package-state.txt absent before using the
taught fresh-output redirection:
printf '%s\n' 'Package observation: tree' 'Read-only queries; preserve initial state.' > package-state.txt
ls -ld -- package-state.txt
cat package-state.txt
Require the ordinary owned single-link note and exactly those two initial lines. Subsequent sections append actual evidence, not a supplied successful output. Recheck this note's identity before each append group.
The taught >> package-state.txt 2>&1 appends stdout to the note, then routes
stderr to that same destination. It does not update the package system.
The immediately following printf records the preceding command's $? before
another command replaces it. Read the entire section, including diagnostics;
a plausible number without its output is insufficient evidence. Require
every header write and append redirection to succeed before continuing its
group. If writing the note fails, stop rather than appending a misleading
status or treating an incomplete section as package evidence.
4. Observe command lookup without executing tree
The taught type -a tree lists all shell matches for this name. An alias,
function or executable path is lookup evidence, not package ownership proof.
A missing match is also useful; do not install software to make this query
succeed. Capture the actual result:
printf '%s\n' '--- Command lookup: tree ---' >> package-state.txt
type -a tree >> package-state.txt 2>&1
printf 'lookup exit: %s\n' "$?" >> package-state.txt
cat package-state.txt
Record the actual matches or missing-name diagnostic. Even if a path appears, do not execute it in this task. A local script could supply that name, and an installed program could be outside the current lookup path. Continue to the database observation rather than treating lookup as the final answer.
5. Read the package state, including all three abbreviation characters
dpkg-query --show queries matching package records; we supply only the
literal name tree. --no-pager keeps this bounded output out of a pager.
--showformat selects the fields. The format is single-quoted so Bash leaves
${...} unchanged for dpkg-query's formatter, not shell-variable expansion.
The formatter's \n starts a new output line.
| Field | Meaning |
|---|---|
${binary:Package} | Reported binary package name, with qualification where applicable. |
${db:Status-Abbrev} | Exactly three characters: desired action, current package status and error flag. |
${Status} | The corresponding full status words, useful for interpreting the abbreviation. |
${Version} | Version stored in this record; it is an installed version only when the state supports that interpretation. |
The brackets around the abbreviation expose its final blank character. These are illustrative state examples, not results from your guest:
| Bracketed value | Full-state meaning | Decision in this lesson |
|---|---|---|
[ii ] | Desired install, currently installed, no error flag; commonly install ok installed. | Record and preserve the existing installation. |
[hi ] | Desired hold, currently installed, no error flag. | It is installed and held; preserve both facts, with no repair or hold change. |
[rc ] | Desired removal, residual configuration state, no error flag. | This is not a clean absent starting state; preserve and reconcile it. |
Other states, error flags or unfamiliar combinations require interpretation. Do not reduce a three-character state to the first letter or treat every row as installed. Capture the exact named query:
printf '%s\n' '--- Package database: tree ---' >> package-state.txt
dpkg-query --no-pager --show --showformat='package=${binary:Package}\nabbrev=[${db:Status-Abbrev}]\nstatus=${Status}\nversion=${Version}\n' tree >> package-state.txt 2>&1
printf 'dpkg-query exit: %s\n' "$?" >> package-state.txt
cat package-state.txt
Interpret the recorded query result separately from the package state:
- Exit
0means the query completed successfully; inspect its state fields before calling the package fully installed. - Exit
1can mean a requested match was not found, wholly or partly. In this single-name query, a clear no-match diagnostic for tree is evidence of no matching database record. Do not generalize every exit 1 to absence. - Exit
2indicates a fatal query problem. Preserve the diagnostic and stop the state conclusion; a database or invocation failure is not absence.
With an installed or held-installed record, retain its actual version exactly. With residual, unpacked, error or otherwise ambiguous state, retain that record rather than pretending it is a fresh installation opportunity. If note redirection itself reports an error, repair neither packages nor the conclusion: stop and reconcile the owned evidence file first.
6. Read cached availability and its origin
apt-cache policy tree reports the named package's installed/candidate
information and version-policy sources from locally acquired metadata.
It can work offline; this query does not refresh repository indexes or prove
that a remote download is currently possible.
printf '%s\n' '--- Cached APT policy: tree ---' >> package-state.txt
apt-cache policy tree >> package-state.txt 2>&1
printf 'apt-cache exit: %s\n' "$?" >> package-state.txt
cat package-state.txt
Require a successfully readable query with no unexpected diagnostic before
interpreting its output. A successful query can still give no usable candidate.
Keep the actual installed value, candidate value and associated source rows;
do not invent a version when a field says (none) or evidence is missing.
| Policy evidence | What to record |
|---|---|
| Installed | Policy's installed-version view; compare it with the detailed dpkg state. |
| Candidate | Current policy choice from local metadata, possibly (none). |
| Version/source rows | The versions, repository URI, release/suite and component shown for them. |
A Candidate can equal the already-installed version with only a
/var/lib/dpkg/status source row. That local database row alone does not
establish that the version is available from an Ubuntu repository.
The course's documented Ubuntu route provides tree through Universe. For
Ubuntu 24.04, the standard source-definition file is
/etc/apt/sources.list.d/ubuntu.sources. Reuse a read-only file inspection
to compare the declared course repository setup with the cached source rows:
cat /etc/apt/sources.list.d/ubuntu.sources
Inspect the expected Ubuntu URI, suite and Components fields, including Universe where required by the checked setup. A cached source row and the current configuration are related evidence, not proof that the remote is reachable or that the cache is fresh. Unexpected configuration, missing Universe evidence or no candidate is a setup condition to reconcile through the declared official route. Do not add a third-party repository or run an index refresh as an improvised fix in this observation lesson.
An installed version and candidate can differ. That difference does not authorize an upgrade or removal. A package named in policy is not thereby installed, and neither field says that a service is running. The later transaction and service lessons make their own fresh checks.
Independent task: choose a branch from two supplied records
Keep your actual note separate from these supplied teaching records. The symbolic version labels below are placeholders for scenario reasoning, not actual package revisions or commands to execute.
| Evidence | Record A | Record B |
|---|---|---|
| Lookup | A guest executable path is found. | No command match is found. |
| Dpkg query | Exit 0; abbreviation [ii ]; full status install ok installed; version INSTALLED_A. | Exit 1 with the explicit single-name no-match diagnostic for tree; no package row. |
| Cached policy | Installed INSTALLED_A; candidate CANDIDATE_A; expected official Ubuntu/Universe source row recorded. | Installed (none); candidate CANDIDATE_B; expected official Ubuntu/Universe source row recorded. |
| Fresh remote download tested? | No. | No. |
Before hints or the answer:
- Identify which record proves an existing installation that must be preserved, and which supports a potential absent-package branch.
- Explain why Record B still does not authorize a transaction now or prove current downloadability. State what the next lesson must recheck.
- Explain how your decision changes if Record A is
[hi ], or if the supposed absent record actually says[rc ]or reports a fatal query error. - Explain why none of these observations proves that a service is running.
Success: interpret both records without changing software, preserve every preexisting installation or ambiguous state, and distinguish actual local state from cached candidate/origin information. Your own note must contain the actual three query sections and their immediate statuses, with no substitution of scenario values for observations.
Graduated hints
- Read the full dpkg status alongside all three abbreviation characters; the query's exit status answers a different question.
- A candidate is selected from cached information. It is not an installation receipt or a live network test.
- Existing, held, residual and failed-query states must be preserved. A clearly absent state is only a possible starting branch for a later separately checked transaction.
Answer and Reasoning
Record A shows a fully installed package. Preserve it, including its actual
version and any existing state, even when the candidate differs. Do not remove
it to manufacture an absent baseline. If its abbreviation is [hi ], it is
still installed and additionally held; no hold change or repair is requested.
Record B combines an explicit no-match database result with policy showing no installed version. That supports the potential absent-package branch, while the official cached candidate identifies a possible source. It does not show current remote reachability or give permission to install in this lesson. Before a later transaction, the next lesson must independently recheck the actual guest, package state, official configuration and current candidate/transaction conditions under its own procedure.
A residual [rc ] record is not the same as a clean no-record state. A fatal
query error does not establish any package state. Preserve those conditions
and reconcile them instead of choosing the ordinary absent branch. If an
installation already exists, any required install/remove practice belongs
on a separately prepared clean owned course baseline; preserving an existing
package is not evidence of having practiced that transaction.
Program lookup, package installation and service execution are distinct. A package can supply commands, libraries or service files; its database state alone does not prove an associated service is currently active. This lesson made no service-state observation and must not claim one.
Read your complete actual note again:
cat package-state.txt
Require the initial header and lookup, database and cached-policy sections, each with actual output/diagnostics and its corresponding immediate status. Explain your own installed/absent/ambiguous conclusion using those fields, and state the cache and origin limits. Do not edit a real failure into the supplied scenario's successful record.
Cleanup and resume without changing package state
After the reasoning and note checks, inspect the known parent entries:
ls -ld ~/linux-practice
ls -ld ~/linux-practice/inspect-package-state
Require the recorded ordinary owned directories, then enter and verify separately:
cd ~/linux-practice/inspect-package-state
pwd
Require the exact recorded exercise path before inspecting the note:
ls -a
ls -ld -- package-state.txt
cat package-state.txt
Require only the recognized owned single-link note and dot entries. Preserve
unknown entries. Establish the actual documented interactive response
convention; shown y/n choices assume English and are not a reason to
guess an unfamiliar affirmative response. Only after those checks:
rm -i -- package-state.txt
If declined, leave the note and stop cleanup. After an accepted removal, inspect the inventory:
ls -a
Require the directory empty, then return and inspect separately:
cd ~/linux-practice
pwd
Require the recorded shared-root path and recheck the recognized owned empty exercise entry before:
ls -ld -- inspect-package-state
Only after accepting that entry:
rmdir -- inspect-package-state
Retain the shared root, packages, repository configuration and preexisting shell declarations. No package or service state was changed to restore.
On interruption, recheck the normal guest, query scope, note identity and
exact location. Inspect complete sections and identify any missing status.
Do not use a later $? as the status of an earlier query. A repeated query
is a new observation: append a clearly labelled new section and capture its
status immediately, or inspect/remove the one recognized note and start a
fresh note after confirming absence. Do not truncate unexplained evidence.
The next lesson rechecks state independently; this note is not a permanent
transaction authorization.
Sources and other study views
- S06 — Canonical package management and Ubuntu repository configuration
- S11 — Ubuntu Noble tree package and Universe availability
- S18 — Bash 5.2 manual: lookup, declarations, quoting and captured status
- S19 — GNU Coreutils manual: owned evidence-note operations
- S28 — Ubuntu Noble apt manual
- S36 — Ubuntu Noble dpkg-query manual
- S52 — Debian Bookworm APT 2.6.1 companion
- S64 — Debian Bookworm apt-cache companion: cached policy
The selected Noble families are APT 2.8 and dpkg-query 1.22; the Bookworm APT companions are 2.6.1. They support the bounded query semantics without claiming identical distribution revisions. Actual tree versions, state, repository rows and query results must come from the learner's observation.
Revision reconstructs the state distinctions; Quick Reference retrieves the read-only query forms.