Skip to content

Linux System Commands

Essential Linux commands for monitoring system resources, managing disk space, and maintaining the Ubuntu server.

🟡 LOW RISK

Most commands here are read-only monitoring commands. Deletion commands are marked with warnings.


Table of Contents

  1. Disk Space Management
  2. Memory Monitoring
  3. CPU & Process Monitoring
  4. System Logs Cleanup
  5. Network Monitoring
  6. System Information

Disk Space Management

Check Disk Usage (df)

bash
# Human-readable format
df -h

# Show only specific filesystem
df -h /

# Show all filesystems including tmpfs
df -ha

Example Output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       100G   45G   50G  47% /

Warning Levels:

  • < 70% - Normal
  • 70-85% - Monitor closely
  • > 85% - Take action to free space
  • > 95% - Critical! System may fail

Check Directory Sizes (du)

bash
# Check specific directory
du -sh /var/www/html

# Check with subdirectories
du -h --max-depth=1 /var

# Find largest directories
du -h --max-depth=1 / 2>/dev/null | sort -rh | head -20

Find Large Files

bash
# Files larger than 100MB
find / -type f -size +100M 2>/dev/null | head -20

# Files larger than 1GB
find / -type f -size +1G 2>/dev/null

# With sizes
find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null | head -20

Find Large Directories

bash
# Top 10 largest directories
du -h --max-depth=2 / 2>/dev/null | sort -rh | head -20

Clean Package Cache

🟠 CAUTION

This removes downloaded package files. Safe but irreversible.

bash
# Clean apt cache
sudo apt clean

# Remove old packages
sudo apt autoremove -y

# Check freed space
df -h

Clean Journal Logs

🟠 CAUTION

This removes old system logs.

bash
# Check journal size
journalctl --disk-usage

# Keep only last 7 days
sudo journalctl --vacuum-time=7d

# Keep only 500MB
sudo journalctl --vacuum-size=500M

Memory Monitoring

Check Memory Usage (free)

bash
# Human-readable
free -h

# With totals
free -ht

# In megabytes
free -m

Example Output:

              total        used        free      shared  buff/cache   available
Mem:           16Gi       8.5Gi       2.1Gi       512Mi       5.4Gi       6.8Gi
Swap:          4.0Gi       1.2Gi       2.8Gi

Key Values:

  • available - Memory available for new applications
  • buff/cache - Memory used for caching (can be freed)
  • swap used - If high, you may need more RAM

Memory Usage by Process

bash
# Top memory consumers
ps aux --sort=-%mem | head -15

# With specific columns
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -15

Check Swap Usage

bash
# Swap summary
swapon --show

# Detailed swap info
cat /proc/swaps

Clear Cache (Emergency)

🟠 USE WITH CAUTION

Only use if system is critically low on memory.

bash
# Clear page cache
sudo sync; echo 1 > /proc/sys/vm/drop_caches

# Clear dentries and inodes
sudo sync; echo 2 > /proc/sys/vm/drop_caches

# Clear all
sudo sync; echo 3 > /proc/sys/vm/drop_caches

CPU & Process Monitoring

Real-time Monitoring (top)

bash
top

Key Commands in top:

  • q - Quit
  • M - Sort by memory
  • P - Sort by CPU
  • k - Kill a process
  • 1 - Show individual CPUs

Better Monitoring (htop)

bash
htop

Install if not available:

bash
sudo apt install htop

htop Features:

  • Color-coded display
  • Mouse support
  • Tree view (F5)
  • Search (F3)
  • Filter (F4)
  • Kill process (F9)

Check CPU Info

bash
# CPU details
lscpu

# Number of cores
nproc

# CPU model
cat /proc/cpuinfo | grep "model name" | head -1

Check Load Average

bash
# Quick view
uptime

# Detailed
cat /proc/loadavg

Understanding Load Average:

  • Three numbers: 1min, 5min, 15min averages
  • Load = 1.0 per CPU core is 100% utilization
  • Example: 4-core server, load 4.0 = 100% utilized

Find Process by Name

bash
ps aux | grep nginx
ps aux | grep php
ps aux | grep mysql

Kill Process

🔴 CAUTION

Killing wrong process can crash services.

bash
# Graceful kill
kill <PID>

# Force kill
kill -9 <PID>

# Kill by name
pkill nginx
killall php-fpm8.3

System Logs Cleanup

Check System Log Sizes

bash
du -sh /var/log/*
du -sh /var/log/

Clear System Logs

🟠 CAUTION

This removes log history. Back up if needed.

bash
# Clear syslog
sudo truncate -s 0 /var/log/syslog

# Clear auth log
sudo truncate -s 0 /var/log/auth.log

# Clear kern log
sudo truncate -s 0 /var/log/kern.log

# Clear all old logs
sudo find /var/log -type f -name "*.log" -mtime +30 -delete
sudo find /var/log -type f -name "*.gz" -delete
sudo find /var/log -type f -name "*.1" -delete
sudo find /var/log -type f -name "*.old" -delete

Clear Specific Service Logs

bash
# Nginx logs
sudo truncate -s 0 /var/log/nginx/access.log
sudo truncate -s 0 /var/log/nginx/error.log

# MySQL logs
sudo truncate -s 0 /var/log/mysql/error.log

# PHP logs
sudo truncate -s 0 /var/log/php8.3-fpm.log

Rotate Logs Now

bash
sudo logrotate -f /etc/logrotate.conf

Network Monitoring

Check Network Connections

bash
# All connections
netstat -an

# Listening ports
netstat -tlnp

# Established connections
netstat -an | grep ESTABLISHED | wc -l

Check Specific Port

bash
# Who's using port 80
sudo lsof -i :80
sudo netstat -tlnp | grep :80

Check Bandwidth (if iftop installed)

bash
sudo iftop

System Information

System Overview

bash
# OS version
cat /etc/os-release

# Kernel version
uname -r

# All system info
uname -a

Uptime

bash
uptime

Last Reboot

bash
last reboot | head -5

Check Running Services

bash
systemctl list-units --type=service --state=running

Quick Disk Cleanup Script

bash
#!/bin/bash
# Save as /root/cleanup.sh

echo "=== Disk Usage Before ==="
df -h /

echo ""
echo "=== Cleaning apt cache ==="
sudo apt clean
sudo apt autoremove -y

echo ""
echo "=== Cleaning journal logs ==="
sudo journalctl --vacuum-time=7d

echo ""
echo "=== Cleaning old Laravel logs ==="
find /var/www/html/storage/logs -name "*.log" -mtime +7 -delete

echo ""
echo "=== Cleaning old system logs ==="
sudo find /var/log -type f -name "*.gz" -delete
sudo find /var/log -type f -name "*.1" -delete

echo ""
echo "=== Disk Usage After ==="
df -h /

Run:

bash
sudo chmod +x /root/cleanup.sh
sudo /root/cleanup.sh

Quick Reference Card

ActionCommand
Disk usagedf -h
Directory sizedu -sh /path
Memory usagefree -h
CPU/Process monitorhtop or top
Load averageuptime
Find large filesfind / -type f -size +100M
Clear apt cachesudo apt clean && sudo apt autoremove
Clear journalsudo journalctl --vacuum-time=7d
Check portsnetstat -tlnp
Kill processkill <PID>
System infouname -a
OS versioncat /etc/os-release

www.onezeroart.com