How can I get a list of all packages in a repository section from command line?

Solution 1:

Make sure the dctrl-toolsInstall dctrl-tools package is installed. It provides useful commands for searching the apt and dpkg package lists. To get a full description of all packages from a particular section that are installable with apt, run

grep-aptavail -F Section hamradio

This will show the full package metadata for every package in the hamradio section. If all you want to see are the package names, run

grep-aptavail -n -F Section -s Package hamradio

If your system is set up for multiarch, the same package may show up more than once in this listing if it is built for more than one architecture. So to refine this further, use either

grep-aptavail -n -F Section -s Package hamradio | sort | uniq

or

grep-aptavail -n -F Section -s Package hamradio | sort -u

to sort the package list and remove duplicate packages with the same name.

Note that you will have to use the actual name of the section, which is different from the "human-readable" name that Synaptic shows in its GUI. For example, the searches above use the section name hamradio instead of the string "Amateur Radio" shown in Synaptic.

See the man page for grep-aptavail for a full description of all options and some examples.

Solution 2:

Well, though you say you don't want to use aptitude because of the output, you need to know that you can modify it to get what you like:

aptitude -F'|%p|%d|' search '?section(hamradio)'

The trick is in the -F switch that modifies the output format. %p means package. This also outputs when package has various architectures (ie amd64 vs i386), and %d which outputs the description. You can personalize the search pattern even more to for example not installed packages:

aptitude -F'|%p|%d|' search '?section(hamradio) !~i'

where ~i means installed and the ! is a not, so it reads as "not (!) installed (~i)", or if you only want the ones that are available to your architecture:

aptitude -F'|%p|%d|' search '?section(hamradio) ~r native'

~r being ?architecture() which matches the architecture of the package and native which lists only the ones that have the same architecture as the system, the equivalent to dpkg --print-architecture.

The previous line can therefore be written even more concisely as:

aptitude -F'|%p|%d|' search '~s hamradio ~r native'

Solution 3:

More fields on a single line, with arbitrary separator

The following one-liner will print all unique package names of a repository section, together with their description, each on a single line. All fields are separated by a pipe character, i.e. ready for conversion into a Markdown pipe table. The resulting table can be found on my web site.

grep-aptavail -n -s Package,Description -F Section hamradio |paste -sd '||\n' |sed 's:^:|:' |sort -u