TDCTF Academy Logo TDCTF ACADEMY

Linux CLI (Command Line Interface)

Shell

Shell adalah antarmuka antara user dan kernel. Shell menerjemahkan perintah dari keyboard dan menjalankannya melalui sistem operasi.

Jenis shell populer:

  • bash (Bourne Again Shell) - Default di hampir semua distro Linux. Fitur: command history, tab completion, job control.
  • zsh (Z Shell) - Lebih modern dengan framework Oh My Zsh, theming, plugin, dan auto-suggestion.
  • fish (Friendly Interactive Shell) - Syntax highlighting otomatis, suggestion berbasis history, konfigurasi via web UI.
echo $SHELL # Cek shell aktif
cat /etc/shells # Lihat daftar shell yang tersedia
chsh -s /bin/zsh # Ganti default shell user

Basic Commands

Navigasi dan manipulasi file:

ls -la # List file dengan detail (hidden files, permission, size)
cd /etc/nginx # Pindah direktori
pwd # Cetak direktori saat ini
cp -r source/ dest/ # Copy direktori rekursif
mv file1 /target/ # Move atau rename
rm -rf dir/ # Hapus direktori beserta isinya (hati-hati!)

Pencarian:

find /var -name "*.log" -type f -size +10M # Cari file log > 10MB
find . -mtime -7 # File yang diubah dalam 7 hari terakhir
grep -rn "ERROR" /var/log/ # Cari string "ERROR" di semua file log
grep -E "^[0-9]{3}" data.txt # Regex: baris diawali 3 digit

Text processing - awk dan sed:

awk '{print $1, $3}' file.txt # Cetak kolom 1 dan 3
awk '/ERROR/ {count++} END {print count}' log.txt # Hitung jumlah ERROR
sed 's/lama/baru/g' file.txt # Replace global
sed -n '10,20p' file.txt # Cetak baris 10-20

Piping dan Redirection

Piping (|) menghubungkan output satu perintah ke input perintah lain. Redirection mengarahkan input/output ke file.

# Piping
dmesg | grep -i error | tail -20 # Cari error di kernel log
ps aux | sort -nrk 3 | head -5 # 5 proses dengan CPU tertinggi

# Redirection
echo "debug=true" > config.txt # Tulis file (overwrite)
echo "port=8080" >> config.txt # Append ke file
command 2> error.log # Redirect stderr
command &> all.log # Redirect stdout + stderr

Variables dan Command Substitution

# Variable
NAME="server1"
echo "Hostname: $NAME"

# Command substitution - output perintah disimpan ke variable
DATE=$(date +%Y%m%d)
LOGFILE="backup-$DATE.log"
echo $LOGFILE # Output: backup-20260721.log

Shell Scripting Dasar

Script dimulai dengan shebang #!/bin/bash.

Struktur dasar:

#!/bin/bash
# Script: system-check.sh

HOST=$(hostname)
UPTIME=$(uptime -p)
DISK=$(df -h / | awk 'NR==2 {print $5}')

echo "=== $HOST ==="
echo "Uptime: $UPTIME"
echo "Disk: $DISK"

Loop for:

#!/bin/bash
for user in alice bob charlie; do
echo "Processing user: $user"
sudo useradd -m "$user"
done

# Loop numeric
for i in {1..5}; do
echo "Iterasi ke-$i"
done

Kondisi if:

#!/bin/bash
DISK_USAGE=$(df / | awk 'NR==2 {print $5}' | sed 's/%//')

if [ $DISK_USAGE -gt 90 ]; then
echo "WARNING: Disk usage ${DISK_USAGE}%"
elif [ $DISK_USAGE -gt 75 ]; then
echo "NOTICE: Disk usage ${DISK_USAGE}%"
else
echo "OK: Disk usage ${DISK_USAGE}%"
fi

Function:

#!/bin/bash
info() {
local msg=$1
echo "[$(date '+%H:%M:%S')] INFO: $msg"
}

error() {
local msg=$1
echo "[$(date '+%H:%M:%S')] ERROR: $msg" >&2
}

info "Script started"
error "Something went wrong"

Sebuah script yang baik selalu diawali dengan shebang, memiliki exit code yang jelas (exit 0 untuk sukses, exit 1 untuk error), dan menggunakan variable dengan tanda kutip untuk mencegah word splitting.

PADA HALAMAN INI