Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

How to get last installed/removed/upgraded packages?

Share your HowTo, Documentation, Tips and Tricks. Not for support questions!.
Post Reply
Message
Author
jesus92gz
Posts: 121
Joined: 2015-02-06 18:07

How to get last installed/removed/upgraded packages?

#1 Post by jesus92gz »

Hello.

I have just installed a partition in my desktop computer, where I installed Debian stretch/testing. I thought the whole computer was going to explode, but this was not the case, it's very stable, running Linux kernel 4.1. In fact I'm writing from it.

After this intro, I would like to share a fast script I just made. I installed php because I needed php-cli, but it installed lots of dependencies: apache2 and lots of other packages, because I did a mistake and installed php5 package, instead of php5-cli. I could also appreciate some users also had problems like this, so I decided to share it.

You will be required php5-cli in order to run the script. I'm now trying to port it to shell script, using sed and awk instead of preg_* php's functions.

The script will get the last installed/removed/purged/upgraded packages of your system and print them in the standard output.

Usage is pretty simple. You just give +x (execute) permissons to the script, and locate it in some place like /usr/local/bin so any user can execute it.

Code: Select all

# nano /usr/local/bin/apt-last-packages # (commenting) then here paste the script
# chmod +x /usr/local/bin/apt-last-packages
I cannot say if someone else have already uploaded here something like this. If so, please delete and remove this thread.

Thank you

Code: Select all

Usage: apt-last-packages [__TYPE__]
Example: apt-last-packages remove (will get the last packages removed)
Tip: Without arguments, it will get the last packages installed
Example in real life environment: apt-get remove $(apt-last-packages)
This will remove last installed packages
Example in real life environment: apt-get install $(apt-last-packages remove)
This will install last removed packages
apt-last-packages

Code: Select all

#!/usr/bin/php

<?php

// @Author: jesus92gz
// License: Free Software. Modify it and redistribute as you want.
// Publish modified code if you can improve it so I can use it as well.
// I hope you might find it useful.

// Default action will be to search last install
$search = "Install:";

// Checks if parameter is set and sets the search pattern
if (isset($argv[1]))
{
	// Depends on the parameter given to the script, sets a search variable
	switch (strtolower($argv[1]))
	{
		case "install":
			$search = "Install:";
			break;
		case "upgrade":
			$search = "Upgrade:";
			break;
		case "remove":
			$search = "Remove:";
			break;
		case "purge":
			$search = "purge";
			break;
		default:
			$search = null;
			break;
	}
}

// If it was set a parameter not accepted, then display usage.

// If it was set a parameter not accepted, then display usage.
if (!$search)
{
	$usage = "Usage: apt-last-packages [__TYPE__]" . PHP_EOL;
	$usage .= "Example: apt-last-packages remove (will get the last packages removed)" . PHP_EOL;
	$usage .= "Tip: Without arguments, it will get the last packages installed" . PHP_EOL;
	$usage .= "Example in real life environment: apt-get remove $(apt-last-packages)" . PHP_EOL;
	$usage .= "This will remove last installed packages" . PHP_EOL;
	$usage .= "Example in real life environment: apt-get install $(apt-last-packages remove)" . PHP_EOL;
	$usage .= "This will install last removed packages" . PHP_EOL . PHP_EOL;

	echo $usage;
	exit;
}

// Gets last APT log from /var/log/apt/history.log
$lastAptLog = file_get_contents("/var/log/apt/history.log");
// Grep all packages (including info about them, as version and arch) that matches the last command to apt passed as a parameter to this script
$lastInstall = preg_match_all("/$search(.*)/", $lastAptLog, $matches);

// Echo to standard output the matched packages
echo trim(preg_replace("/(:([1-9a-zA-Z]+\s))|(\(([^)]+)\))|(,)/", "", $matches[1][count($matches[1]) - 1]));
TODO
  • Transcript to Shell Scripting language.
    Get a specific package management. eg installation before previous. Just like git diff HEAD~2 or git diff HEAD~x in git.
    Get various installations/uninstallations/upgrades. eg get last 2 removes or installations.

Post Reply