1

I'm very new to ubuntu and I'm trying to find a command that tells what packages and the versions are installed on my "sandbox" I'm looking to bundle this as a output file

Kenny
  • 13

3 Answers3

1

I know you can use dpkg --get-selections | awk '{print $1}' to see all installed packages. but not sure about the versions i'll let you know if i find somthing else

1

Your question may be marked as a duplicate because you're expected to do some of your own research before posting on StackExchange sites. But, because the possible duplicate (How to list all installed packages) doesn't touch on version info, here's your answer:

dpkg -l | grep "^ii" | awk '{print $2,$3}'

or

dpkg -l | awk '/^ii/ {print $2,$3}'

Decoded:

dpkg -l list all packages, including ones which have been removed.

grep "^ii" print only lines that start with "ii" (to exclude packages which have been removed, usually marked "rc"). Note that installed packages may not always be in "ii" status. This command will check: dpkg -l | grep -v "^ii" | grep -v "^rc" | tail -n +6

awk '{print $2,$3}' print the second and third columns (package name and version, respectively).

p.s. I wrote some comments about this before but my code was wrong.

wjandrea
  • 14,504
1

Although selecting fields from the output of dpkg -l certainly works, the more fundamental dpkg-query command allows the output fields and format to be customized without resorting to additional text processing tools. As it happens, plain

dpkg-query -W

with no explicit format string gives exactly a tab-separated list of package names and versions (equivalent to dpkg -l | awk '{print $2,$3}') as noted in man dpkg-query:

-W, --show [package-name-pattern...]
       Just like the --list option this will list all packages matching
       the  given  pattern.  However the output can be customized using
       the --showformat option.  The default output  format  gives  one
       line  per  matching package, each line having the name (extended
       with the architecture qualifier for  Multi-Arch  same  packages)
       and installed version of the package, separated by a tab.


If you want a prettier output more akin to that of dpkg -l you could use something like

dpkg-query -W -f='${binary:Package;-25}\t${Version}\n'

to left-justify the package names in a field of width 25 columns, or

dpkg-query -W -f='${db:status-abbrev}\t${binary:Package;-25}\t${Version}\n'

to include the ii etc. status flags at the start of each line.

steeldriver
  • 142,475