Skip to main content
edited body
Source Link
Faheem Mitha
  • 36.1k
  • 33
  • 131
  • 192
$ dpgkdpkg -l Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) ||/ Name Version Architecture Description +++-===========================================================-==================================-============-======================================================================== ii adduser 3.113+nmu3 all add and remove users and groups ii apt 0.9.7.8+rpi1 armhf commandline package manager ii apt-utils 0.9.7.8+rpi1 armhf package managment related utility programs ii aptitude-common 0.6.8.2-1 all architecture indepedent files for the aptitude package manager ii atmel-firmware 1.3-4 all Firmware for Atmel at76c50x wireless networking chips. $ dpkg -l | grep '^ii ' 
$ dpgk -l Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) ||/ Name Version Architecture Description +++-===========================================================-==================================-============-======================================================================== ii adduser 3.113+nmu3 all add and remove users and groups ii apt 0.9.7.8+rpi1 armhf commandline package manager ii apt-utils 0.9.7.8+rpi1 armhf package managment related utility programs ii aptitude-common 0.6.8.2-1 all architecture indepedent files for the aptitude package manager ii atmel-firmware 1.3-4 all Firmware for Atmel at76c50x wireless networking chips. $ dpkg -l | grep '^ii ' 
$ dpkg -l Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) ||/ Name Version Architecture Description +++-===========================================================-==================================-============-======================================================================== ii adduser 3.113+nmu3 all add and remove users and groups ii apt 0.9.7.8+rpi1 armhf commandline package manager ii apt-utils 0.9.7.8+rpi1 armhf package managment related utility programs ii aptitude-common 0.6.8.2-1 all architecture indepedent files for the aptitude package manager ii atmel-firmware 1.3-4 all Firmware for Atmel at76c50x wireless networking chips. $ dpkg -l | grep '^ii ' 
Source Link

Starting from @derobert's answer, I worked my way to getting exactly the current version of all packages to reinstall.

Short version:

sudo dpkg -l | grep '^ii ' | sed 's/ */\t/g' |cut -f 2,3 | sed 's/\t/=/' | xargs apt-get install --reinstall -y --ignore-missing 

Explained:

The key is actually specifying the required version of each package.

The general command is:

apt-get install --reinstall <package>=<version> 

Breaking down the long command line:

$ dpgk -l Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) ||/ Name Version Architecture Description +++-===========================================================-==================================-============-======================================================================== ii adduser 3.113+nmu3 all add and remove users and groups ii apt 0.9.7.8+rpi1 armhf commandline package manager ii apt-utils 0.9.7.8+rpi1 armhf package managment related utility programs ii aptitude-common 0.6.8.2-1 all architecture indepedent files for the aptitude package manager ii atmel-firmware 1.3-4 all Firmware for Atmel at76c50x wireless networking chips. $ dpkg -l | grep '^ii ' 

...gets rid of the header lines and a few packages with status 'hold' (marked as hi instead of ii)

$ dpkg -l | grep '^ii ' | sed 's/ */\t/g' 

... converts any number of spaces to a single TAB character, preparing the ground for cut.
(Btw: why, oh why, doesn't sed support x+ regex for "character x, one or more times"? It can be emulated with xx* - meaning 'x' once followed by 'x' zero or more times)

The output looks like this:

ii adduser 3.113+nmu3 all add and remove users and groups ii apt 0.9.7.8+rpi1 armhf commandline package manager ii apt-utils 0.9.7.8+rpi1 armhf package managment related utility programs ii aptitude-common 0.6.8.2-1 all architecture indepedent files for the aptitude package manager ii atmel-firmware 1.3-4 all Firmware for Atmel at76c50x wireless networking chips. 

Next:

$ dpkg -l | grep '^ii ' | sed 's/ */\t/g' | cut -f 2,3 | sed 's/\t/=/' 

...gets the name and version of each package (the 2nd and 3rd fields), and replaces the tab that separates them with an '='

adduser=3.113+nmu3 apt=0.9.7.8+rpi1 apt-utils=0.9.7.8+rpi1 aptitude-common=0.6.8.2-1 atmel-firmware=1.3-4 

Finally, pipe each of the above to apt-get as a long list of arguments using xargs.

Notice the parameter --ignore-missing - this command is run as 'best effort' - I don't want the updating to stop because some packages are not available to reinstall (those will stay unmodified)

$ dpkg -l | grep '^ii ' | sed 's/ */\t/g' |cut -f 2,3 | sed 's/\t/=/' | xargs apt-get install --reinstall --ignore-missing 

While testing, I also added a --dry-run argument to apt-get.