Skip to main content
Command Line & Shell Workflow
Lesson 4 of 10

Command Line & Shell Workflow

Navigate filesystems, redirect standard input and output streams, and chain commands with pipes in Bash and PowerShell to filter logs accurately.

Beginner 20 min IT foundations

Learning objectives

  • Distinguish between a terminal emulator, a shell interpreter, and the OS kernel
  • Navigate directories and inspect files using relative paths, absolute paths, and PATH lookup
  • Redirect standard output and standard error streams without corrupting input files
  • Build multi-stage text-filtering pipelines to summarize log files in Bash and PowerShell

Recommended first: Operating Systems, Kernels & ProcessesFilesystems, Inodes & Storage Layout

#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:

text
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 with stdout.
  • >> appends stdout to the end of a file.
  • 2> redirects stderr (for example, 2> /dev/null discards error text on Linux).
  • < feeds a file into stdin.
Separating stdout and stderr in Bash
$ 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.

  1. 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:

Counting 401 responses per IP in a lab log
$ 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.22

Notice 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 >: running sort names.txt > names.txt opens and empties names.txt before sort reads it. Write to a new file (sorted.txt) first, then inspect and rename.
  • Forgetting sort before uniq: uniq only deduplicates consecutive lines, producing inaccurate counts on unsorted logs.
  • Unquoted spaces and globs: typing cat my report.txt asks cat for two separate files (my and report.txt); wrap paths with spaces in quotes ("my report.txt").
  • Piping untrusted scripts straight to a shell: patterns like curl ... | bash execute 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 PATH to start processes.
  • Every process uses stdin (0), stdout (1), and stderr (2); separating stdout (>) from stderr (2>) keeps saved output clean.
  • Pipes (|) stream stdout from one tool directly into stdin of the next, making grep, cut, sort, and uniq -c fast log-triage building blocks.
  • Always write redirected output to a new filename, quote paths with spaces, and inspect downloaded scripts before running them.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge