Skip to content
Tutorials Center
My learning

Topics

Lesson 43 of 55 ยท Working with Computers

The Command Line: First Steps

A beginner's guide to the command line: why use a terminal, how to navigate, list, create and read files, connect commands with pipes, and delete safely.

Before graphical desktops, every computer was driven by typing. You wrote a short command, pressed Enter, and the machine answered with text. That way of working never went away. Programmers, system administrators, scientists and cloud engineers still spend much of their day in a terminal, because typed commands can be repeated, saved in a script, combined and run on a machine that has no screen at all.

This lesson teaches the first steps: what the terminal and shell are, how to move around, how to look at and create files, how to connect small commands with pipes, and how to stay safe with commands that delete. The real commands in this lesson are shown as read-only text because the site cannot give you a real shell. Instead, the TryIt blocks contain a small model of a shell that you can run, so you can see exactly what each idea does. It builds on File Systems and the shell described in What an Operating System Does.

Terminal, shell and command

Three words are often mixed up.

  • The terminal is the window where you type and read text.
  • The shell is the program running inside it that reads each line, works out what you mean and starts the right program. Common shells are Bash and Zsh (macOS and Linux) and PowerShell (Windows).
  • A command is a line you type: a program name followed by arguments and options that change its behavior.
Terminal
ls -l documents

Here ls is the program, -l is an option (long format) and documents is an argument (what to list). Options usually start with -, and the exact set depends on the program. Most programs explain themselves if you run them with --help, and on Linux and macOS man ls opens a manual page.

Why bother with typing when you can click? Because a command line is precise (it says exactly what to do), repeatable (you can run the same line again, or put 50 lines in a file), composable (small tools are chained together) and remote (you can control a server on the other side of the world over a text connection).

Where am I? Navigating

The shell always has a current working directory: the folder that relative paths are measured from. You met paths in File Systems; the same rules apply here.

Terminal
pwd                  # print working directory: /home/sam
ls                   # list what is in the current directory
cd documents         # move into documents
cd ..                # move up one level
cd /                 # jump to the root
cd ~                 # jump to your home directory (Unix shells)

Lines that start with # are comments. The shell ignores them, so they are a handy way to leave a note.

Windows Command Prompt and PowerShell use similar ideas. cd works the same, dir (or ls in PowerShell) lists a folder, and Get-Location (or pwd) shows where you are. Paths use \ and begin with a drive letter such as C:\Users\Sam, but PowerShell also accepts /.

Two shortcuts will save you many keystrokes. Press Tab to auto-complete a file name, and press the Up arrow to bring back earlier commands. Beginners who type everything in full make far more typos.

Looking, creating and reading

A small set of commands covers most everyday work.

Task Linux / macOS PowerShell
List files ls ls or Get-ChildItem
Make a folder mkdir notes mkdir notes
Make an empty file touch a.txt New-Item a.txt
Show a file cat a.txt cat a.txt or Get-Content a.txt
Copy cp a.txt b.txt cp a.txt b.txt
Move or rename mv a.txt c.txt mv a.txt c.txt
Delete a file rm a.txt rm a.txt

Text goes out of commands to the screen, and you can send it somewhere else with redirection. echo hello > greeting.txt writes the word into a file (replacing what was there), and echo again >> greeting.txt appends a new line. That is how scripts build files without opening an editor.

The first stand-in is a tiny shell written in JavaScript. It keeps a toy file system in memory, has a current directory and understands pwd, ls, cd, mkdir, touch, echo, cat, redirection with > and >>, and rm. Read the script at the bottom to see the whole session, then edit it and run your own commands.

JS A mini shell: navigate, create, read

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice the last two lines. A command that does not exist, or a path that does not exist, produces an error message instead of crashing the shell. Read error messages carefully: they usually say exactly what is wrong.

Pipes: small tools, big results

The idea that makes the command line powerful is the pipe, written |. Each program can read text from its standard input and write text to its standard output. A pipe connects the output of one command directly to the input of the next, so you can build a custom tool from tiny parts.

flowchart LR
  F["todo.txt"] --> C["cat"]
  C -->|text| G["grep milk"]
  G -->|matching lines| W["wc -l"]
  W --> S["screen: 2"]

A pipeline: each command passes its output to the next as input.

Terminal
cat todo.txt | grep milk | wc -l     # count lines that contain "milk"
ls | sort | head -3                  # first three names in sorted order

Every tool does one small thing: grep keeps lines that match a word, sort orders lines, wc -l counts lines, head keeps the first few. Because they all speak plain text, any can be combined with any other. This design choice from the early Unix days is a big reason command-line tools have lasted for fifty years.

The second stand-in adds pipes. Each stage receives the text from the stage before it, and the stages here are grep, sort, wc and uniq.

JS Pipes: connecting commands

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last pipeline answers "how many different people logged in?" without writing a program. It reads the log, keeps login lines, cuts out the name, sorts so equal names are neighbours, removes duplicates and counts. Real uniq also only removes adjacent duplicates, which is why sort comes first.

Staying safe with delete commands

The terminal trusts you completely. When you say delete, it deletes, and there is usually no Recycle Bin and no confirmation. The file system only forgets the file (see File Systems), but recovery is never guaranteed.

rm -rf folder removes a folder and everything inside it without asking. A typo such as an extra space, a wrong variable or a wildcard like * in the wrong directory can destroy far more than you meant. Never paste a command you do not understand, and be extra careful with commands that begin with sudo, which runs them with administrator rights.

Habits that keep you out of trouble:

  • Run pwd and ls first, so you know where you are and what a wildcard will match.
  • Try ls *.tmp before you run rm *.tmp. The first shows exactly what the second will touch.
  • Use the interactive flag when you are unsure: rm -i old.txt asks before each deletion.
  • Prefer moving things to a spare folder over deleting them, then clean up later.
  • Keep backups of anything you cannot recreate.
  • Never run a long command from an unknown source without reading it.

The third stand-in adds a "dry run" to the mini shell idea: it lists what a pattern would delete before it deletes anything, and refuses the most dangerous target outright.

JS A safer delete: preview first

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What is real and what is the stand-in? The commands, options, pipes, redirection and safety habits are real and work the same in Bash, Zsh and (mostly) PowerShell. The shells above are JavaScript models: a real shell starts separate programs, connects them with operating-system pipes and works on a real disk. PowerShell pipes objects rather than plain text, which is a notable difference.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the shell?

    The terminal is the window; the shell (Bash, Zsh, PowerShell) interprets what you type.
  2. 2. You are in /home/sam and run cd ../.. . Where do you end up?

    Each .. goes up one level: /home/sam to /home, then to /.
  3. 3. What does the pipe in ls | wc -l do?

    A pipe connects standard output of the first command to standard input of the next. Saving to a file would use the > redirection.
  4. 4. Which habit is the best protection before running rm *.tmp?

    Previewing the wildcard shows what will be deleted. Adding sudo only makes mistakes more powerful.
  5. 5. What does echo hi >> log.txt do?

    appends, while a single > overwrites the file.

Summary

  • The terminal shows text, the shell interprets it, and a command is a program name plus options and arguments.
  • Navigate with pwd, ls and cd; paths can be absolute or relative, with . and .. as shortcuts.
  • Create and read with mkdir, touch, echo, cat; use > to overwrite and >> to append.
  • Pipes connect small tools into a pipeline, because they all read and write plain text.
  • Deletion is immediate and usually unrecoverable: preview with ls, use rm -i, and keep backups.
  • Read error messages; they tell you what went wrong.

Next, move from commands to tables of numbers: Spreadsheets and Data Basics.