Linux System Information Commands Guide

System Information Commands

Commands for displaying system information.

uname - Display System Information

Displays basic system information.

Option Description
-a Display all information
-s Display kernel name
-n Display network node hostname
-r Display kernel release number
-v Display kernel version
-m Display machine hardware name
-p Display processor type
-o Display operating system

Examples:

uname -a - Display all system information
uname -r - Display kernel release number
uname -o - Display operating system
uname -a | awk '{print $3}' - Extract only the kernel version
echo "Kernel: $(uname -r)" - Display kernel version in a shell script
uname -m | grep -q "x86_64" && echo "64-bit system" || echo "32-bit system" - Determine system architecture
uname -a | grep -i "ubuntu" - Check if system is Ubuntu
uname -a > system_info.txt - Save system information to a file
if [ "$(uname)" = "Linux" ]; then echo "This is Linux"; fi - Conditional based on OS type

hostname - Display/Set Hostname

Displays or sets the system's hostname.

Option Description
-f Display fully qualified domain name (FQDN)
-i Display host's IP address
-d Display DNS domain name

Examples:

hostname - Display hostname
hostname -f - Display fully qualified domain name
sudo hostname new-hostname - Temporarily change hostname (reverts after reboot)
hostname -I - Display all IP addresses of the system
hostname | tr '[:lower:]' '[:upper:]' - Display hostname in uppercase
echo "Current host: $(hostname)" - Display hostname in a shell script
hostname -i | grep -q "127.0.0.1" && echo "Localhost configuration detected" - Check for localhost configuration
ping -c 1 $(hostname) - Ping your own host
ssh user@$(hostname -f) - SSH to your own host using FQDN

df - Display Disk Space Usage

Displays disk space usage of file systems.

Option Description
-h Display in human-readable format (KB, MB, GB, etc.)
-T Display file system type
-i Display inode information
-a Display all file systems (including those with 0 size)

Examples:

df -h - Display disk usage in human-readable format
df -h /home - Display disk usage for a specific mount point
df -hT - Display disk usage with file system types
df -h | grep -v "tmpfs" | sort -k 5 -r - Exclude temporary file systems and sort by usage percentage
df -h | awk '$5 > "80%" {print $0}' - Display file systems with usage over 80%
df -i | grep -v "tmpfs" - Display inode usage excluding temporary file systems
df -h | grep "^/dev" | awk '{total += $2; used += $3} END {print "Total: " total "GB, Used: " used "GB"}' - Calculate total capacity and usage across all disks
df -h --output=source,size,used,avail,pcent | grep -v "tmpfs" - Display only specific columns
watch -n 5 "df -h | grep /dev/sda" - Monitor disk usage every 5 seconds
df -h | mail -s "Disk Usage Report $(date +%F)" admin@example.com - Email disk usage report

du - Display Directory Size

Displays disk usage of directories and files.

Option Description
-h Display in human-readable format (KB, MB, GB, etc.)
-s Display only total size for specified directory
-a Display size of all files and directories
-c Display grand total
--max-depth=N Display directories only to specified depth

Examples:

du -h - Display size of current directory and subdirectories
du -sh /home/user - Display only total size of specified directory
du -h --max-depth=1 / - Display size of directories directly under root
du -h --max-depth=1 | sort -hr - Sort subdirectories by size (largest first)
du -h --max-depth=1 | grep "[0-9]G\b" - Display only directories with size in GB
find . -type f -name "*.log" | xargs du -ch - Display size of all log files with total
du -sh /var/* 2>/dev/null | sort -hr | head -n 5 - Display 5 largest directories in /var (ignoring errors)
du -h --time /home/user | sort -r - Display sizes with last modification time, sorted by size
du -h --exclude="*.bak" --max-depth=2 /home - Display directory sizes excluding backup files
du -b | sort -n | awk '{printf "%.3f MB: %s\n", $1/(1024*1024), $2}' | tail - Display largest directories in MB
for i in /*; do if [ -d "$i" ]; then du -sh "$i"; fi; done | sort -hr - Display top-level directories sorted by size

free - Display Memory Usage

Displays system memory usage.

Option Description
-h Display in human-readable format (KB, MB, GB, etc.)
-m Display in megabytes
-g Display in gigabytes
-t Display total line
-s N Update display every N seconds (continuous monitoring)

Examples:

free -h - Display memory usage in human-readable format
free -m - Display memory usage in megabytes
free -s 5 - Update memory usage display every 5 seconds
free -m | awk 'NR==2{printf "Memory Usage: %s/%s MB (%.2f%%)\n", $3,$2,$3*100/$2 }' - Display memory usage percentage
free -m | grep Mem | awk '{print "Used: " $3 "MB, Free: " $4 "MB, Available: " $7 "MB"}' - Extract used, free, and available memory
watch -n 1 'free -m | grep "Mem\|Swap"' - Monitor memory and swap usage every second
free -b | grep "Mem:" | awk '{printf "%.2f GB\n", $2/(1024*1024*1024)}' - Display total memory in GB
free -m > memory_$(date +%Y%m%d_%H%M%S).log - Save memory usage to timestamped log file
for i in {1..10}; do free -m | grep Mem; sleep 5; done - Record memory usage 10 times at 5-second intervals
free -m | awk '/Mem:/ {print "Memory used: " 100*$3/$2 "%"} /Swap:/ {print "Swap used: " 100*$3/$2 "%"}' - Display memory and swap usage percentages

lscpu - Display CPU Information

Displays CPU architecture information.

Option Description
-e Display in extended readable format
-a Display both online and offline CPUs
-J Output in JSON format

Examples:

lscpu - Display CPU information
lscpu -e - Display CPU information in extended format
lscpu | grep -E "^CPU\(s\)|Core|Socket|Model name" - Extract key CPU information
lscpu -p | grep -v "^#" | wc -l - Count available CPU cores
lscpu | grep "MHz" | awk '{print $NF " MHz"}' - Extract CPU frequency
lscpu -J | jq - Display CPU information in formatted JSON
lscpu | grep -i cache | sort - Extract and sort cache information
lscpu -p=CPU,CORE,SOCKET | grep -v "^#" - Display CPU, core, and socket relationships
lscpu | awk -F: '/Model name/ {print $2}' | sed 's/^ *//' - Extract CPU model name and remove leading spaces
lscpu | grep -i "virt" | grep -i "tech" - Display virtualization technology information

lsblk - Display Block Devices

Displays block devices (hard disks, SSDs, USB drives, etc.) on the system.

Option Description
-a Display all devices
-f Display filesystem information
-m Include mountpoint information
-o NAME,SIZE,... Specify columns to display

Examples:

lsblk - Display block devices
lsblk -f - Display with filesystem information
lsblk -o NAME,SIZE,FSTYPE,MOUNTPOINT - Display only specific columns
lsblk -d | grep -v "loop" - Display only disks (exclude loop devices)
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT | grep "disk\|part" | sort -k 2 -hr - Display disks and partitions sorted by size
lsblk -J | jq - Display block device information in formatted JSON
lsblk -o NAME,SIZE,FSTYPE,MOUNTPOINT,UUID | grep -v "^loop" - Display detailed information including UUID (exclude loop devices)
lsblk -o NAME,SIZE,RO,RM,MOUNTPOINT | grep "1$" | grep "^sd" - Find removable SD devices
lsblk -b | awk '$4=="0" && $6=="disk" {sum+=$3} END {print sum/1024/1024/1024 " GB"}' - Calculate total disk capacity in GB
lsblk -o NAME,SIZE,MOUNTPOINT | grep -v "^loop" | awk 'NR>1 {print}' | column -t - Display in formatted table

lsusb - Display USB Devices

Displays USB devices connected to the system.

Option Description
-v Display verbose information
-t Display USB device tree
-d [vendor]:[product] Display only devices with specified vendor and product ID

Examples:

lsusb - List USB devices
lsusb -v - Display verbose information
lsusb -t - Display in tree structure
lsusb | grep -i "storage" - Find storage devices
lsusb -v | grep -A 5 -i "keyboard" - Display detailed information about keyboard devices
lsusb | awk '{print $6, $7, $8, $9, $10, $11}' - Extract device names
lsusb -v | grep -i "bcdusb" | sort - Extract and sort USB version information
lsusb | cut -d' ' -f6 | sort | uniq -c - Count devices by vendor
lsusb -v 2>/dev/null | grep -i "power" | grep -i "ma" - Display power consumption information
for dev in $(lsusb | awk '{print $2":"$4}' | sed 's/://' | sed 's/://'); do lsusb -D /dev/bus/usb/$dev | grep -i product; done - Display product names for all USB devices

uptime - Display System Uptime and Load

Displays system uptime, number of users, and load averages.

Option Description
-p Display uptime in pretty format
-s Display system up since date/time

Examples:

uptime - Display uptime and load averages
uptime -p - Display uptime in pretty format
uptime -s - Display system start time
uptime | awk '{print $3, $4}' | sed 's/,//' - Extract only uptime
uptime | awk '{print $10, $11, $12}' - Extract only load averages
watch -n 5 uptime - Monitor uptime and load averages every 5 seconds
uptime | awk '{print $10}' | sed 's/,//' | awk '{if ($1 > 1.0) print "High load: " $1; else print "Normal load: " $1}' - Evaluate 1-minute load average
echo "System has been up since $(uptime -s)" - Display formatted uptime
for i in {1..12}; do uptime | awk '{print $10}' | tr -d ','; sleep 5; done - Record load average 12 times at 5-second intervals
uptime > uptime_$(date +%Y%m%d_%H%M%S).log - Save uptime information to timestamped log file
uptime | awk '{print $3, $4, $5}' | sed 's/,//g' | awk -v d="$(date +%F)" '{print d, $0}' - Combine current date with uptime