Know which version dpkg/apt considers more recent

dpkg --compare-versions 1.0-2ubuntu5 lt 1.1-1~raphink3 && echo y || echo n
Compares two versions with dpkg. It is not always obvious what version dpkg/apt will consider to be more recent. Operators include the following : * These treat an empty version as earlier than any version: lt le eq ne ge gt. * These treat an empty version as later than any version: lt-nl le-nl ge-nl gt-nl. * These are provided only for compatibility with control file syntax: < > >. This command doesn't output anything. It only returns with status 0 or 1, hence the echo "y" || echo "n" to get an output.
Sample Output
y

2
By: raphink
2009-02-28 18:01:59

These Might Interest You

  • Need admin right to run dpkg-query Show Sample Output


    2
    dpkg-query -Wf '${Package} - ${Version}\n' | sort -n
    Nicolargo · 2012-08-22 10:53:48 1
  • If the first two letters are "ii", then the package is installed. You can also use wildcards. For example, . dpkg -l openoffice* . Note that dpkg will usually not report packages which are available but uninstalled. If you want to see both which versions are installed and which versions are available, use this command instead: . apt-cache policy python Show Sample Output


    1
    dpkg -l python
    hackerb9 · 2011-01-05 06:15:13 1
  • Removes the useless "grep" step present in many other solutions and tries to be as short and as fast as possible. A more elegant solution but longer and spawning one more process would be: dpkg -l | awk '/^rc/ { print $2 }' | xargs dpkg -P Show Sample Output


    0
    dpkg -P $(dpkg -l | awk '/^rc/ { print $2 }')
    adelyne · 2016-10-24 21:37:08 0
  • Recently in Debian Wheezy the dpkg command refuses to work with wildcards, so this is the one-liner alternative.


    0
    sudo dpkg -P $(sudo dpkg -l yourPkgName* | awk '$2 ~ /yourPkgName.*/' | awk '$1 ~ /.i/' | awk '{print $2}')
    woohoo · 2014-08-02 18:14:02 1

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: