Show the percentage of battery remaining in your laptop from the Linux command line.
We’ll use the upower
app to list out all the batteries your laptop has
installed. Most laptops these days have just one but my ThinkPad T460s has dual
batteries as you can see below:
upower -e
/org/freedesktop/UPower/devices/line_power_AC
/org/freedesktop/UPower/devices/battery_BAT0
/org/freedesktop/UPower/devices/battery_BAT1
/org/freedesktop/UPower/devices/DisplayDevice
The file which contains the current level of your battery is stored at
/sys/class/power_supply/BAT0/capacity
so we can simply cat
out the figure
into the terminal:
cat /sys/class/power_supply/BAT0/capacity
My use case for getting the battery level like this was because the standard ‘power manager applet’ that comes with Cinnamon likes to freak out on me every now and then. Sometimes it shows the percentage remaining with a dozen decimal places which makes a mess of the panel. Other times it tells me I have a hundred hours of battery remaining and as much as I want that to be true, it ain’t true.
You can use this technique with ZimiZone’s Command result Desklet, just go into the desklet’s settings and add it as a command. It’ll also work with schorschii’s Battery Level Indicator Desklet by going into the settings and adding the path into the ‘Path to battery capacity file’ option (you don’t need the ‘cat’ in this case, just the path will do it).
Check out all the files in /sys/class/power_supply_BAT0
if you need the
battery status (ie charging, full, discharging etc), voltage, model, and a bunch
of other battery related stats.
ls /sys/class/power_supply/BAT0
alarm cycle_count hwmon2 present type
capacity device manufacturer serial_number uevent
capacity_level energy_full model_name status voltage_min_design
charge_start_threshold energy_full_design power subsystem voltage_now
charge_stop_threshold energy_now power_now technology