Skip to content

How to Check a Linux Laptop’s Battery From the Command Line

Linux laptop showing a bash prompt
fatmawati achmad zaenuri/Shutterstock.com

Laptop computers let you work wherever you want. Well, just as long as there is life in your laptop’s battery. Here’s how to check your battery on the Linux command line.

The Laptop Battery

Unplugged from a mains AC adapter, your laptop is completely dependent on its battery for everything. Powering the screen, using the hard drives, accessing Wi-Fi, and reading user input all come to a jarring halt if your battery isn’t up to the job.

Manufacturers can’t agree on whether leaving a laptop plugged in all the time is a good thing or bad. If you don’t want to do that, then your laptop usually won’t be at 100% battery charge when you do go out with it.

Batteries decline over their usable life, too. So an older battery isn’t able to retain the same charge that it did when it was new. And it is very unlikely that even when it was new, you could get what the manufacturer claimed in its advertising.

Knowing you have to keep an eye on the battery charge is part and parcel of using a laptop of course. That’s nothing new. But what if you need to check the battery from the command line?

Perhaps you’re out and remotely connecting to a laptop in your home using SSH, and you can’t remember if it is plugged in or running on its battery. Maybe you use a laptop as a GUI-less server or with a tiled window manager and you don’t have an on-screen display of battery charge.

Being able to find out the power status of your laptop at the command line means you can include those techniques in scripts, too.

Checking Your Battery With upower

The upower command can be used to find out what mains power and battery sources are available to your laptop. Once you’ve discovered them you can ask for more details.

The -e (enumerate) option lists all of the power sources it can find.

upower -e

Listing the power devices with upower

The first entry is for the AC adapter. The second is the battery. You may find you have several batteries in your laptop. Also, note that the first battery is sometimes numbered one, and sometimes it is numbered as zero, according to the preferences of the manufacturer.

The “DisplayDevice” entry isn’t a power source. It is a composite device that represents the status icon to show in desktop environments.

To take a closer look at our battery, we’ll use the -i (information) option, and pass the full descriptor for the battery.

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

Examining the battery with upower

The two items of most interest are the “Time to Empty” value and the “Percentage” value. These give an indication of the duration that the battery can continue to power the laptop, and the percentage of charge remaining in the battery.

Output from upower with the AC adapter unplugged

An important point to note is that the duration is related to the current activity of the laptop. If the load on the laptop increases, that duration will decrease.

Using our test laptop remotely over an SSH connection meant the laptop’s built-in display wasn’t being used. It auto-blanked after a short period of time. With the screen blanked, the battery life of the laptop was over an hour longer than with the screen illuminated.

If the AC adapter is connected, the information returned by upower it is slightly different.

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

Output from upower with the AC adapter plugged in

The “Time to Empty” value has been replaced by the “Time to Full” value, which is the time left before the battery reaches 100%. The “icon name” value has also changed to “battery-full-charging-symbolic”, reflecting the presence of mains power.

We can take a deeper look at the AC adapter, too.

upower -i /org/freedesktop/UPower/devices/line_power_ACAD

Examining the AC adapter with upower, with the adapter plugged in

The “Online” value will display “yes” if the AC adapter is plugged in, and “no” if it is unplugged.

Examining the AC adapter with upower, with the adapter unplugged

Examining the Contents of /sys/class/power_supply/

On a laptop, the “/sys/class/power_supply/” directory contains information that we can make good use of. Two subdirectories—“ACAD” and “BAT1”—contain information that we can reference to check on the battery capacity and whether the AC adapter is plugged in.

Note that the battery subdirectory might be called “BAT0” on your laptop. If you have multiple batteries fitted to your laptop, you’ll have multiple battery subdirectories.

A file called “online” in the “ACAD” subdirectory holds the digit one if the AC adapter is plugged in, and the digit zero if it is not.

A file called “capacity” in the “BAT1” subdirectory holds the value of the battery charge state.

ls /sys/class/power_supply/
cat /sys/class/power_supply/ACAD/online
cat /sys/class/power_supply/BAT1/capacity

Checking the online and capacity files with cat

This laptop has the AC adapter plugged in, and the battery charge is at 81%.

Because these two values ​​are presented in a straightforward and unadorned fashion, they’re ideal for use in scripts.

Let’s say you have a backup script that you only want to have executed if the AC power is present, or if the battery charge is greater than 70%. This stub of a script shows how you could achieve that.

#!/bin/bash

charge_level="$(cat /sys/class/power_supply/BAT1/capacity)"

ac_adapter="$(cat /sys/class/power_supply/ACAD/online)"

if [[ ac_adapter -eq 0 ]]; 

then 

  if [[ charge_level < 70 ]]; 

  then 

    echo "Insufficient battery charge for backup:" $charge_level 

  else 

    echo "Sufficient battery charge, starting backup:" $charge_level 

  fi 

else

  echo "On Mains power, starting backup." 

fi

The script obtains the values ​​from the two files and stores them in the variables charge_level and ac_adapter.

If the AC adapter is not plugged in the value in ac_adapter will be zero. If that's the case, the script checks the battery charge charge_level. If the battery charge is over 70%, the backup runs.

If the AC adapter is plugged in, the backup runs and the script doesn't bother checking the battery charge value.

Copy the script into an editor and save it as “battery.sh.” Make sure you use the correct path to the battery subdirectory on your laptop.

We need to make the script executable with the chmod command:

chmod +x battery.sh

making the script executable

Now we can run the script. The AC adapter is plugged in.

./battery.sh

Running the script with the AC adapter plugged in

Let's unplug the AC adapter and run it again.

./battery.sh

Running the script with the AC adapter unplugged

The power condition of the laptop is correctly detected, and the script acts accordingly.

Checking the Battery with acpi

If you have the advanced configuration and power interface package installed you can use it to query the battery and power condition of the laptop. If you don't have it installed, it's a small package and installs very quickly.

On Ubuntu, you can install it with this command.

sudo apt install acpi

Installing acpi on Ubuntu

On Fedora you'll use:

sudo dnf install acpi

Installing acpi on Fedora

On Manjaro you should type:

sudo pacman -Sy acpi

Installing acpi on Manjaro

We'll use the command with the -a (AC adapter) option and then once more with the -b (battery) option. Finally, we'll run it with the -b (battery) option and the -i (information) option. This gives a little extra information if any is available.

acpi -a
acpi -b
acpi -bi

Checking the AC adapter and battery with acpi

Knowledge is Power

And now you can gain knowledge about the power feeding your laptop.

The ability to have scripts check whether there is either mains power or sufficient battery power to perform high-load or long tasks such as system images or upgrades is particularly powerful.

RELATED: How to Maximize Your Linux Laptop's Battery Life