Finding your way around the terminal

First lesson of the Terminal and Command Line course, free to read: about 15 minutes, 3 exercises and 3 quiz questions. This is the catalogue's base lesson; in the student space, it is rewritten for your profile.

Objectives

  • Understand the role of the terminal and the shell
  • Read the command prompt and the structure of a command
  • Navigate the directory tree with pwd, ls and cd

Terminal and shellNavigationAbsolute and relative paths

The terminal is a window where you talk to the computer by typing text. The program that reads and runs your commands is called the shell: the most common one on Linux servers is bash, and macOS uses zsh by default (the commands in this course work in both). On Windows, people generally use WSL (an integrated Linux) or Git Bash to get the same commands.

Why use it when graphical interfaces exist? Because servers often have no screen, because many tools (Git, Python, npm, Docker) are driven from the command line, and because a command can be automated in a script.

The prompt and the anatomy of a command

The prompt looks like lea@portable:~/projets$. It shows the user, the machine, the current directory, then $: the shell is waiting for your command.

ls -l --all Documents
# ^   ^   ^      ^
# |   |   |      argument: what to act on
# |   |   long option
# |   short option
# command name

Options change the behavior (often -x in short form, --word in long form). Short options can be combined: ls -la is equivalent to ls -l -a.

The directory tree

Files are organized as a tree. On Linux and macOS, everything starts from the root /. Your home directory is for example /home/lea (Linux) or /Users/lea (macOS), abbreviated ~.

pwd                 # print working directory: where am I?
# /home/lea

ls                  # lists the contents of the current directory
ls -l               # long format: permissions, size, date
ls -a               # includes hidden files (starting with a dot)

cd projets          # enters the projets subdirectory
cd ..               # goes up to the parent directory
cd ~                # returns to the home directory (cd alone does the same)
cd -                # goes back to the previous directory

Absolute and relative paths

  • An absolute path starts with / and is valid everywhere: /home/lea/projets/site.
  • A relative path starts from the current directory: from /home/lea, projets/site refers to the same directory.
  • . refers to the current directory and .. to the parent: ../photos means “the photos directory next to me”.

If a name contains spaces, wrap it in quotes: cd "Mes documents".

Three habits that save time

  1. Tab automatically completes file and command names. Pressing it twice shows the possibilities.
  2. The up and down arrows recall previous commands; history lists them.
  3. man ls opens the manual for a command (press q to quit); ls --help gives a summary.

And if a command seems stuck, Ctrl+C interrupts it.

Exercises

Solutions and tests stay on the server. In the student space, your code is run and checked, and the tutor gives hints before the solution.

  1. TaskDifficulty 11 hint

    First tour

    Open the terminal. Display the current directory, list its contents including hidden files, move into /tmp, then return to your home directory.

    • The pwd command displays a path starting with /
    • ls -a shows at least the . and .. entries
    • After cd /tmp, pwd displays /tmp
    • After cd ~, pwd displays your home directory
  2. Multiple choiceDifficulty 21 hint

    Relative path

    You are in /home/lea/projets/site. Which path refers to /home/lea/photos?

    • ../photos
    • ../../photos
    • ~/projets/photos
    • /photos
  3. Multiple choiceDifficulty 11 hint

    Anatomy of a command

    In ls -lh /var/log, what does /var/log represent?

    • An option
    • An argument
    • The command name

End-of-lesson quiz

  1. What does cd .. do?

    • Returns to the home directory
    • Goes up to the parent directory
    • Returns to the previous directory
  2. Which command displays the current directory?

    • ls
    • pwd
    • cd
  3. What does the Tab key do?

    • Cancel a command
    • Automatically complete a name
    • Display the history

The rest of the course

  1. Create, copy, move and delete15 min
  2. Reading files and finding information15 min
  3. Redirections and pipes: combining commands15 min
  4. Scripts, permissions and environment variables20 min

Terminal and Command Line · Beginner · 2 h 20 min