I usually connect to my laptop using SSH from my workstation. At times I need to check the battery remaining on the laptop. In order to do this I need to remote desktop into the laptop. Since this is a small task, I don’t want to remote desktop to the laptop every time.
There is no doubt that the Linux Terminal is very powerful. In this case as well I can get the battery information right from the terminal. Upower is what we will use for this task.
Executing the above code will enumerate and list the power devices attached to the computer. Here is what was returned on my laptop.
As highlighted battery_BA0 is the battery attached to my laptop.
upower -i /org/freedesktop/UPower/devices/battery_BAT0
On executing the following command, complete information related to the battery is returned.
pradeep@workhorse ~ $ upower -i /org/freedesktop/UPower/devices/battery_BAT0
model: ASUS Battery
power supply: yes
updated: Thursday 16 April 2017 05:55:55 PM IST (2 seconds ago)
has history: yes
has statistics: yes
energy: 16.188 Wh
energy-empty: 0 Wh
energy-full: 37.475 Wh
energy-full-design: 38 Wh
energy-rate: 9.059 W
voltage: 7.6 V
time to empty: 1.8 hours
1492096313 43.000 discharging
Since I am interested in only the battery remaining percentage, I will GREP the output. So the command would like this.
upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "state| percentage"
and here I get the output that I needed, directly from the terminal!