Ubuntu – Get name and version of available packages to text file – name and version only on same line, per package

aptcommand line

I am looking to get a text file with a list of some available packages and their latest version. I need to have the package name and its latest version on the same line.


Currently, I can get a version number for a single package:

example 1:

apt-cache show python-numpy | grep Version

This gives me 1 line of output:

Version: 1:1.8.2-2

example 2:

apt-cache show mysql-server | grep Version

This gives me 1 line of output:

Version: 5.5.44-0+deb8ul


There is more than one package that I am interested in.


What I am looking for is the package name and the version – I would want this format:

python-numpy 1.8.2-2
mysql-server 5.5.44-0
python-six 1.8.0-1
python-wheel 0.24.0-1
apache2 2.4.10-10
python-urllib3 1.9.1-3
python-setuptools 5.5.1-1

If possible, I would like these 6 lines to be written to 'Output.txt'.
If it is not possible to get all these lines in the same file, then I would like to only get the 1st line above – python-numpy 1.8.2-2 – in the output file.


Currently, I can get the name and version of each of these packages individually. However, is there a way to automate this process and get all at once?


The only packages I am looking for are:


Best Answer

If you have a list of packages (say, in a file named input, one per line), then you can do:

while read package
    echo $package "$(apt-cache show $package | awk '/Version/{print $2}')"
done < input > Output.txt