1. Introduction
Depending on the desktop environment of the Linux distribution we are using, we may or may not have a battery status indicator on the desktop. Sometimes, we may have to check the battery status using the command line.
In this tutorial, we’ll look at some ways to check the battery status (charging status and power percentage) using commands.
2. Using upower
upower is a command-line client for the UPower daemon, which is responsible for system-wide power management on most Linux distributions. Before we use this command to obtain the information about the battery, we need to figure out the path for the connected battery. This can be done using upower -e or upower –enumerate:
$ upower -e
/org/freedesktop/UPower/devices/line_power_AC
/org/freedesktop/UPower/devices/battery_BAT0
/org/freedesktop/UPower/devices/DisplayDevice
The second line, where the device name starts with “battery..”, is the one we are looking for. It’s usually battery_BAT0 or battery_BAT1, depending on the distribution we’re using.
Once we have this path, we can proceed to get detailed information about the battery using upower -i:
$ upower -i /org/freedesktop/UPower/devices/battery_BAT0
native-path: BAT0
vendor: LGC-LGC3.0
model: DELL 49VTP27
serial: 14057
power supply: yes
updated: Monday 18 April 2022 07:45:28 AM (105 seconds ago)
has history: yes
has statistics: yes
battery
present: yes
rechargeable: yes
state: charging
warning-level: none
energy: 53.5464 Wh
energy-empty: 0 Wh
energy-full: 57.3981 Wh
energy-full-design: 48.84 Wh
energy-rate: 2.4531 W
voltage: 12.564 V
time to full: 1.6 hours
percentage: 93%
capacity: 100%
technology: lithium-ion
icon-name: 'battery-full-charging-symbolic'
History (rate):
1650248128 2.453 charging
Needless to say, we can use grep or other commands to process the output into a format we need:
$ upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep percentage | grep -o "[0-9]*"
93
$ upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep state | cut -d ':' -f2 | xargs
charging
These days, most devices have only a single battery, so we can also use the upower command with the -d parameter. This essentially prints information for all the connected devices:
$ upower -d
Device: /org/freedesktop/UPower/devices/line_power_AC
native-path: AC
power supply: yes
updated: Thursday 21 April 2022 07:42:11 AM (658 seconds ago)
has history: no
has statistics: no
line-power
warning-level: none
online: yes
icon-name: 'ac-adapter-symbolic'
Device: /org/freedesktop/UPower/devices/battery_BAT0
native-path: BAT0
vendor: LGC-LGC3.0
model: DELL 49VTP27
serial: 14057
power supply: yes
updated: Thursday 21 April 2022 07:52:12 AM (57 seconds ago)
has history: yes
has statistics: yes
battery
present: yes
rechargeable: yes
state: charging
warning-level: none
energy: 48.507 Wh
energy-empty: 0 Wh
energy-full: 57.3981 Wh
energy-full-design: 48.84 Wh
energy-rate: 7.1262 W
voltage: 12.544 V
time to full: 1.2 hours
percentage: 93%
capacity: 100%
technology: lithium-ion
icon-name: 'battery-full-charging-symbolic'
History (rate):
1650507732 7.126 charging
....more info
This command is equivalent to running upower -e, followed by upower -i for each device, and thus saves us one step in the process.
3. Using the /sys/class/power_supply Directory
The Linux kernel exposes various information about the system and connected hardware devices in a read-only filesystem known as sysfs. We can access this file system using the /sys directory. We can find the battery device directories under the /sys/class/power_supply directory using the ls command:
$ ls /sys/class/power_supply/
AC BAT0
BAT0 is the directory we are looking for. It’s usually BAT0 or BAT1, depending on the Linux distribution we’re using. Next, we can get various information about the battery from files inside the directory. We could start with listing files to see what files are present:
ls /sys/class/power_supply/BAT0
alarm charge_full_design device power subsystem voltage_min_design
capacity charge_now hwmon2 present technology voltage_now
capacity_level current_now manufacturer serial_number type
charge_full cycle_count model_name status uevent
Now, we can read the contents of the corresponding files to get the required data:
$ cat /sys/class/power_supply/BAT0/capacity
100
$ cat /sys/class/power_supply/BAT0/status
Full
In the above snippet, capacity gives us the percentage of charge in the battery and status tells us whether it is charging or discharging.
4. Conclusion
In this article, we looked at two different methods to get information about battery status using the command line.
Using the upower command, we’re able to print a detailed output about the battery status using one or two commands.
The information can also be accessed using the files in the /sys/class/power_supply/BAT0/ (or BAT1) directory. The information here is very raw and discrete, without any formatting. So, we can directly use the data in bash scripts if we need to.