# The command line

> Open a terminal, move around your files, create and delete folders, and run programs by typing commands.

Learn to Code roadmap · Getting started · 14 min read · https://levelcode.org/roadmaps/learn-to-code/command-line

Programmers spend a lot of time in a window full of text called the **terminal**. It looks old-fashioned, but it's the fastest way to run programs, install tools and work with files, and nearly every tutorial, tool and job assumes you can use it. This lesson teaches the dozen commands you'll use every day. You don't need to memorise anything: you'll use these so often that they'll stick on their own.

## Opening a terminal

The terminal is a program that runs a **shell**: a program that waits for you to type a command, runs it, and prints the result.

- **macOS**: open *Terminal* (press Cmd+Space, type "Terminal", press Enter). The shell is called zsh.
- **Linux**: open *Terminal* from your applications menu, or press Ctrl+Alt+T on many distributions. The shell is usually bash.
- **Windows**: open *Windows Terminal* or *PowerShell* from the Start menu. The commands in this lesson work in PowerShell too, with the differences noted along the way.

You'll see a **prompt**, something like:

```text
ana@laptop ~ %
```

It shows your username, your computer's name and where you are (`~` means your home folder). The prompt is the shell saying "I'm ready". In this course, commands are shown without the prompt, so when you see:

```bash
pwd
```

type `pwd` and press Enter.

## Where am I? Folders and paths

Your files are organised in folders (also called **directories**) inside other folders, like a tree. The shell is always "in" one folder, called the **current directory** or working directory.

```bash
pwd
```

```text
/Users/ana
```

`pwd` means "print working directory". The answer is a **path**: the route from the top of the tree (`/`, called the root) down to where you are. On Windows paths look like `C:\Users\ana`, with backslashes.

To see what's in the current folder, list it:

```bash
ls
```

```text
Desktop    Documents    Downloads    Music    Pictures
```

Many commands accept **options** (also called flags) that change what they do. `ls -l` shows a long listing with sizes and dates; `ls -a` shows hidden files, whose names start with a dot.

```bash
ls -la
```

## Moving around

`cd` means "change directory":

```bash
cd Documents     # go into the Documents folder
pwd              # /Users/ana/Documents
cd ..            # go up one level, to the parent folder
cd ~             # go to your home folder from anywhere
cd               # on macOS and Linux, plain cd also goes home
```

Everything after `#` is a **comment**: a note for humans that the shell ignores. You don't need to type the comments.

There are two kinds of path:

- An **absolute path** starts at the root and works from anywhere: `/Users/ana/Documents/notes.txt`.
- A **relative path** starts from where you are now: if you're in `/Users/ana`, then `Documents/notes.txt` means the same file.

Two special names help with relative paths: `.` means "this folder" and `..` means "the parent folder". So `../Downloads` means "go up one level, then into Downloads".

> [!TIP]
> Press **Tab** to auto-complete names. Type `cd Doc` and press Tab, and the shell completes `Documents/`. If several names match, press Tab twice to see them. Tab completion saves typing and, more importantly, prevents typos.

Press the **up arrow** to bring back previous commands. Press **Ctrl+C** to stop a command that's running (or to abandon a line you're typing). These three keys will save you hours.

## Creating, copying, moving and deleting

Make a folder for your coding work:

```bash
cd ~
mkdir code                 # make directory
cd code
mkdir -p projects/hello    # -p creates the parent folders too, if needed
```

Create an empty file and see its contents:

```bash
touch notes.txt            # create an empty file (PowerShell: New-Item notes.txt)
cat notes.txt              # print a file's contents (empty for now)
```

Copy, move and rename:

```bash
cp notes.txt notes-backup.txt      # copy
mv notes-backup.txt projects/      # move into a folder
mv notes.txt todo.txt              # "move" to a new name = rename
```

Delete:

```bash
rm todo.txt                 # delete a file
rm -r projects/hello        # delete a folder and everything inside it
```

> [!WARNING]
> The command line has no recycle bin. `rm` deletes immediately and permanently. Double-check the path before pressing Enter, and be especially careful with `rm -r`. Never run a delete command you've copied from the internet without understanding exactly what it removes.

## Names with spaces

The shell splits commands on spaces, so `cd My Projects` means "cd with two arguments: `My` and `Projects`", which fails. Wrap names with spaces in quotes:

```bash
cd "My Projects"
```

This is why programmers usually name files and folders without spaces, using dashes or underscores: `my-projects`, `budget_2026.csv`.

## Running programs

Every command you've typed is a program. `ls` is a small program stored somewhere on your computer, and the shell finds it by name. When you install Python in the next lesson, you'll run it the same way:

```bash
python3 --version
```

The pattern is always **program, options, arguments**:

```text
cp        -r          photos    photos-backup
program   option      argument  argument
```

To learn what a command can do, ask it. Most programs print help with `--help`, and on macOS and Linux `man ls` opens the full manual (press `q` to quit, space to page down).

When a command fails, read the message. "No such file or directory" means the path is wrong: check your current folder with `pwd` and look at what's there with `ls`. "Permission denied" means you're not allowed to change that file. "command not found" means the program isn't installed, or the name is misspelled.

## A reference card

| Task | macOS / Linux | PowerShell |
|---|---|---|
| Where am I? | `pwd` | `pwd` |
| List files | `ls`, `ls -la` | `ls`, `ls -Force` |
| Change folder | `cd folder`, `cd ..`, `cd ~` | same |
| Make a folder | `mkdir name`, `mkdir -p a/b` | `mkdir name` (creates parents automatically) |
| Create an empty file | `touch file.txt` | `New-Item file.txt` |
| Show a file | `cat file.txt` | `cat file.txt` |
| Copy | `cp a b`, `cp -r dir1 dir2` | `cp a b`, `cp -r dir1 dir2` |
| Move or rename | `mv a b` | `mv a b` |
| Delete | `rm file`, `rm -r folder` | `rm file`, `rm -r folder` |
| Clear the screen | `clear` (or Ctrl+L) | `clear` |

## Practice

Using only the terminal:

1. Go to your home folder and create this structure:

   ```text
   code/
     practice/
       notes/
         ideas.txt
       archive/
   ```

2. Copy `ideas.txt` to `archive/ideas-old.txt`.
3. Rename `notes/ideas.txt` to `notes/plans.txt`.
4. List everything inside `practice`, including subfolders. (Hint: `ls -R` on macOS and Linux, `ls -Recurse` in PowerShell.)
5. Delete the `archive` folder and everything in it.
6. Without using `cd`, list the contents of `notes` from your home folder using a relative path.

**Solution**

```bash
cd ~
mkdir -p code/practice/notes code/practice/archive
cd code/practice
touch notes/ideas.txt            # PowerShell: New-Item notes/ideas.txt
cp notes/ideas.txt archive/ideas-old.txt
mv notes/ideas.txt notes/plans.txt
ls -R                            # PowerShell: ls -Recurse
rm -r archive
cd ~
ls code/practice/notes
```

`ls -R` prints:

```text
archive	notes

./archive:
ideas-old.txt

./notes:
plans.txt
```

`mkdir -p` accepts several paths at once and creates any missing parents, so one command builds both branches of the tree. The final `ls code/practice/notes` works from your home folder because the path is relative to where you are; from anywhere else, you'd use the absolute path or `~/code/practice/notes`.

## Key takeaways

- The terminal runs a shell, which runs the commands you type: program, then options, then arguments.
- `pwd`, `ls` and `cd` tell you where you are, what's there and move you around; `.` is here and `..` is the parent.
- `mkdir`, `touch`, `cp`, `mv` and `rm` create, copy, move, rename and delete; `rm` is permanent.
- Tab completes names, the up arrow repeats commands, and Ctrl+C stops what's running.
- When something fails, read the message, check `pwd` and `ls`, and avoid spaces in file names.

## Quiz

1. You're in /Users/ana/code. Which path refers to /Users/ana/Downloads?

   A. ./Downloads
   B. ../Downloads
   C. Downloads
   D. /Downloads

   **Answer: B.** .. is the parent folder (/Users/ana), so ../Downloads goes up one level and into Downloads. /Downloads would be a folder at the very top of the disk.

2. What does mv report.txt final-report.txt do?

   A. Copies the file
   B. Deletes report.txt
   C. Renames report.txt to final-report.txt
   D. Creates an empty file

   **Answer: C.** Moving a file to a new name in the same folder is how you rename it. cp would copy it and leave the original.

3. Why does cd My Documents fail?

   A. The shell treats My and Documents as two separate arguments; use quotes around names with spaces
   B. cd can't enter folders in your home directory
   C. Folder names must be lowercase
   D. You need to type cd -r

   **Answer: A.** Spaces separate arguments. "My Documents" in quotes is one argument, which is what cd needs.

4. You ran rm -r old-project by mistake. How do you get it back?

   A. Run undo
   B. Open the recycle bin
   C. Press Ctrl+Z
   D. You usually can't: rm deletes permanently, so restore from a backup if you have one

   **Answer: D.** rm bypasses the trash entirely. That's why you check paths before deleting, and why backups and version control matter.
