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:

  • 2
    What is "some available packages"? – A.B. Jan 3 '16 at 19:16
  • Sorry, missed that. I added the list to the OP. Thanks. – edesz Jan 3 '16 at 19:33
  • You're missing the STDIN / STDOUT redirections: done < input > Output.txt. – kos Jan 3 '16 at 20:30
  • I rolled back your question to the previous version since it doesn't really make sense to edit for a copy-paste error, the fix is just copying muru's script exactly as it's written. – kos Jan 3 '16 at 20:33
  • The apt-show-versions command seems to come close to what you want – steeldriver Jan 3 '16 at 21:46

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
  • I have 2 questions: a. Is the above run from the terminal, or from a separate script? b. So, input.txt would like python-wheel apache2 etc. etc....each on a new line? – edesz Jan 3 '16 at 19:35
  • @WR a. you can do it either way b. input would have python-wheel on one line, mysql-server on the next, etc. – muru Jan 3 '16 at 19:36
  • Thanks. I tried this both from a bash script file and directly from the terminal but I am getting an error message. I added this to the original post (Attempt 2) - all the error messages and output is there. Any thoughts about what might be causing this? – edesz Jan 3 '16 at 20:30
  • @WR Have a closer look at my code. You'll notice you're missing a < and a >. – muru Jan 3 '16 at 20:32
  • Sorry, just want to clarify: Did you mean < input.txt >? Also, the echo line - should that be indented if I'm putting this in a *.txt file? – edesz Jan 3 '16 at 20:35

After some wrangling with the aptitude documentation, including this Debian aptitude search term reference, I managed to come up with this ugly one-liner

aptitude -F '%p %V' --group-by=none --sort=name,~version versions \
  ?exact-name\({python-numpy,mysql-server,python-six,python-wheel,apache2,python-urllib3,python-setuptools}\) \
  | sort -uk1,1
  • -F '%p %V' formats the output as package name and then candidate version
  • --sort=name,~version sorts by package name and version descending so that we can use sort -u to skim off the newest
  • ?exact-name() prevents the search from being expanded to include things like mysql-server-5.5 for mysql-server

Note the ugly brace expansion for the list of packages to check: there doesn't seem to be an option like -oAptitude::Some::Param=true to enforce ?exact-name() globally.

FWIW the apt-show-versions utility seems to get 90% of the way there with 10% of the effort: here are the results for the packages you mention on my 14.04.3 system:

$ aptitude -F '%p %V' --group-by=none --sort=name,~version versions \
  ?exact-name\({python-numpy,mysql-server,python-six,python-wheel,apache2,python-urllib3,python-setuptools}\) \
  | sort -uk1,1
apache2 2.4.7-1ubuntu4.8                                                        
mysql-server 5.5.46-0ubuntu0.14.04.2                                            
python-numpy 1:1.8.2-0ubuntu0.1                                                 
python-setuptools 3.3-1ubuntu2                                                  
python-six 1.5.2-1ubuntu1                                                       
python-urllib3 1.7.1-1ubuntu4                                                   
python-wheel 0.24.0-1~ubuntu1                                                   


$ apt-show-versions python-numpy mysql-server python-six python-wheel apache2 python-urllib3 python-setuptools
apache2 not installed
mysql-server:all/trusty-security 5.5.46-0ubuntu0.14.04.2 uptodate
python-numpy:amd64/trusty-updates 1:1.8.2-0ubuntu0.1 uptodate
python-setuptools:all/trusty-updates 3.3-1ubuntu2 uptodate
python-six:all/trusty-updates 1.5.2-1ubuntu1 uptodate
python-urllib3:all/trusty-updates 1.7.1-1ubuntu4 uptodate
python-wheel:all/trusty-updates 0.24.0-1~ubuntu1 uptodate

The most obvious difference is it apparently doesn't include packages that are not currently installed.

  • Thank you! My main concern is that it works. The aptitude approach would be the better option here then. Why do you have sort -uk1,1 rather than just sort-u? – edesz Jan 3 '16 at 22:38
  • @WR the k1,1 is so that sort only looks at the first field (i.e. the package name) when eliminating duplicates. At least I think that's how it works. – steeldriver Jan 3 '16 at 22:44
  • You could use ?or() instead of shell expansion, but that tends to make aptitude slow, so using regexes is prefered. – Braiam Jan 4 '16 at 4:28
  • @Braiam thanks - do you know what the syntax of an ?or() version would be? I tried that but couldn't seem to get it right – steeldriver Jan 4 '16 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.