Skip to content

Linux Command Line Mastery — Scripting & Administration

DodaTech Updated 2026-06-22 6 min read

In this tutorial, you'll learn about Linux Command Line Mastery. We cover key concepts, practical examples, and best practices.

The Linux command line is the most powerful interface for system administration, automation, and development — controlling everything from file operations to network diagnostics.

What You'll Learn

In this tutorial, you'll master essential Linux commands, bash scripting with variables and control flow, file permissions (chmod, chown), process management (ps, top, kill), systemd services, cron jobs, grep/awk/sed for text processing, and network troubleshooting with curl, ping, ss, and tcpdump.

Why It Matters

Every server runs Linux. Whether you manage cloud infrastructure, deploy applications, or build security tools, the command line is your primary interface. Automation through scripts replaces hours of manual work with a single command.

Real-World Use

Doda Browser's build pipeline runs on Linux servers. A bash script clones repos, builds Docker images, runs integration tests, and deploys to staging — all triggered by a cron job that checks for git changes every 15 minutes.

flowchart LR
  A[Terminal] --> B[File Ops: ls, cp, mv, find]
  A --> C[Process: ps, kill, systemctl]
  A --> D[Text: grep, sed, awk]
  A --> E[Network: curl, ss, ping]
  A --> F[Automation]
  F --> G[Bash Scripts]
  F --> H[Cron Jobs]
  F --> I[systemd Services]

Essential Linux Commands

File Operations

# Find large files (>100MB) in the current directory
find . -type f -size +100M -exec ls -lh {} \; | sort -k5 -hr
# Count lines of code, excluding node_modules
find . -name '*.js' -not -path '*/node_modules/*' | xargs wc -l | tail -1
# Search for a string in all files
grep -rn 'malware_signature' /etc/scanner/ --include='*.conf'

Expected output: For the first command: a list of large files with human-readable sizes, sorted largest first. The grep command returns filenames and matching lines.

Text Processing with grep, sed, awk

# Extract IP addresses from a log file
grep -oE '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b' /var/log/auth.log | sort | uniq -c | sort -nr
# Replace all occurrences in files
sed -i 's/old-api.example.com/new-api.example.com/g' config/*.yaml
# Print columns 2 and 5 from a CSV
awk -F',' '{print $2, $5}' data.csv | column -t

Expected output: The first command shows IP addresses sorted by frequency. The sed command silently replaces URLs in config files. Awk prints specific columns in aligned format.

Process Management

# List processes by memory usage
ps aux --sort=-%mem | head -10
# Kill all processes matching a name
pkill -f "node scanner.js"
# Monitor real-time process activity
htop

Expected behavior: ps aux --sort=-%mem | head -10 shows the 10 most memory-hungry processes. pkill -f kills all matching processes by pattern. htop provides an interactive process viewer (install with apt if not present).

Bash Scripting

Variables and Control Flow

#!/bin/bash
# Script: backup.sh — Backup a directory with timestamp
BACKUP_DIR="/var/backups"
SOURCE_DIR="/etc/scanner"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
DEST_FILE="scanner_backup_${TIMESTAMP}.tar.gz"

if [ ! -d "$SOURCE_DIR" ]; then
    echo "Error: Source directory $SOURCE_DIR does not exist" >&2
    exit 1
fi

mkdir -p "$BACKUP_DIR"
tar -czf "${BACKUP_DIR}/${DEST_FILE}" "$SOURCE_DIR" && \
    echo "Backup created: ${BACKUP_DIR}/${DEST_FILE}"

Expected output: On success: Backup created: /var/backups/scanner_backup_20260622_143000.tar.gz. On failure: Error: Source directory /etc/scanner does not exist to stderr.

Functions and Error Handling

#!/bin/bash
log() {
    local level="$1"
    local message="$2"
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] [${level}] ${message}"
}

cleanup_temp_files() {
    local dir="/tmp/scanner_*"
    rm -rf $dir
    log "INFO" "Cleaned up temporary files"
}

trap cleanup_temp_files EXIT
log "INFO" "Scanner started"
# Do work...
log "INFO" "Scanner finished"

Expected behavior: When the script exits (normally or via error), cleanup_temp_files runs automatically via the trap. Log entries include timestamps for auditing.

System Administration

systemd Service Management

# Create a systemd service for the scanner
# File: /etc/systemd/system/realtime-scanner.service
[Unit]
Description=Real-Time File Scanner
After=network.target

[Service]
Type=simple
User=scanner
ExecStart=/usr/local/bin/scanner --daemon
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

# Enable and start
sudo systemctl enable realtime-scanner
sudo systemctl start realtime-scanner
sudo systemctl status realtime-scanner

Expected behavior: The scanner starts on boot, restarts if it crashes, and logs to journald. systemctl status shows running state, PID, and recent log entries.

Cron Jobs

# Edit crontab: crontab -e
# Format: minute hour day month weekday command
# Run every day at 2:30 AM
30 2 * * * /usr/local/bin/update-signatures.sh
# Run every 15 minutes
*/15 * * * * /usr/local/bin/health-check.sh
# Run on weekdays at 9 AM
0 9 * * 1-5 /usr/local/bin/generate-report.sh

Expected behavior: Scripts execute at the specified times. Redirect output to a log file to capture results: >> /var/log/scanner.log 2>&1.

Command Comparison

Tool Purpose Example Alternative
grep Search text with regex grep -r 'error' /var/log/ ripgrep (rg)
sed Stream editor for find/replace sed -i 's/foo/bar/g' file.txt awk for complex transforms
awk Pattern scanning and processing awk '{print $1}' log.txt cut for simple column extraction
find Search files by criteria find . -name '*.log' -mtime +7 fd for faster UX
curl HTTP requests from terminal curl -X POST -d '{}' api.example.com httpie for readable output

Common Errors

  1. Using rm -rf without checking the path — A typo like rm -rf / var/log (space after /) destroys the system. Always double-check paths and use ls first.
  2. Forgetting chmod +x — A script with proper syntax fails with "Permission denied". Make it executable: chmod +x script.sh.
  3. Piping errors to nowherecommand 2>/dev/null hides errors you need to debug. Redirect to a log file instead: command 2>>error.log.
  4. Using sudo in scripts without checking — Scripts that assume root fail when run as a regular user. Check $(id -u) at the top and exit with a clear message.
  5. Not quoting variablesrm -rf $DIR fails if $DIR contains spaces or is empty. Always quote: rm -rf "$DIR".

Practice Questions

  1. What does chmod 755 mean in terms of permissions? Owner: rwx (7), Group: r-x (5), Others: r-x (5). Owner can read, write, execute; everyone else can read and execute.

  2. How do you find the 5 largest files in a directory tree? find . -type f -exec du -h {} + | sort -rh | head -5

  3. What is the difference between kill, pkill, and killall? kill sends a signal by PID; pkill matches by process name pattern; killall kills all processes with the exact name.

  4. How can you run a command every minute? Add to crontab: * * * * * /path/to/command

Challenge

Write a bash script that monitors disk usage and sends an alert when any partition exceeds 80% usage. The script should log to syslog, send an email (using mail command), and optionally trigger a cleanup of old log files.

Mini Project: Linux Server Hardening Script

Create a bash script that hardens a Ubuntu server for production:

  1. Create a non-root user with sudo privileges and SSH key authentication
  2. Disable root SSH login and password authentication in /etc/ssh/sshd_config
  3. Configure UFW firewall to allow only ports 22, 80, 443
  4. Install and configure fail2ban to block IPs after 5 failed SSH attempts
  5. Set up log rotation for application logs in /etc/logrotate.d/
  6. Create a daily cron job that checks for available security updates

This matches the server setup used by Durga Antivirus Pro's cloud infrastructure team.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro