You must have come across tons of indicator apps, applets and widgets which show the power and battery status on Linux. However, keeping these utilities running also mean you are polling these values constantly (almost always from /sys/class/power_supply) and wasting CPU cycles. How to check the power supply and battery status from the cmdline?

upower

upower can show information of both battery and power adapter. Note that upower uses dbus messages and is more resource intensive than the second method we will discuss.

To find out all the power sources attached to your computer, run:

$ upower -e /org/freedesktop/UPower/devices/line_power_ADP1 /org/freedesktop/UPower/devices/battery_BAT1

To check power adapter status:

$ upower -i /org/freedesktop/UPower/devices/line_power_ADP1

To check battery status:

$ upower -i /org/freedesktop/UPower/devices/battery_BAT1

To dump all information:

$ upower -d

Add the following alias to ~/.bashrc to show useful adapter and battery info through upower:

alias pow='upower -i $(upower -e | grep 'ADP') | grep -E "online";upower -i $(upower -e | grep 'BAT') | grep -E "state|time\ to\ empty|percentage"'

Usage:

$ pow online: no state: discharging time to empty: 3.5 hours percentage: 98%

subsystem /sys/class/power_supply/

You can also get similar information from the power_supply subsystem. This is also the preferred way.

For power adapter, try (ADP number might be different for you):

$ cat /sys/class/power_supply/ADP1/uevent

For battery information (BAT number might be different for you):

$ cat /sys/class/power_supply/BAT1/uevent

Useful alias for ~/.bashrc:

alias pow='cat /sys/class/power_supply/ADP1/uevent | grep -E POWER_SUPPLY_ONLINE;cat /sys/class/power_supply/BAT1/uevent | grep -wE "POWER_SUPPLY_STATUS|POWER_SUPPLY_CAPACITY"'

Note that calculating the discharge time is a bit tricky. The following formula gives the value in hours:

(POWER_SUPPLY_ENERGY_NOW / POWER_SUPPLY_POWER_NOW)

Here’s a small script (say pow.sh) to show all the information:

#!/bin/bash online=`cat /sys/class/power_supply/ADP1/online` if [ $online -eq 1 ]; then echo "online: yes" echo "status: " $(cat /sys/class/power_supply/BAT1/uevent | grep -wE "POWER_SUPPLY_STATUS" | cut -f 2 -d '=') echo "capacity: " $(cat /sys/class/power_supply/BAT1/uevent | grep -wE "POWER_SUPPLY_CAPACITY" | cut -f 2 -d '=')% else echo "online: no" echo "status: " $(cat /sys/class/power_supply/BAT1/uevent | grep -wE "POWER_SUPPLY_STATUS" | cut -f 2 -d '=') echo "capacity: " $(cat /sys/class/power_supply/BAT1/uevent | grep -wE "POWER_SUPPLY_CAPACITY" | cut -f 2 -d '=')% energy=`cat /sys/class/power_supply/BAT1/uevent | grep -wE "POWER_SUPPLY_ENERGY_NOW" | cut -f 2 -d '='` power=`cat /sys/class/power_supply/BAT1/uevent | grep -wE "POWER_SUPPLY_POWER_NOW" | cut -f 2 -d '='` life=$(expr $energy / $power) result=$(echo "scale=3;$energy / $power" | bc -l) result=$(echo "$result - $life" | bc -l) result=$(echo "$result * 60" | bc -l) result=`printf '%.0f

' $result` echo "battery life: " $life hrs $result minutes fi

Output:

$ pow online: no status: Discharging capacity: 99% battery life: 4 hrs 13 minutes