Ubuntu – How to pipe installed packages to apt-cache


I'm trying to get package description of my installed packages.

It would look like this:

sudo apt list --installed | sed 's/\/.*//' | apt-cache search {} \;

The problem is to send each line from sed to apt-cache.
Any suggestion ?

Best Answer

The {} \; syntax that you have guessed is specific to the find command and won't work in this context. You could use xargs ex.

apt list --installed | sed 's/\/.*//' | xargs -n1 apt-cache search

Actually xargs has its own {}-like syntax that you could use here, i.e.

. . . | xargs -n1 -I{} apt-cache search {}

although it is redundant in this case (it's useful for cases where you want to substitute the piped value at a point other than the end of the command).

However the apt-cache search command is almost certainly not what you want here - it will search the entire (online) catalog for packages that match each of the results of apt list --installed. Even if you restrict the scope of the search using --names-only you will still get many false positives (i.e. substring matches between packages that ARE installed and packages that AREN'T). It will also be horribly inefficient.

For this particular task I'd skip apt altogether and use the lower-level dpkg-query command ex.

dpkg-query -W -f='${binary:Package}: ${binary:Summary}\n'

which will be much more efficient (only traverse the package list once) and will be strictly limited to the local package catalog.

If you want to limit the output to packages that are currently installed, add the db:Status-Abbrev field and filter on that e.g.

dpkg-query -W -f='${db:Status-Abbrev}\t${binary:Package} - ${binary:Summary}\n' | 
  awk '/^ii/'


dpkg-query -W -f='${db:Status-Abbrev}\t${binary:Package} - ${binary:Summary}\n' | 
  awk -F'\t' '/^ii/ {print $2}'