List names of all packages starting with a specific word

Say I want to install all the ubuntu-wallpapers packages, so I would enter the following at the terminal:

sudo apt install ubuntu-wallpapers*

I am trying to get the same effect using

dpkg --set-selections < ./pkgs.txt

The problem is, this method does not support the * wildcard in the pkgs.txt file.

i was able to get the exact list of packages using

apt-cache search ^ubuntu-wallpapers

but I get the descriptions too. Is there a way to get only the package names so I can redirect the output to the pkgs.txt file?


Solution 1:

You can use apt-cache pkgnames to list only the names of all packages starting with a specific prefix:

$ apt-cache pkgnames ubuntu-wallpaper
ubuntu-wallpapers-karmic
ubuntu-wallpapers-vivid
ubuntu-wallpapers-maverick
ubuntu-wallpapers-utopic
ubuntu-wallpapers-wily
ubuntu-wallpapers-quantal
ubuntu-wallpapers-raring
ubuntu-wallpapers-precise
ubuntu-wallpapers-lucid
ubuntu-wallpapers-natty
ubuntu-wallpapers
ubuntu-wallpapers-trusty
ubuntu-wallpapers-oneiric
ubuntu-wallpapers-saucy
ubuntu-wallpapers-xenial

See man apt-cache for more info.

Alternatively, you could process the output of apt-cache search and display only the first column by piping it e.g. through one of these commands below or anything similar:

  • cut -d' ' -f1
    
  • grep -Eo '^\S+'
    
  • sed 's/\s.*//'
    
  • awk '{print $1}'
    

Solution 2:

Alternative using apt list and egrep:

  • Search for a group of packages

    apt list --installed | grep -o "^[^/]*" | egrep "^(gcc|make|git)$"
    
  • Search for a group of packages starting with

    apt list --installed | grep -o "^[^/]*" | egrep "^(gc|mak)"
    

The command in the middle is to get just the package name. Also works with cut -f1 -d"/".

If given a list of packages you want to know which ones you don't have already installed:

pkglist="libcurl4-openssl-dev libsdl2-dev libopenal-dev libvulkan-dev libgl1-mesa-dev clang gcc make git"

comm -13 <(apt list --installed | cut -f1 -d"/" | sort) <(echo $pkglist | tr " " "\n" | sort)

For the first parameter, you can replace with apt-mark showmanual to compare to the manually installed packages.

Beware:

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.