Get System Info with Bash

Summary: Check RAM, disk, and CPU from a script.


With the power of Bash scripting, you can automate the tedious process of checking your system information. Want to quickly check how much RAM you have, your disk usage, and the health of your CPU? You can do all of this from the terminal using simple Bash scripts. In this article, we'll walk through writing a script to capture key information about your Linux system.

Why Check System Info with Bash?

  • Automation: Schedule health checks or collect info on multiple machines.
  • Quick Diagnostics: Instantly spot problem areas (e.g., low disk space).
  • Custom Reports: Format the report as you like for easy reading or parsing.

What We'll Cover

  • Checking total and available RAM.
  • Getting disk usage for critical partitions.
  • Extracting CPU information.
  • Combining all info into a neat Bash script.

1. Check RAM Usage

We'll use the free command, which is present on most Linux distributions.

free -h

To extract just the important numbers, use awk:

free -h | awk '/^Mem:/ { print "Total RAM: " $2 ", Used: " $3 ", Free: " $4 }'

Output example:

Total RAM: 7.7Gi, Used: 2.5Gi, Free: 4.1Gi

2. Detect Disk Usage

Use df to show filesystem disk space usage. The -h flag gives human-readable sizes.

df -h /

To get only the relevant line (root partition):

df -h / | awk 'NR==2 { print "Disk (/):",  $3 " used of " $2 " (" $5 " full)" }'

Sample output:

Disk (/): 14G used of 30G (45% full)

You can check more mounts by changing the / to another path, like /home.


3. Monitor CPU Info

To get your CPU model and the number of cores, use lscpu or read /proc/cpuinfo:

lscpu | grep 'Model name'
lscpu | grep '^CPU(s):'

Or, using a one-liner with awk:

awk -F: '/model name/ {print $2; exit}' /proc/cpuinfo
nproc

Sample output:

 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
4

4. Putting it All Together: System Info Script

Let's combine these into a single script called system-info.sh:

#!/bin/bash

echo "=== System Information ==="
echo

# RAM usage
echo "[RAM]"
free -h | awk '/^Mem:/ { print "Total: " $2 ", Used: " $3 ", Free: " $4 }'
echo

# Disk usage for root
echo "[Disk Usage]"
df -h / | awk 'NR==2 { print "Root (/): " $3 " used of " $2 " (" $5 " full)" }'
echo

# CPU info
echo "[CPU]"
awk -F: '/model name/ {print "Model:" $2; exit}' /proc/cpuinfo | sed 's/^ *//'
echo "Cores: $(nproc)"

Set the script as executable:

chmod +x system-info.sh

Run it:

./system-info.sh

Sample output:

=== System Information ===

[RAM]
Total: 7.7Gi, Used: 2.5Gi, Free: 4.1Gi

[Disk Usage]
Root (/): 14G used of 30G (45% full)

[CPU]
Model: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
Cores: 4

Bonus: Formatting & Extending

  • Pipe output to a file for logging: ./system-info.sh > status.txt
  • Add network info: ip a or ifconfig
  • Use color with tput or ANSI codes for better readability.

Conclusion

With a short Bash script, you can easily check vital system resources—RAM, disk, and CPU—within seconds. Tweak this script to suit your setup or automate health checks across systems!

Happy scripting!