list of installed packages without using apt?

Stuart McGraw smcg4191 at mtneva.com
Wed Nov 29 06:22:37 UTC 2017


On 11/28/2017 06:02 AM, Tom H wrote:
> On Mon, Nov 27, 2017 at 10:30 PM, Xen <list at xenhideout.nl> wrote:
>> Stuart McGraw schreef op 27-11-2017 23:18:
>>>
>>> Is there some way of getting output similar to 'apt list --installed'
>>> without using apt? Specifically I want a list of installed packages,
>>> their version numbers, and whether they were installed at my request
>>> or as a dependency. dpkg-query provides the first two items and I
>>> think apt-mark the last, but the problem is combining them.
>>>
>>> I dont want to use apt because: 1) I am using in a shell script and
>>> apt prints an annoying warning, 2) The warning recommends against
>>> using apt in scripts :-) I want to use basic low-level commands
>>> and not large complex things like apt or aptitude.
>>
>> The problem is more apt being annoying than apt being bad.
>>
>> If you wanted to combine dpkg and apt-mark you would get something like:
>>
>> dpkg -l | grep "^ii" | awk {'print $2'} | while read name; do
>> # now process version string
>> version=${name#*-}
>> name=${name%-*-*}
>> apt-mark showmanual | grep -Fx "$name" && manual=yes || manual=
>> done
> 
> You can combine "grep "^ii" | awk {'print $2'}" into "awk '/^ii/ {print $2}'".
> 
> But this is slow and only lists the package names.
> 
> You have to use an apt-related tool because dpkg-related tools don't
> keep track of automatically-installed packages.
> 
> To list installed packages, their version numbers, and whether they
> were installed automatically or not, you can use (in two steps to
> avoid the slowness above but without an indication of manual/automatic
> installation)
> 
> dpkg-query -W -f '${version} ${package}\n' $(apt-mark showmanual)
> dpkg-query -W -f '${version} ${package}\n' $(apt-mark showauto)
> 
> aptitude isn't large and complex. You can use
> 
> aptitude search -F "%p %v# %M" ~i
> 
> which lists all installed packages, with the lines of those installed
> automatically appended with an "A".

I think the answer to my question, "is there any low-level command that
will give me installed package info and auto/manual status?" turns out 
to be "no". 

The options as I understand are:
1. Extract the info from /var/lib/apt/lists and /var/lib/dpkg/info
2. Combine the outputs of apt-mark and dpkg-query (or related  commands)
3. Get the info from aptitude or app

Seems like the best option for me is 2, a variation on Xen/TomH's ideas:
  (dpkg-query -W -f '${package} ${version}\n' $(apt-mark showauto)|sed -e 's/$/ A/'; \
  dpkg-query -W -f '${package} ${version}\n' $(apt-mark showmanual)|sed -e 's/$/ M/';) | sort

While I like Ubuntu so far, I have to say that package management seems
a bit of a hodge-podge.  Not really a criticism since everything seems to 
work ok but takes a while to learn one's way around. :-)

Thanks very much for all the ideas!





More information about the ubuntu-users mailing list