Skip to content
Snippets Groups Projects
3_first_steps_in_a_terminal.md 6.73 KiB
Newer Older
Laurent Modolo's avatar
Laurent Modolo committed
---
title: First step in a terminal
---

# First step in a terminal

[![cc_by_sa](./img/cc_by_sa.png)](http://creativecommons.org/licenses/by-sa/4.0/)

Objective: learn to use basic terminal command

Congratulation you are now connected on your VM !

The first thing that you can see is a welcome message (yes GNU/Linux users are polite and friendly), and information on your distribution.

> A **Linux distribution** (often abbreviated as **distro**) is an [operating system](https://en.wikipedia.org/wiki/Operating_system) made from a software collection that is based upon the [Linux kernel](https://en.wikipedia.org/wiki/Linux_kernel)

What is the distribution installed on your VM ?

You can go to this distribution website and have a look at the liste of firm using it.

## Shell

A command-line interpreter (or shell), is a software designed to read lines of text entered by a user to interact with an OS.

To simplify the shell execute the following infinite loop:
1. read a line
2. translate this line as a program execution with it's parameters
3. launch the corresponding program with the parameters
3. wait for the program to finish
4. Go back to 1.

When you open a terminal on an Unix like OS, you will have a **prompt** displayed: it can end with a **$** or a **%** caracter depending on your configuration. As long as you see your prompt, it means that you are in step **1.**, if no prompt is visible, you are in step **4.** or you have set up a very minimalist configuration for your shell.

<img src="./img/prompt.png" alt="prompt" style="zoom:150%;" />

The blinking square or vertical bar represent your **cursor**. Shell predate graphical interfaces, so most of the time you won't be able to move this cursor with your mouse, but with the directional arrows (left and right).

On the IFB, your prompt is a **$**

```sh
etudiant@VM:~$
```

You can identify the following information form your prompt: **etudiant** is your login and **VM** is the name of your VM (**~** is where you are on the computer, but we will come back to that later).

On Ubuntu 20.04, the default shell is [Bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell)) while on recent version of macOS it's [zsh](https://en.wikipedia.org/wiki/Z_shell). There are [many different shell](https://en.wikipedia.org/wiki/List_of_command-line_interpreters), for example, Ubuntu 20.04 also have [sh](https://en.wikipedia.org/wiki/Bourne_shell) installed.

## Launching programs

You can launch every program present on your computer from the shell. The syntax will always be the following:

```sh
etudiant@VM:~$ program_name option_a option_b option_c [...] option_n
```

And pressing **enter** to execute your command.

For example we can launch the `cal` software by typing the following command and pressing **enter**:

```sh
cal
```

When you launch a command, various things can happen:

- Information can be diplayed in the shell
- Computation can be made
- Files can we read or written
- etc.

We can pass argument to the `cal` software the following way

```sh
cal -N
```

What is the effect of the `-N` parameter ?

You can add as many parameters as you want to your command, try `-N -C` what is the meaning of the `-C` parameter ?

The `-H` option display the month of a given date in a `yyyy-mm-dd` format. Try to display your month of birth in vertical format.

Traditionally, parameters are *named* which mean that they are in the form of:

* `-X` for an on/off option (like `cal -N`)
* `-X something` for an input option (like `cal -H yyyy-mm-dd`)

Here the name of the paremeter is `X`, but software can also accept list of unamed parameter. Try the following:

```sh
cal 2
cal 1999
cal 2 1999
```

What is the difference for the parameter value `2` in the first and third command ?

## Moving around

For the `cal` program, the position in the file system is not important (it's not going to change the calendar). However, for most tools that are able to read or write files, it's important to know where you are. This is the first real difficulty with command line interface: you need to remember where you are.

If you are lost you can **p**rint your **w**orking **d**irectory (i.e. where you are now, working) with the command

```sh
pwd
```

Like `cal`, the `pwd` command return textual information

By default when you log on an Unix system you are in your **HOME** directory. Every user (except one) should have it's home directory in the `/home/`folder.

To **c**hange **d**irectory you can type the command `cd`, `cd` take one argument: the path of the directory where you want to go. go to the `/home` directory.

```sh
cd /home
```



The `cd` command don't return any textual information, but change the environement of the shell (you can confirm it with ` pwd`) ! You can also see this change in your prompt:

```sh
`etudiant@VM:/home$`
```

What happend when you type `cd` without any argument ?

What is the location shown in your prompt ? Is it coerent with the `pwd` information ? Can you `cd` to the `pwd` path ?

When we move around a file system, we often want to see what is in a given folde. We want to **l**i**s**t the directory content. Go back to the `/home` directory and use to the `ls` command see how many have a home directory there.

We will see various option of the `ls` command, throughout this course. Try the `-a` option.

```sh
ls -a
```

What changed commpared to the `ls` command without this option ?

Go to your home folder with the bare `cd` command and run the `ls -a` command again. The `-a` option make the `ls` command list hidden files and folders. On Unix systems, hidden files and folders are every files and folders whose name start with a "**.**".

Can you `cd` to "**.*" ? 

```sh
cd .
```

What happend ?

Can you cd to "**..**" ?

```sh
cd ..
```

What appended ?

Repeat 3 times the previous command (you can use the upper directionnal arrow to repeat the last command).

What append ?

You can use the `-l` option in combinaison with the `-a` option to know more about those folder.

## Unix file system structure

> On a UNIX system, everything is a file ; if something is not a file, it is a process.
>
> Machtelt Garrels

The followings are files:

- a text file
- an executable file
- a folder
- a keyboard
- a disk
- an usb key
- ...

This mean that your keyboard is represented as a file within the OS.

This file system is organized as a tree. As you have seen every folder has a parent folder exept the `/` folder whose parent is itself.

Every file can be accessed by an **absolute path** starting at the root. Your user home folder can be accessed with the path `/home/etudiant/`. Go to your user home folder.

We can also access file with a **relative path**, using the special folder "**..**". From your home folder, go to the *ubuntu*  user home folder without passing by the root (we will see use of the "**.**" folder later).