Skip to content

Instantly share code, notes, and snippets.

@128keaton
Last active September 17, 2019 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 128keaton/84a29afe68a7427d18add8dd0e3493d3 to your computer and use it in GitHub Desktop.
Save 128keaton/84a29afe68a7427d18add8dd0e3493d3 to your computer and use it in GitHub Desktop.
Prints HDD/Memory Usage (bash)
--------------------------------------------------|Usage|--------------------------------------------------
Memory Usage: [###############-------------------------] 38% (372M/962M, 194M available)
Disk Usage: [##########------------------------------] 26% (5G/19G, 13G available)
-----------------------------------------------------------------------------------------------------------
#!/bin/bash
TOTAL_MEM=`free -m | awk 'NR==2{print $2}'`
USED_MEM=`free -m | awk 'NR==2{print $3}'`
FREE_MEM=`free -m | awk 'NR==2{print $4}'`
TOTAL_DISK=`df -h | awk 'NR==4{print $2}' | tr -d -c 0-9`
USED_DISK=`df -h | awk 'NR==4{print $3}'`
FREE_DISK=`df -h | awk 'NR==4{print $4}' | tr -d -c 0-9`
# First arg is the total free, second is the used amount
# Third arg is the available amount, fourth is the name of what we're measuring
# Fifth arg is the measurement unit (i.e 'M' or 'G')
function UsageBar {
# Process data
let _progress=(${2}*100/${1})
let _done=(${_progress}*4)/10
let _left=40-$_done
# Build progressbar string lengths
_done=$(printf "%${_done}s")
_left=$(printf "%${_left}s")
printf "${4} Usage: [${_done// /#}${_left// /-}] ${_progress}%% (${2}${5}/${1}${5}, ${3}${5} available)\n"
}
echo "--------------------------------------------------|Usage|--------------------------------------------------"
UsageBar ${TOTAL_MEM} ${USED_MEM} ${FREE_MEM} 'Memory' 'M'
UsageBar ${TOTAL_DISK%.*} ${USED_DISK%.*} ${FREE_DISK%.*} 'Disk' 'G'
echo "-----------------------------------------------------------------------------------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment