cat /sys/class/power_supply/BAT0/capacity - Shows your current battery percentage
cat /sys/class/net/w*/operstate - Shows if your wifi is connected (shows up if connected and down when not)
cat /sys/class/net/e*/operstate - Shows if your ethernet is connected (shows up if connected and down when not)

These are actual files in the linux system that you can open with a file manager and see the output !
There are many more like these in the /sys/class directory. My first reaction when i knew about this was like “Why the hell haven’t i heard about it before ?”. This tip may be useful to general users, but for people who like to do scripting, this can be gold.

Does anyone know where to get more information about the sys/class directory and what all the outputs mean in different cases ?

NOTE: i’m not a linux expert or a professional programmer, but i’m interested in them and learns what i can from online. So i prefer simple solutions that can be easily understood.

    • federateduserOP
      link
      fedilink
      arrow-up
      3
      ·
      4 years ago
      ~ >>> time cat /sys/class/power_supply/BAT0/capacity
      57
      cat /sys/class/power_supply/BAT0/capacity  0.00s user 0.00s system 3% cpu 0.098 total
      ~ >>> time acpi
      Battery 0: Discharging, 57%, 04:55:40 remaining
      acpi  0.00s user 0.01s system 5% cpu 0.105 total
      

      These are very light commands, but i feel like the cat command is more light on resources. acpi might be a better command for general users cause it shows more information about battery. Note that it might not be installed on all systems, so you may have to install it manually. The /sys/class directory is more useful when scripting something cause it works more at a system level and don’t need any extra modules or programs for it to work.

      Before i knew about it, i used nmcli -f STATE -t g for checking my wifi connection (on a script), which was way less efficient. The reason why /sys/class method is so nice is cause you don’t need any unnecessary modules or programs just for checking simple system things. These different commands we use maybe doing the same thing on our back.