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 ~.
Navigating
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/siterefers to the same directory. .refers to the current directory and..to the parent:../photosmeans “the photos directory next to me”.
If a name contains spaces, wrap it in quotes: cd "Mes documents".
Three habits that save time
- Tab automatically completes file and command names. Pressing it twice shows the possibilities.
- The up and down arrows recall previous commands;
historylists them. man lsopens the manual for a command (pressqto quit);ls --helpgives 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.
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
Relative path
You are in
/home/lea/projets/site. Which path refers to/home/lea/photos?- ../photos
- ../../photos
- ~/projets/photos
- /photos
Anatomy of a command
In
ls -lh /var/log, what does/var/logrepresent?- An option
- An argument
- The command name
End-of-lesson quiz
What does
cd ..do?- Returns to the home directory
- Goes up to the parent directory
- Returns to the previous directory
Which command displays the current directory?
- ls
- pwd
- cd
What does the Tab key do?
- Cancel a command
- Automatically complete a name
- Display the history
The rest of the course
- Create, copy, move and delete15 min
- Reading files and finding information15 min
- Redirections and pipes: combining commands15 min
- Scripts, permissions and environment variables20 min