Skip to content

17 min read

Basic terminal commands for Mac and Linux

In one sentence: The terminal is the window where you type; the shell is the program that reads what you type and runs it. With three commands, pwd, ls and cd, you already know where you are, what's there and how to move around.

What you will learn

  • Tell the terminal from the shell, and recognize the command, its options and its arguments in an instruction.
  • Move through folders with pwd, ls and cd.
  • Write absolute and relative paths, and use the shortcuts ~ (your folder), . (here) and .. (the folder above).
  • Ask the terminal itself for help with man and which, and type less with tab completion and history.

Imagine you’ve just logged in to a server. There’s no desktop, no windows and no mouse: only a line like ubuntu@course:~$ and a blinking cursor. There’s no Finder or File Explorer: to find out what’s in a folder, you have to ask in writing. In Phase 8 you’ll rent a server on the internet, and that will be the only way into it.

In Phase 0 you already typed commands in the TerminalAn application where you give the computer instructions by typing text instead of clicking. Almost every server is managed this way.Go to definition: curl, dig, nc. They worked, but you probably copied them without really knowing which part was which. It was like repeating sentences in a language you don’t speak. In this lesson you start learning that language.

Typing has another advantage, too. A written instruction can be repeated, saved in a file so it runs by itself (you’ll do that in lesson 8) or handed to someone else so they do exactly the same thing. You can’t copy and paste a click.

The analogy

Imagine a huge building with no windows. From the lobby, stairs lead down to several rooms; from each room, more stairs lead down to more rooms, and so on for many floors. The rooms hold papers. The rooms are the folders on your computer, and the papers, the files.

You’re always in a single room, and you can’t see anything else. To find something out, you pass notes to a very literal receptionist: “Where am I?” (pwd), “What’s in this room?” (ls) or “Take me to such-and-such room” (cd). That receptionist is the ShellThe program that reads what you type in the terminal, interprets it and runs it. The most used are bash, the one on almost every Linux, and zsh, the one on macOS.Go to definition.

For them to take you somewhere, you give them directions. If they’re complete, starting from the lobby (“go down to Staff, then to Ana, then to Course”), they work wherever you are: that’s an absolute PathThe address of a file or a folder inside the computer, with the folder names separated by slashes, like /Users/ana/backend-course. It’s absolute if it starts at the root (/) and relative if it starts from where you are.Go to definition. If they start from where you are (“the room above”), it’s a relative path.

Where the analogy breaks down

  • In a real building, when you walk into a room you see what’s there. Not in the terminal: you only know what you ask.
  • The receptionist doesn’t guess: one misspelled name, one extra capital letter or one space without quotes, and they don’t understand you.
  • They don’t ask “Are you sure?” either. If you ask them to delete something, they delete it, and there’s no trash. You’ll see it in lesson 2.

Open the terminal (if you don’t know how, see the Phase 0 introduction). You’ll see a window with a bit of text and a cursor. It looks like one thing, but it’s two:

  • The terminal is the application: the window that shows the text and picks up what you type.
  • The shell is the program running inside it. It reads what you type, interprets it and runs it: it’s the receptionist from the analogy.

There are several shells. Macs have come with one called zsh since 2019; Linux and WSL almost always come with one called bash. Everything in this lesson works the same in both, and when something changes, we’ll tell you. (If your Mac says /bin/bash, your account dates from before 2019; the commands are the same.)

The text in front of the cursor is the PromptThe text the shell writes before the cursor, like “ana@laptop ~ %”, to tell you it’s waiting for a command.Go to definition: it means “I’m waiting for an instruction.” It usually includes your user, the computer’s name and the folder you’re in. On a Mac it looks something like ana@laptop ~ %, and on Linux or WSL, like ana@laptop:~$.

On this site, the prompt is shortened to a $ in front of each command. You don’t type that $, and the copy button already leaves it out.

When you type an instruction and press Enter, this happens:

  1. You typels -l

    the terminal hands it the text

  2. The shellzsh or bash

    finds the program and starts it

  3. The ls program/bin/ls

    reads the folder and writes the list

  4. The terminalshows it to you

The shell doesn’t know how to list folders. What it does is find a program that does, ls, which is a file stored at /bin/ls (on Linux, at /usr/bin/ls), and start it. When it finishes, the shell prints the prompt again. Almost every command is a program like that: a file stored in some folder on the disk.

The shell cuts each instruction into pieces at the spaces:

Part In ls -l /usr/bin What it is
Command ls The program you want to run
Option -l Changes how it works. Starts with - (or with -- if it’s a word, like --help)
Argument /usr/bin What it works on: here, the folder you want to list

The shell doesn’t know what -l means: the first piece tells it which program to start, and it passes the rest along as is. It’s ls that decides -l means “long format.” That’s why every program has its own options.

Three rules:

  • One-letter options can be combined: ls -la is the same as ls -l -a.
  • Options that are a word take two dashes: --help.
  • Spaces separate the parts, so a name with spaces goes in quotes: cd "My documents". Without them, the shell sees two arguments, My and documents.

In the terminal, folders are also called DirectoryAnother word for a folder. The terminal uses it a lot.Go to definition.

Everything hangs from a single folder, the root, written /: it’s the lobby from the analogy. On Mac and Linux there are no drives like Windows’ C:: everything is somewhere below /.

Your home folder, the one with your documents and downloads, has a shortcut: ~ (the tilde). On a Mac it’s /Users/ana, and on Linux and WSL, /home/ana, with your user instead of ana. On a US keyboard, ~ is Shift plus the key to the left of 1.

This is part of a Mac’s tree. Names that end in / are folders:

  • Directory/
    • Directorybin/
      • ls the ls program (on Linux it’s in /usr/bin)
    • DirectoryUsers/ on Mac, the home folders (on Linux it’s called home)
      • Directoryana/ your home folder, ~
        • Directorybackend-course/
          • Directoryphase-1/
            • …
    • Directoryusr/
      • Directorybin/ more programs
        • …
    • Directorytmp/ temporary files
      • …

Every folder has a unique address, its path: the folders you go through from the root, separated by /. Your practice folder’s is /Users/ana/backend-course/phase-1. The slash has two roles: at the start it’s the root, and between two names, a separator.

Where you are and what’s there: pwd and ls

Section titled “Where you are and what’s there: pwd and ls”

In the terminal you’re always in one folder, your current folder, just like in the building. When you open the terminal, you start in your home folder. In WSL, it sometimes opens in a folder like /mnt/c/Users/Ana, which is your Windows folder: type cd, press Enter and you’ll be in your Linux one.

  • pwd (print working directory) prints the full path of your current folder.
  • ls (from list) shows what’s in it. Given a path, it shows what’s in another one: ls /usr/bin.
  • ls -l (from long) shows one line per file, with more details: the size, the date, the owner. The letters at the start, like drwxr-xr-x, are the permissions, which you’ll see in lesson 4.
  • ls -a (from all) also shows hidden files: the ones that start with a dot, like .zshrc. They don’t appear in a normal ls or in the Finder. They’re usually configuration files, and they’re hidden to stay out of the way.
  • ls -la combines the two.

cd (change directory) moves you:

  • cd phase-1 goes into phase-1, if it’s inside where you are.
  • cd .. goes up to the folder above.
  • cd on its own, or cd ~, takes you back to your home folder.
  • cd - takes you back to the previous folder, like the browser’s back button, and tells you which one it is.

If all goes well, cd prints nothing: in the terminal, silence is usually a good sign.

ls is a separate program, but cd is part of the shell itself: it’s what’s called a builtin. It has to be. Every running program, every ProcessA running program. When you start a server, the operating system creates a process that stays alive waiting for requests.Go to definition, has its own current folder. A separate cd would change its own folder and exit, and you’d stay where you were. It would be as if the receptionist, instead of taking you, sent a messenger: the messenger would get there, but you wouldn’t.

That’s why the shell itself makes the change. type cd confirms it: it answers cd is a shell builtin.

You’re in /Users/ana/backend-course and you want to go into phase-1. You can give the shell the full way there, cd /Users/ana/backend-course/phase-1, or just the step you’re missing, cd phase-1. Both take you to the same place, but they don’t work the same way:

  • Absolute: the first one. It starts with / and describes the full way from the root. It works wherever you are.
  • Relative: the second one. It doesn’t start with / and starts from your current folder. cd phase-1 only works if phase-1 is where you are.

And three shortcuts:

  • ~ is your home folder. The shell replaces it with the full path, so ~/backend-course works from anywhere, like an absolute path.
  • . is the folder you’re in: ./phase-1 is the same as phase-1.
  • .. is the one above, and it can be repeated: ../.. goes up two levels.

More examples, also from /Users/ana/backend-course:

To go to Absolute path Relative path
The phase-1 folder /Users/ana/backend-course/phase-1 (or ~/backend-course/phase-1) phase-1 (or ./phase-1)
Your home folder /Users/ana (or ~) ..
The root / ../../..

The absolute one doesn’t depend on where you are; the relative one does.

Two helpers save you almost all the typing.

The Tab key. Type the start of a name and press Tab: the shell completes it. cd ~/bac becomes cd ~/backend-course/. It works with folders, files and commands. If several names start the same way, it completes what they share; press Tab again and it shows you the options. And if it completes nothing, that name doesn’t exist where you think it does.

The history. ↑ and ↓ step through the instructions you’ve already typed: you can change them or repeat them with Enter. Ctrl + R searches the history: type a piece and the last instruction that contained it appears. history shows the numbered list. In zsh only the last 16 appear; with history 1, all of them.

The keys work the same in zsh and bash.

man ls opens the manual for ls. It takes up the whole window: Space moves forward, / searches (type and press Enter) and Q quits. It’s dense: use it to look up a specific option, not to read it cover to cover.

Many programs give a summary with --help: curl --help, or ls --help on Linux. But the Mac’s ls doesn’t understand it. Mac and Linux come with different versions of the basic tools, very similar, but not the same.

which ls tells you where a program is: /bin/ls on a Mac and /usr/bin/ls on Linux. With cd it gives no path: zsh tells you it’s part of the shell, and bash prints nothing.

The goal: move around your computer without the mouse and, at the end, complete a challenge without copying a single instruction. If you don’t have the practice folder yet, create it as explained in the introduction. Everything happens in your terminal, and no exercise creates, changes or deletes anything: you only look and move around.

The outputs are from a Mac, with the user replaced by an example one, ana. In each exercise we tell you what changes on Linux and WSL.

1. Which shell do you use?

Ventana de terminal
echo $SHELL

What you will see:

/bin/zsh

echo prints whatever you ask it to, and $SHELL is a variable: a name that holds a value, here the shell you use. You’ll see them in lesson 7, “Environment variables”.

You do type this $, because it’s stuck to the variable’s name. The prompt’s one stands alone, at the start of the line.

On Linux and WSL you get /bin/bash. On Windows, if nothing comes out, you’re in PowerShell and not in Ubuntu: open “Ubuntu” from the Start menu, as in Phase 0.

2. Where you are and what's there

Ventana de terminal
cd ~/backend-course/phase-1
pwd
ls -la

What you will see:

/Users/ana/backend-course/phase-1
total 0
drwxr-xr-x@ 2 ana staff 64 Oct 12 15:29 .
drwxr-xr-x@ 3 ana staff 96 Oct 12 15:29 ..

You can paste the three instructions together and press Enter: the shell runs them one after another. If the first line answers no such file or directory, you haven’t created the practice folder yet: create it as in the introduction and try again.

The folder is empty, but ls -a shows . and ..: the folder itself and the one above. They’re in every folder, and that’s why cd .. works anywhere.

The d at the start says both are real folders (directory). The @ is a macOS mark: the folder carries extra information that the system stores separately. You may not get it, and on Linux you never do.

Your dates will be different. On Linux and WSL, the path starts with /home/ana, you get total 8, you get ana ana instead of ana staff (the second name is the group; you’ll see it in lesson 4), folders show a size of 4096 and a letter or two of the permissions may change.

3. Go up and come back

Ventana de terminal
cd ..
pwd
ls
cd phase-1

What you will see:

/Users/ana/backend-course
phase-1

cd .. takes you up to backend-course, ls shows what’s there (only phase-1) and cd phase-1 takes you back down with a relative path. Neither cd prints anything.

On Linux and WSL, the path starts with /home/ana.

4. Jump to the root and back

Ventana de terminal
cd /
ls
cd -

What you will see:

Applications dev Library sbin Users Volumes
bin etc opt System usr
cores home private tmp var
~/backend-course/phase-1

cd / takes you to the root, and ls shows what hangs from it: Users, with the home folders, and system folders you don’t need to touch. Your Mac may have one more or one fewer, and the columns depend on the width of the window.

cd - takes you back to where you were and prints the folder. In zsh, your home folder is shortened to ~.

On Linux and WSL, ls / shows other folders, like boot, media, root or srv, and no Users or Applications. And bash prints the full path: /home/ana/backend-course/phase-1.

5. Program or part of the shell?

Ventana de terminal
which ls
type cd

What you will see:

/bin/ls
cd is a shell builtin

ls is a file on the disk, and cd, part of the shell. On Linux and WSL, which ls gives /usr/bin/ls.

Finally, three things you do with the keyboard that have no output to copy:

  1. Type cd ~/bac, without pressing Enter, and press Tab. The shell completes it: cd ~/backend-course/. Now press Enter.
  2. Press ↑ several times: the instructions from these exercises appear, from most recent to oldest. To clear the line without running anything, press Ctrl + C.
  3. Type man ls and press Enter. Inside, type /-a and press Enter: you jump to the explanation of the -a option. Quit with Q.

And now, the challenge. Step 1 left you in backend-course: first go back to your practice folder with cd phase-1. This time, type the instructions yourself, without copying them:

Challenge: from your practice folder, get to /tmp with a relative path, check it with pwd and come back with a single instruction.Show answer
Ventana de terminal
cd ../../../../tmp
pwd
cd -

From ~/backend-course/phase-1 there are four levels up to the root: phase-1, backend-course, ana and Users (on Linux and WSL, home). That’s why you need four .., and from the root you go down into tmp. pwd answers /tmp, and cd - takes you back to ~/backend-course/phase-1.

If you get no such file or directory, count the levels with pwd before leaving: each folder in the path is one ... If you press Tab twice after each ../, you’ll see what’s at that level.

You’ve been using paths and instructions for a long time, even if you didn’t call them that:

  • Web addresses have paths too. This page’s ends in /en/phase-1/basic-terminal-commands/: folders separated by /, the same idea as on your disk.
  • The Finder accepts paths: in the Go menu, choose “Go to Folder” (Cmd + Shift + G) and type ~/backend-course.
  • In Phase 0, curl -v http://localhost:8080 was already an instruction with its three parts: the command (curl), an option (-v) and an argument (the address).

And if you code:

  • import Button from '../components/Button' uses a relative path: .. goes up to the folder above, just like in the terminal.
  • VS Code’s integrated terminal is a terminal with a shell inside, the same zsh or bash as always.
  • When you type npm run dev, it’s the shell that finds the npm program and starts it with two arguments, run and dev.
  • A name with spaces, without quotes. cd My documents gives cd two arguments, My and documents. zsh answers with a strange message, cd: string not in pwd: My, and bash with a clearer one, bash: cd: too many arguments. Use quotes (cd "My documents"), a backslash before the space (cd My\ documents) or Tab, which adds it for you.
  • Capital letters. By default, the Mac doesn’t tell uppercase from lowercase in names: if you have a Documents folder, cd documents also takes you there. Linux does tell them apart, and there it fails. What works on your Mac can fail on the server: type names exactly as they are, or let Tab complete them.
  • Mixing up the root and your folder. cd /phase-1 gives cd: no such file or directory: /phase-1 (in bash, bash: cd: /phase-1: No such file or directory). The / at the start makes it look for phase-1 in the root. Remove it, or use ~/backend-course/phase-1.
  • Copying the prompt’s $ from other websites, as in $ pwd. The shell looks for a program called $ and answers zsh: command not found: $ (in bash, $: command not found). Copy only what comes after it.
  • Getting stuck inside man. You haven’t broken anything: quit with Q.
  • ls --help on the Mac. It answers ls: unrecognized option `--help' and a line with its usage. Use man ls; on Linux and WSL, --help does work.
  • The terminal is the window; the shell (zsh on Mac, bash on Linux and WSL) is the program that reads your instructions and starts other programs.
  • An instruction is cut at the spaces into a command, options and arguments. A name with spaces goes in quotes.
  • pwd tells you where you are, ls what’s there and cd moves you. cd is part of the shell because it changes the shell’s own folder.
  • An absolute path starts at the root (/) and works from anywhere; a relative one starts from where you are. ~ is your folder, . the current one and .. the one above.
  • Tab completes, ↑ repeats and man explains commands to you.
On the server you’re in /home/ubuntu/projects/shop/web. Where does cd ../.. take you?

You’ve written down the instruction cd phase-1. It worked yesterday, but today, right after opening the terminal, it answers no such file or directory. What happened?

Imagine someone writes a separate cd program, a file on the disk like /bin/ls. What would happen when you used it?

In the instruction curl -sI https://example.com, what is -sI?

You want to go into a folder called Trip photos. You type cd Trip photos and get an error. Why?

On your Mac you type ls --help and it answers unrecognized option. What’s going on?

phase-1 · lesson 1/10