#Why this matters
Graphical file managers and text editors hide exact paths, repeat poorly across dozens of servers, and struggle when a single log file holds millions of lines. In incident response, system administration, and capture-the-flag labs, analysts rely on the command line because every action is explicit, repeatable, and works inside a lightweight remote SSH session.
Building on how the kernel schedules processes and how filesystems organize directories, this lesson covers how a shell interprets commands, manages input and output streams, and composes small utilities into fast analysis pipelines.
#Terminals, shells, and command anatomy
A terminal (or terminal emulator) is the window that draws characters on screen and sends your keystrokes onward. Inside it runs a shell, a user-space program such as bash on Linux or pwsh (PowerShell) on Windows, which reads each line, expands variables or wildcards, and asks the kernel to run the requested command.
Most shell commands follow a simple structure:
command [options / flags] [arguments]Options modify behaviour (such as -l for long listing or -n for line numbers), while arguments name the targets (such as a file path or search pattern). When you type a bare command name like grep, the shell searches each directory in your PATH environment variable in order. If a script sits in your current directory and that directory is not in PATH, you invoke it explicitly with ./script.sh.
| Task | Bash (Linux/macOS) | PowerShell (Windows/cross-platform) |
|---|---|---|
| Print working directory | pwd |
Get-Location |
| List directory contents | ls -la |
Get-ChildItem -Force |
| Read file contents | cat app.log |
Get-Content app.log |
| Search text lines | grep "ERROR" app.log |
Select-String "ERROR" app.log |
| Show command help | man grep or grep --help |
Get-Help Select-String |
#Standard streams and redirection
When the kernel starts a process, it attaches three standard file descriptors:
- stdin (0): where the program reads input (by default, your keyboard).
- stdout (1): where the program writes normal results (by default, the terminal screen).
- stderr (2): where the program writes errors and diagnostics (also the terminal screen by default).
Keeping stdout and stderr separate lets you save clean results to a file while still seeing warnings on screen, or discard expected permission errors without hiding real data.
>overwrites a file withstdout.>>appendsstdoutto the end of a file.2>redirectsstderr(for example,2> /dev/nulldiscards error text on Linux).<feeds a file intostdin.
$ ls /etc/hosts /nonexistent > found.txt 2> errors.txt
$ cat found.txt
/etc/hosts
$ cat errors.txt
ls: cannot access '/nonexistent': No such file or directory#Composing pipelines to filter logs
Redirection writes to disk, whereas a pipe (|) connects stdout of the command on the left directly to stdin of the command on the right in memory. By chaining single-purpose tools, you can extract structured answers from raw logs in seconds.
- Read log (cat / tail)Filter lines (grep) | Pick fields (cut) | Sort & count (sort | uniq -c)
Suppose a lab web server writes space-separated entries to access.log. To count which IP addresses triggered 401 authentication failures, filter the status code, isolate the IP field, sort identical entries together, and count them:
$ cat access.log
2026-11-18T10:01:02Z 10.10.10.15 200 /index.html
2026-11-18T10:01:05Z 192.0.2.44 401 /admin
2026-11-18T10:01:06Z 192.0.2.44 401 /admin
2026-11-18T10:01:08Z 10.10.10.22 401 /login
2026-11-18T10:01:09Z 192.0.2.44 401 /admin
$ grep " 401 " access.log | cut -d' ' -f2 | sort | uniq -c | sort -nr
3 192.0.2.44
1 10.10.10.22Notice why sort comes before uniq -c: uniq only collapses adjacent matching lines, so unsorted duplicates will be counted separately. PowerShell follows the same pipeline idea with |, except stages pass structured objects rather than plain text, allowing commands like Get-Content access.log | Select-String " 401 " to filter cleanly without brittle spacing assumptions.
#Common mistakes
- Truncating an input file with
>: runningsort names.txt > names.txtopens and emptiesnames.txtbeforesortreads it. Write to a new file (sorted.txt) first, then inspect and rename. - Forgetting
sortbeforeuniq:uniqonly deduplicates consecutive lines, producing inaccurate counts on unsorted logs. - Unquoted spaces and globs: typing
cat my report.txtaskscatfor two separate files (myandreport.txt); wrap paths with spaces in quotes ("my report.txt"). - Piping untrusted scripts straight to a shell: patterns like
curl ... | bashexecute remote code before you can inspect it. Download to a file first and read it.
#Summary
- A terminal displays text while the shell parses commands, expands arguments, and searches
PATHto start processes. - Every process uses
stdin(0),stdout(1), andstderr(2); separatingstdout(>) fromstderr(2>) keeps saved output clean. - Pipes (
|) streamstdoutfrom one tool directly intostdinof the next, makinggrep,cut,sort, anduniq -cfast log-triage building blocks. - Always write redirected output to a new filename, quote paths with spaces, and inspect downloaded scripts before running them.