Site icon linuxhardened

Basic Shell Commands

Basic Shell Commands

Basic Shell Commands

The shell scripting is an linux based script which works on shell language. It uses Bash or sh as an interpreter to execute it. It is a basic command based script which can handle day-to-day tasks which require huge execution time. To learn basic shell scripting, you must have handson basic shell commands.

The shell script can make tasks simpler by simply automating them for day-to-day tasks like disk cleanup, purging policies, backups, log rotations, etc. The shell script itself can’t make automation like scheduling the tasks. We require a Linux utility known as cron jobs for scheduling tasks, which can execute scripts at a specified time for execution, which makes less human involvement.

Basic shell commands:

Read-only commands:

Files and directories management commands:

Text processing commands:

Note: sed can change file content. Double-check your command to protect your data.

Process management commands:

Network management commands:

Note: Most organizations avoid nmap because while scanning, it causes suspicious activity on the server.

Input/Output & Variable Commands:

Utility commands:

Example Scripts:

Let’s see some example scripts that we can use in daily tasks to improve our productivity.

Script to check system health in a single click:

#!/bin/bash

# --- Script Name: health_check.sh ---
# A simple script to perform basic system health checks.

echo "--- System Health Check Report ---"
echo "Run Date: $(date)"
echo "----------------------------------"
echo

# 1. Check System Uptime and Load Average
echo "### System Uptime and Load Average ###"
uptime
echo "---"
echo

# 2. Check Disk Usage (Human-readable format)
echo "### Disk Usage ###"
df -h
echo "---"
echo

# 3. Check Memory Usage (Human-readable format)
echo "### Memory Usage ###"
free -h
echo "---"
echo

# 4. Check Top 5 Processes (by CPU usage - requires 'top' or 'htop')
# Using top: -b (batch mode), -n 1 (one iteration), -o %CPU (sort by CPU), head (first 7 lines: header + 5 processes)
if command -v top &> /dev/null; then
    echo "### Top 5 CPU Consuming Processes ###"
    top -bn1 -o %CPU | head -n 12
    echo "---"
    echo
else
    echo "### Top Processes (requires 'top' command) ###"
    echo "'top' command not found. Skipping process check."
    echo "---"
    echo
fi


# 5. Check Kernel Version
echo "### Kernel Version ###"
uname -a
echo "---"
echo

# 6. Check Logged-in Users
echo "### Logged-in Users ###"
who
echo "---"
echo

echo "--- Health Check Complete ---"

The expected output:

Basic Shell Commands

Conclusion:

Shell scripting automates Linux tasks using interpreters like Bash and basic commands. These commands cover essential areas like file management, text processing, and system monitoring. Scripts streamline repetitive day-to-day operations, improving efficiency. Task scheduling requires separate utilities like cron jobs, as scripts alone don’t handle it. Learning fundamental commands is key to writing effective scripts and understanding command arguments.

Exit mobile version