List currently installed unstable packages (Gentoo Linux)

I accidentally removed my package accept_keywords file. There were only a few packages which I needed from ~amd64, but I can't remember them all. I could recover them if I had the list all the currently installed unstable packages. How do I do that?


Solution 1:

We will use equery command to get this done.

equery y "PACKAGE NAME" will output information about package with keywords data. Unstable packages are marked as ~. So we need to write a script which will iterate list of installed packages and output if it is stable or not.

Output of single equery y "PACKAGE" looks like this:

test@test ~ $ equery y "portage"
Keywords for sys-apps/portage:
                 |                             | u   |  
                 | a a   a           p     s   | n   |  
                 | l m   r h i m m   p s   p   | u s | r
                 | p d a m p a 6 i p c 3   a x | s l | e
                 | h 6 r 6 p 6 8 p p 6 9 s r 8 | e o | p
                 | a 4 m 4 a 4 k s c 4 0 h c 6 | d t | o
-----------------+-----------------------------+-----+-------
[M]2.1.6.7_p1    | + + + o + + + ~ + + + + + + | # 0 | gentoo
    2.1.11.62    | + + + o + + + ~ + + + + + + | #   | gentoo
     2.1.12.2    | + + + o + + + ~ + + + + + + | #   | gentoo
        2.2.1    | + + + o + + + ~ + + + + + + | #   | gentoo
        2.2.6    | ~ ~ ~ o ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ | #   | gentoo
     [I]2.2.7    | + + + + + + + ~ + + + + + + | o   | gentoo
        2.2.8    | ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ | #   | gentoo
        2.2.8-r1 | ~ ~ ~ ~ + ~ ~ ~ ~ ~ ~ ~ ~ ~ | o   | gentoo
         9999    | o o o o o o o o o o o o o o | o   | gentoo

From this output we need to get row with [I] which means the package installed. Next we need to output keyword for your architecture (let's say it is amd64 - second column of the table). For this we can use awk (AWK's Manual page for reference). For single package it will look like this:

equery y "portage" | awk '/[I]/{print $4;}'

and output will be

+

while for unstable package the output would be

~

Now we need to do it with all installed packages. This script will help us:

#!/bin/bash

ALL_PKG=`equery list "*"`

for PKG in $ALL_PKG
do

 echo $PKG
 equery y "$PKG" | awk '/[I]/{print $4;}'

done

You need to adjust the awk's print number (awk '/[I]/{print $4;}') based on yours system architecture.

There are some exceptions which need to be handled individually. For example package winetricks gives following output:

test@test ~ $ equery y "winetricks"
Keywords for app-emulation/winetricks:
         |                             | u   |  
         | a a   a           p     s   | n   |  
         | l m   r h i m m   p s   p   | u s | r
         | p d a m p a 6 i p c 3   a x | s l | e
         | h 6 r 6 p 6 8 p p 6 9 s r 8 | e o | p
         | a 4 m 4 a 4 k s c 4 0 h c 6 | d t | o
---------+-----------------------------+-----+-------
20130629 | o ~ o o o o o o o o o o o ~ | # 0 | gentoo
20130707 | o ~ o o o o o o o o o o o ~ | o   | gentoo
99999999 | o o o o o o o o o o o o o o | o   | gentoo

There is no [I] symbol here, so awk will not output anything. If you will see no output or strange output for package - inspect it manually. There should be very small amount of such packages.

This version also outputs the list of unstable packages to unstable.txt

#!/bin/bash
ALL_PKG=`equery list "*"`

for PKG in $ALL_PKG
do

  echo $PKG
  export stb=$(equery y "$PKG" | awk '/[I]/{print $4;}' 2>&1)
  echo $stb
  if [[ "$stb" == "~" ]]; then echo $PKG >> unstable.txt; fi

done

Solution 2:

Solution using eix, but much much faster, then the solution using equery

eix -c --installed-unstable

If you need just names of packages, jsut add --only-names option.