brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: dpkg-query -W -f prints exactly the package fields you ask for

Linux
~/tips/tip-apt-dpkg-query-showformat

dpkg-query -W -f='${Package}\t${Version}\n' 'openssh-*'

Prints chosen fields of installed packages, such as name and version, in a format scripts can read.

The output of dpkg -l is made for reading: the manual says its format varies to fit the terminal width and is not easily machine-readable. For a script I use dpkg-query and choose the fields myself.

dpkg-query -W (--show) lists packages that match a pattern. By default it prints one line per package: the name and the installed version, separated by a tab. With -f (--showformat) you give a format string, and fields are written as ${Field}:

dpkg-query -W -f='${Package}\t${Version}\n' 'openssh-*'

In the format string a backslash starts an escape: \n is a newline and \t a tab. The manual says the format is output for each listed package, so I add the \n myself. A width is possible after a semicolon, and a negative width left-aligns, which lets me build a tidy table:

dpkg-query -W -f='${Package;-30} ${Version}\n' 'linux-image-*'

The manual also lists some special fields. binary:Package is the name with an architecture qualifier when that is needed to be unambiguous, binary:Synopsis is the short description and db:Status-Abbrev is the three-letter status such as ii. A list of installed packages with their short description:

dpkg-query -W -f='${binary:Package}: ${binary:Synopsis}\n' | less

Another field in the list is Installed-Size. Sorting on it shows the biggest installed packages:

dpkg-query -W -f='${Installed-Size}\t${Package}\n' | sort -n | tail -n 10

I would treat the number as a rough size as dpkg has it recorded. The manual lists the field but I did not look up the unit, so do not copy it into a report as megabytes.

The caveat is the quoting. Use single quotes, otherwise the shell tries to expand ${Package} itself and you get empty output.

Further reading: dpkg-query(1): –show and –showformat

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link