How to find the number of packages needing update from the command line?

To obtain that output, you can use the command

sudo /usr/lib/update-notifier/update-motd-updates-available

or, if you don't want to use sudo,

cat /var/lib/update-notifier/updates-available

Explanation

The login application shows the output found in the file /etc/motd, that is a symbolic link to /var/run/motd.

This last file is updated by the mounted-varrun service (see /etc/init/mounted-varrun.conf) invoking all scripts in /etc/update-motd.d/, and in particular

/etc/update-motd.d/90-updates-available

that in turn calls the script

/usr/lib/update-notifier/update-motd-updates-available

this script executes various actions, and at last writes the output to the text file

/var/lib/update-notifier/updates-available

EDIT

Regarding the reboot part of the question, run this command

/usr/lib/update-notifier/update-motd-reboot-required

it will give no output if reboot is not required.


Why can't you just run this?

/usr/lib/update-notifier/apt-check --human-readable

That's what /usr/lib/update-notifier/update-motd-updates-available does to collect the information, at least in the version of Ubuntu I'm using (12.10).


I also search for a scripred method for a update check inside minimal docker containers, when I found a comment in /usr/lib/update-notifier/apt-check:

apt-get -s -o Debug::NoLocking=true upgrade | grep ^Inst

This allows a scripted update check, without the need to install the update-notifier-common package