How can I get the percentage of my battery life from the command line? [duplicate]

Solution 1:

The below command outputs a lot status and statistical information about the battery. The /org/... path can be found with the command upower -e (--enumerate).

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

Example output:

  native-path:          /sys/devices/LNXSYSTM:00/device:00/PNP0C0A:00/power_supply/BAT0
  vendor:               NOTEBOOK
  model:                BAT
  serial:               0001
  power supply:         yes
  updated:              Thu Feb  9 18:42:15 2012 (1 seconds ago)
  has history:          yes
  has statistics:       yes
  battery
    present:             yes
    rechargeable:        yes
    state:               charging
    energy:              22.3998 Wh
    energy-empty:        0 Wh
    energy-full:         52.6473 Wh
    energy-full-design:  62.16 Wh
    energy-rate:         31.6905 W
    voltage:             12.191 V
    time to full:        57.3 minutes
    percentage:          42.5469%
    capacity:            84.6964%
    technology:          lithium-ion
  History (charge):
    1328809335  42.547  charging
    1328809305  42.020  charging
    1328809275  41.472  charging
    1328809245  41.008  charging
  History (rate):
    1328809335  31.691  charging
    1328809305  32.323  charging
    1328809275  33.133  charging

You could use tools like grep to get just the information you want from all that output.

One simple way: piping the above command into

grep -E "state|to\ full|percentage"

outputs:

state:               charging
time to full:        57.3 minutes
percentage:          42.5469%

If you would often like to run that command, then you could make a Bash alias for the whole command. Example:

alias bat='upower -i /org/freedesktop/UPower/devices/battery_BAT0| grep -E "state|to\ full|percentage"'

Add that to the end of your .bashrc file, and you can type 'bat' any time, in the terminal.

There is also a upower -d (--dump) command that shows information for all available power resources such as laptop batteries, external mice, etc.

Solution 2:

A friendly reminder: since Linux kernel 2.6.24 using /proc to store ACPI info has been discouraged and deprecated.

Now we are encouraged to use -> /sys/class/power_supply/BAT0.

UPDATE: Linux 3.19 and onwards, we should look at the following directory -> /sys/class/power_supply/BAT1/

For example, checking capacity & status running

Linux 4.20

# uname -a
Linux netbook 4.20.1-arch1-1-ARCH #1 SMP PREEMPT Wed Jan 9 20:25:43 UTC 2019 x86_64 GNU/Linux
# cat /sys/class/power_supply/BAT1/capacity
99
# cat /sys/class/power_supply/BAT1/status
Charging

and

Linux 5.9

# uname -a
Linux netbook 5.9.1-arch1-1 #1 SMP PREEMPT Sat, 17 Oct 2020 13:30:37 +0000 x86_64 GNU/Linux
# cat /sys/class/power_supply/BAT1/capacity
100
# cat /sys/class/power_supply/BAT1/status
Full