Course contents · Lesson 4 of 19

Command Line Basics: Talk to Your Computer in Text

The Command Line · Lesson 4 of 19 · 9 min Foundations

A guide says "open a terminal and type this", and you face a dark window of text that seems to punish every typo. This lesson covers command line basics: what the terminal and the shell are, how a command is built, nine first commands, getting help, and deleting safely.

Terminal, shell and command

The terminal is the window: an app that passes what you type to the shell and shows its replies. macOS and Linux call their app Terminal; on Windows, PowerShell opens in a similar window.

The shell is the program that reads each command and runs it (lesson 2). Most Linux distributions use bash (the Bourne Again Shell), and macOS has used zsh (the Z shell) by default since version 10.15. Windows has PowerShell, Microsoft's own shell.

A command is one instruction. Usually it names a program, which the shell starts as a new process (lesson 3); a few, such as cd, are built into the shell. When it is ready, the shell prints a prompt, such as asha@MacBook-Air ~ % on a Mac: your user name, the computer, the folder you are in (~ is your home folder) and %. Bash prompts usually end in $. You never type the prompt.

Working by typing is a command-line interface (CLI); windows, icons and clicks are a graphical user interface (GUI). Most servers have no screen, so engineers manage them through a CLI.

The anatomy of a command

The shell splits each command into parts at the spaces. Take ls -l /etc, which lists the folder /etc in detail. In the diagram, notice the order.

A terminal window shows the prompt, a dollar sign typed by the shell, then the command ls -l /etc. Brackets under each part lead to three labels: ls is the command, which program to run, here list. -l is the option or flag, how it works, here a long format with details on each line. /etc is the argument, what to act on, here a folder. A footer shows the same shape in cp -i notes.txt backup.txt: a command, an option and two arguments
A command is a program name, then options that say how, then arguments that say what.

The first word, ls (list), is the command. Next come options, also called flags: they start with a dash and change how the command works. -l asks for the long format: one line per item, with details such as size, owner and date changed.

Last come arguments: what to act on, here /etc, the folder where Linux keeps system settings (lesson 5). Without an argument, ls lists the folder you are in. Spaces and capitals matter: ls-l is one unknown word, so the shell replies "command not found", and -L is a different option from -l.

First commands: where am I, and what is here?

The shell is always in some folder, called its working directory (a directory is a folder). Commands act there unless you name another place, and a new terminal usually starts in your home folder.

pwd (print working directory) shows where you are. ls lists what is there. cd (change directory) moves you: cd Documents goes into a folder and cd .. goes up one level. mkdir (make directory) creates a folder, echo prints the text you give it, and cat prints what is inside a file.

In the session below, > sends echo's text into a file instead of onto the screen, replacing anything already in that file (lesson 8). Notice the quiet steps.

Six steps from top to bottom, each command on the left and the reply on the right. pwd, print working directory, replies /home/asha. mkdir practice, make a folder, prints nothing because it worked. cd practice, move into it, prints nothing, and you are now in /home/asha/practice. echo hello > note.txt prints nothing, and hello went into note.txt. ls, list what is here, replies note.txt. cat note.txt, show what is inside, replies hello. A note says the user name asha is illustrative and on a Mac pwd shows /Users/asha
Quiet steps are good news: a command that works often prints nothing at all.

Copy, move and delete

Copying and moving take the source first, then the target. cp notes.txt notes2.txt copies a file, and cp -r copies a folder with everything inside. mv notes.txt old.txt renames a file, and mv old.txt reports moves it into the folder reports: a rename is just a move to a new name.

rm (remove) deletes a file, and rm -r deletes a folder and its contents. In the diagram, the last row is the one job that differs.

Two columns compare clicks in Finder or File Explorer with commands in the shell. Double-click Documents is cd Documents. See what is inside is ls. New Folder named reports is mkdir reports. Copy and paste a file is cp notes.txt notes2.txt. Rename a file is mv notes.txt old.txt. In the last row, highlighted as a warning, Move to Trash or Recycle Bin, which you can still undo, is not quite rm old.txt, which deletes at once with no Trash and no undo
Each click has a typed twin, except that rm skips the Trash and cannot be undone.

PowerShell maps most of these names to its own commands (ls runs Get-ChildItem), but their options differ, so the rest of the course uses Linux.

Getting help

On macOS and Linux, man ls opens the manual page for ls, with every option. Press Space for the next page and q to quit. Most Linux commands also print a short summary with --help; many macOS versions do not understand it, so use man there. PowerShell has Get-Help.

With Tab completion, you type the first letters of a command or file name, press Tab, and the shell finishes it. If several names fit, press Tab twice to list them.

The up arrow brings back earlier commands to fix and rerun; history lists them in bash and zsh. If a command seems stuck, Ctrl+C stops it.

Safety habits: rm has no Recycle Bin

rm skips the Trash and Recycle Bin: the file is gone at once, with no undo and no "are you sure?". So build these habits:

  • Run pwd first, to know which folder you are acting in.
  • Run ls on the exact name, and check that it shows only what you expect.
  • Complete names with Tab, so a typo cannot point at the wrong file.
  • Use rm -i, which asks before each file.
  • Slow down for rm -r, and never run a pasted command you do not understand.

Watch spaces: rm old notes.txt tries to delete two files, old and notes.txt. Quote such names: rm "old notes.txt". cp and mv also replace a same-named file without asking; -i makes them ask.

Check yourself

After mkdir reports, the shell just shows the prompt again. Did it fail?

No. Most commands print nothing on success and an error on failure. Run ls to see the new reports folder.

final.txt already exists. Why does cp -i draft.txt final.txt ask a question that cp draft.txt final.txt does not?

-i is an option: it changes how cp works, making it ask before replacing an existing final.txt. The file names are arguments: they only say what to copy and where.

To delete a file called old notes.txt, you type rm old notes.txt. Why is this risky, and what is safer?

The space makes two arguments, so rm tries to delete old and notes.txt, which may be real files, gone for good. Quote the name or let Tab complete it, and check it with ls first.