Both answers already provided have their pros and cons.
Starting with debfoster
gives a list of packages which is simple to parse, so the following gives the requested result:
apt-cache policy $(debfoster -q -d tcpdump|tail -n +2)|awk '/^[^ ]/ { package=$0 } / Installed/ { print package " " $2 }'
using tail
to skip the first line and awk
to process the result in a single operation. (Using a command substitution avoids the need to process newlines.) Starting with debfoster
means we can only do this with a package which is already installed, so we can then use dpkg
to provide more information:
dpkg -l $(debfoster -q -d tcpdump|tail -n +2)
Starting with apt-rdepends
gives a list of packages which is a little harder to process, with duplicates; but it has the advantage of being able to process packages which aren't yet installed:
apt-cache policy $(apt-rdepends -p tcpdump 2>| /dev/null|awk '/Depends/ {print $2}'|sort -u)|awk '/^[^ ]/ { package=$0 } / Installed/ { print package " " $2 }'
This can also be used with dpkg -l
:
dpkg -l $(apt-rdepends -p tcpdump 2>| /dev/null|awk '/Depends/ {print $2}'|sort -u)
but this requires that dpkg
know about all the packages involved, which may not be the case if the package being processed isn't installed.
debfoster
includes Recommends
by default; this can be disabled using --option UseRecommends=no
:
debfoster -q --option UseRecommends=no -d tcpdump
apt-rdepends
doesn't include Recommends
by default; this can be enabled using -f Depends,PreDepends,Recommends -s Depends,PreDepends,Recommends
:
apt-rdepends -f Depends,PreDepends,Recommends -s Depends,PreDepends,Recommends -p tcpdump
although it doesn't give all the dependencies debfoster
finds in that case. (For example debfoster
finds that tcpdump
depends on apt
via libssl1.0.0
, debconf
and apt-utils
, but apt-rdepends
doesn't.)
A slight variation on Installing packages by importing the list with dpkg --set-selections should do the trick.
Save the list of packages on your reference system:
dpkg --get-selections > packages.lst
Then install packages based on that list on your target system, after updating the list of available packages:
dpkg --merge-avail <(apt-cache dumpavail)
dpkg --clear-selections
dpkg --set-selections < packages.lst
apt-get dselect-upgrade
Best Answer
dpkg -l
will always give the correct list of installed (or removed but still configured) packages. It actually usesdpkg-query
to do its job, and that references the contents of/var/lib/dpkg/status
which is the reference for installed packages.apt
uses the same information, but until recently didn't provide an easy way to list packages;apt list
now does that, and you'll get the same results as given bydpkg -l
(in a different format).dpkg
can be used to list all packages. If all the software on a system is installed using packages, then it will list all the software; but if any software has been installed manually, without a package, then it won't know about it.