Shell & Terminal Basics

The keyboard-driven interface used by every Linux admin.

What the Shell Actually Is

The shell is a command interpreter — it reads your input, finds the right program to run, executes it, and shows you the output. bash is the most common shell on Linux servers. zsh is popular on desktops (it's bash-compatible with nicer defaults).

The prompt tells you who you are and where you are. A $ means normal user. A # means root — be careful.

michael@npmserv:~$        ← normal user, home directory
root@npmserv:/etc#        ← root, in /etc — dangerous

Anatomy of a command:
  ls   -la   /var/log
  │     │        └── argument (where)
  │     └────────── flag (how)
  └──────────────── command (what)

Essential Commands

Navigation & files:
  pwd              → print working directory (where am I?)
  ls -la           → list files, long format, show hidden
  cd /etc/nginx    → change to absolute path
  cd ..            → go up one directory
  cd ~             → go home
  mkdir -p a/b/c   → create nested directories

Files:
  cp src dest      → copy
  mv src dest      → move or rename
  rm file          → delete file
  rm -rf dir/      → delete directory (no undo — be careful)
  touch file.txt   → create empty file or update timestamp

Viewing:
  cat file         → print whole file
  less file        → page through file (q to quit)
  head -20 file    → first 20 lines
  tail -f file     → follow file live (great for logs)

Pipes & Redirection

The pipe (|) is one of Linux's most powerful ideas: chain commands together so the output of one becomes the input of the next.

Pipes:
  ps aux | grep nginx          → find nginx processes
  cat /var/log/auth.log | grep "Failed" | tail -20
  journalctl -u nginx | grep "error"

Redirection:
  cmd > file.txt               → write stdout to file (overwrite)
  cmd >> file.txt              → append stdout to file
  cmd 2> errors.txt            → write stderr to file
  cmd > out.txt 2>&1           → stdout + stderr to same file
  cmd < input.txt              → feed file as stdin

Useful combos:
  grep -r "password" /etc/ 2>/dev/null    → suppress permission errors
  ls -la | sort -k5 -rn | head -10       → largest files first

Tab Completion & History

Two habits that separate fast operators from slow ones: tab completion and history search. Use them constantly.

Tab completion:
  type "sys" then Tab → completes to "systemctl" (or shows options)
  type "/etc/ngi" Tab → completes to "/etc/nginx/"

History:
  ↑ / ↓              → scroll through recent commands
  Ctrl+R             → reverse search (type part of a command)
  history            → show full history list
  !42                → re-run command #42 from history
  !!                 → re-run last command
  sudo !!            → re-run last command with sudo

Getting Help

man ls                  → full manual page for ls (q to quit)
ls --help               → shorter built-in help
type ls                 → is this an alias, function, or binary?
which nginx             → where is the nginx binary?
whereis nginx           → binary + man page locations

Searching man pages:
  man -k "disk usage"   → find commands related to disk usage
  apropos copy          → same thing

Environment Variables

Environment variables store configuration that programs can read. PATH tells the shell where to look for commands. Getting comfortable with them is essential for scripting and service configuration.

echo $HOME          → /home/michael
echo $PATH          → where shell looks for commands
echo $USER          → current username
printenv            → print all environment variables

Set a variable (current session only):
  export MY_VAR="hello"
  echo $MY_VAR

Persistent variables → add to ~/.bashrc or ~/.bash_profile:
  export EDITOR=vim
  export PATH="$PATH:/opt/mytools/bin"

≈·*•—[ A|S ]—•*·≈

- Crafted by Axiom|Spectre