Shell commands for different system info (battery time/percent/temp; CPU percent; disk percent/free/used; memory free/available/used/etc; bluetooth 'is connected'; network download/upload throughput.)

A few weeks ago I posted in the forums Tocco, which is a Python utility that is able to grab a good amount of OSX system info. In hopes of speeding things up, I began researching OSX commands that give partial system info and wrote a few bash scripts that some folks in here might find useful.

Do note that the multiline commands can be simply put into one line -- my intention wasn't to make things look obscure. I do not believe there is a feasible way to grab the following out-of-the-box from OSX (but would love to be proven wrong):

  • CPU Temperature
  • Battery Temperature

Note: All of these commands are native to OSX. None of these commands require sudo. Contributions to this thread are welcomed. :slight_smile:


Battery

Percent Left

pmset -g batt | grep "InternalBattery" | awk '{print $3+0"%"}'

Time Left

pmset -g batt | grep "InternalBattery" | awk '{print $5}'

Temperature

Fahrenheit

bc <<< "scale=2; ((($(ioreg -r -n AppleSmartBattery | grep Temperature | awk '{print $3}')/10.0)-273.30) * 9/5) + 32"

Celsius

bc <<< "scale=2; (($(ioreg -r -n AppleSmartBattery | grep Temperature | awk '{print $3}')/10.0)-273.15)"

CPU

Percentage Being Used

/usr/bin/top -ocpu -R -F -l 1 | grep "CPU usage" | awk '{print $3+$5"%"}'

Disk

Percentage Free

df | grep disk1s1 | awk '{print 100-$5"%"}'

Free Disk

df -h | grep disk1s1 | awk '{print $4"B"}'

Used Disk

df -h | grep disk1s1 | awk '{print $2"B"}'

Memory

Replace free in the grep -w "free" statement to pull a different values. Available values:

  • free
  • inactive
  • active
  • speculative
  • Others in vm_stat command.

Specific

function bth () {
    b=${1:-0}; d=''; s=0; S=(B {K,M,G,T,P,E,Z,Y}B)
    while ((b > 1024)); do
        d="$(printf ".%02d" $((b % 1024 * 100 / 1024)))"
        b=$((b / 1024))
        let s++
    done
    echo "$b$d ${S[$s]}"
};
bth $(vm_stat | grep -w "free" | awk '{print $3*4096}')

Bluetooth

Is Device Connected

Change the device_name to whatever Bluetooth device you have on your computer. Prints either "Yes" or "No."

export device_name="AirPods"
echo $(system_profiler SPBluetoothDataType 2> /dev/null | grep $(echo $device_name) -A 7 | grep Connected) | awk '{if($1==""){print "No"}else{print $2}}'

Network

SSID Name

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | grep -w SSID | awk '{print $2}'

Network Download Throughput

function bth () {
    b=${1:-0}; d=''; s=0; S=(B {K,M,G,T,P,E,Z,Y}iB)
    while ((b > 1024)); do
        d="$(printf ".%02d" $((b % 1024 * 100 / 1024)))"
        b=$((b / 1024))
        let s++
    done
    echo "$b$d ${S[$s]}/sec"
};

function func () {
  nettop -L 1 -c -P -x | awK -F "\"*,\"*" '{sum += $5} END {print sum}';
};

export first=$(func);
sleep 1;
export second=$(func);

bth $(expr $second - $first)

Network Upload Throughput

function bth () {
    b=${1:-0}; d=''; s=0; S=(B {K,M,G,T,P,E,Z,Y}iB)
    while ((b > 1024)); do
        d="$(printf ".%02d" $((b % 1024 * 100 / 1024)))"
        b=$((b / 1024))
        let s++
    done
    echo "$b$d ${S[$s]}/sec"
};

function func () {
  nettop -L 1 -c -P -x | awK -F "\"*,\"*" '{sum += $6} END {print sum}';
};

export first=$(func);
sleep 1;
export second=$(func);

bth $(expr $second - $first)

Here's how you show how much disk space you have used / free on your boot drive:

df -H / | awk '/\//{getline; print $3" / "$4}'

Mine shows this:

155G / 286G

If you just want free space:

df -H / | awk '/\//{getline; print $4}'

or just used space

df -H / | awk '/\//{getline; print $3}'

1 Like

Completely forgot disk space -- good idea! Gonna modify those a little bit and post em' in a sec. Thanks for the share.

get SSID of current Wi-Fi network

This option works regardless of what port your Wi-Fi is on:

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I \
2>/dev/null | awk -F': ' '/ SSID/{print $NF}')

This one works if you know which port is your Wi-Fi port (usually en0 on MacBooks, en1 on iMac or Mac mini):

networksetup -getairportnetwork "en0" 2>/dev/null \
| sed 's#^Current Wi-Fi Network: ##g'

Or you can calculate the proper port and then use it:

PORT=$(networksetup -listallhardwareports 2>/dev/null \
| grep 'Wi-Fi' -A1 \
| grep -o en.)

networksetup -getairportnetwork "$PORT" 2>/dev/null \
| sed 's#^Current Wi-Fi Network: ##g'
1 Like

Modified it a little bit and added! Thanks for share again.

How would I modify upload and download throughput to show Mbps instead?