---
# System prepended metadata

title: Intro to Linux and the Command Line

---

# Intro to Linux and the Command Line

What we refer to as the 'Linux' operating system, is actually a whole family of operating systems running on the Linux kernel. Ubuntu is one of the most popular members (or distributions) of the Linux family, and the one we shall be going over.

Linux is based on an old, influential operating system called UNIX, which is actually what MacOS is also based on, meaning they actually have a lot in common. Linux and MacOS together with other UNIX-based systems are sometimes collectively referred to as \*nixes. 

Interestingly, in a way you've probably already used Linux extensively, without even knowing it, because Android is also built on top of the Linux kernel! However, when we talk about Linux, usually we're not talking about just the kernel, but some operating system in the Linux family.

## The Linux Filesystem

The linux filesystem is layed out in a different way, directories (folders) all start at `/`, the directory which is called the root of the filesystem. The root folder contains basically your entire operating system. Of the directories that are a part of this, an important one is `/home`, where all the user's personal data is stored (similar to `C:\Users`) on a Windows system. A directory exists with your user's name, which is called the home directory. If the current user is `bob`, then their home directory would be `/home/bob`, and will often be represented by `~` for short.

## Packages and Installing programs

Before installing any programs here is a primer on what packages are:
A **package** delivers and maintains new software for Linux-based computers. Just as Windows-based computers rely on executable installers, the Linux ecosystem depends on packages. Think of a package as a zip file containing all the code and setup information you need to run a program. There are special programs called **package-manager** that uses the information in the package to install the program on Linux. In the case of ubuntu we'll be working with `.deb` packages and `apt` as our package manager. You can look [here](https://www.lifewire.com/guide-to-linux-packages-2202801) to understand more about packages and package managers.
Now its the time to continue the tradition of using Firefox to install chrome. Similar to how you would install Chrome on Windows. Download the `.deb` package from [here](https://www.google.com/chrome/?platform=linux). Once you have downloaded the `.deb` file there are two ways to install it, using the GUI or the command line. We'll go through both here:

1. **Using the GUI:** Just double click on the downloaded `.deb` file and it would be opened in the store. You can then click install and chrome will be installed.
2. **Using the command line:** Navigate to the directory where you saved the '.deb' file and use the following command to install it


```bash
sudo apt install ./google-chrome-stable_current_amd64.deb
```

Now you can try installing applications like discord and teams the same way.

## The Shell and Commands

Whenever you open up the terminal you are opening what is called a **shell**. As the name suggests a shell is just  a 'shell' around the operating system. The terminal in Linux is a wonderful, powerful tool. When transitioning from the world of Windows it is hard to appreciate this because the Windows (`cmd.exe`) shell is rather lacking. 

Most \*nix default shells (bash(ubuntu), zsh(macos), etc) provide you with a lot more power and abilities, you will find that almost anything in Linux is configurable using the shell. Another advantage is that it is very flexible as compared to a GUI interface.

The shell allows us to use text commands to do stuff, this includes moving around in the file tree, creating and deleting files, launching programs and compiling your code. When you open up the terminal you'll see a prompt similar to this:

```
username@hostname:~ $
```

This is where you'll be typing commands to do stuff.

### Structure of a command

The basic structure of any command is as follows:

```
<command-name> [arguments...]
```

So each command has a specific command-name, followed by one or more arguments that the command takes. You can try this by typing gibberish on the command line and pressing enter, you'll receive an error message in the likes of:

```
command not found
```


We'll be going through some of the commands starting from the basic ones:

### `whoami`

Type `whoami` on the command line and press enter. It prints the name of the user currently logged in.

### `man`

The `man` command is short for manual and it is really a manual for every command. Just type `man whoami` to know and it gives you the who information about the `whoami` command. You can press `q` on your keyboard to exit the man pages. Whenever you want to find what a command does or what arguments it can take, use `man <command-name>`. 
Now to really drive the importance of `man` try using `man` to find out more about `man` using the `man man` command.

As an exercise you may look at the man pages entry of each of the commands discussed.

As an alternative to `man` you can use `tldr`. It's a command you can install, which you then run like this: `tldr <command>`. It gives you a very quick overview of a command, with some handy examples of common usage scenarios.

### `clear`

The usage of the `clear` command is very simple; it just clears the terminal.

### `pwd`

The `pwd` command stands for *print working directory*. It prints out the name of the current directory in which you are in. You would usually have the current directory shown in your prompt, so you would not use this often.

### `cd`

You can use the `cd` command to change directories. Use the `cd` command to move into the downloads folder and use `pwd` to print the working directory.

```shell
cd Downloads
```

now `cd` back to the home directory using 

```shell
cd ..
```

You can use both relative and absolute paths as arguments to the `cd` command.

### `ls`

When Inside a directory, you can list all the contents of the folder using `ls`.

```bash
ls
```

If you add a folder name or path, it will print that folder's contents:

```bash
ls Downloads
```

`ls` also accepts many options, one of them is `ls -al`

```bash
ls -al /bin
```

You have, from left to right:

- the file permissions 
- the number of links to that file
- the owner of the file
- the group of the file
- the file size in bytes
- the file's last modified date-time
- the file name

This set of data is generated by the `l` option. The `a` option instead also shows the hidden files. Look at the `man` pages to check out more options and flags.

### `touch`

The `touch` command is to create a new file.

```bash
touch a.txt
```

would create an empty file named `a.txt` the current directory.

### `mkdir`

The `mkdir` command is used to create an empty new folder.

```bash
mkdir New
mkdir new
```

would create a new directory named `New` and `new`. Note that in Linux file names are case sensitive `New` and `new` are completely different directories.

### `rmdir`

Once you have created an empty directory. You can delete it using the `rmdir` command. Note that `rmdir` cannot remove a directory that has files in it.

```bash
rmdir New
```

would delete the `New` directory you just created. 

### `rm`

The `rm` command is used to remove files. Use the following command:

```shell
touch a.txt
rm a.txt
```

to delete the file `a.txt` we created earlier.
The `rm` command accepts many options, one of them is `-rf`. Look at the `man` pages to know more about the other flags it accepts.

```bash
mkdir new
rm -r new
```

`-r` stands for *recursive* tells `rm` to recursively remove directories and their contents.

`-f` stands for *force*  `rm` to forcefully remove files.

Thus a command like 

```shell
rm -rf /
```

is **very** dangerous as it is basically telling to remove the whole root directory. 

### `mv`

Once you have a file, you can move it around using the `mv` command. You specify the file current path, and its new path:

```bash
touch abc.txt
mv abc.txt xyz.txt
```

The `abc.txt` file is now moved to `xyz.txt`. This is how you **rename** files and folders.

If the last parameter is a folder, the file located at the first parameter path is going to be moved into that folder. In this case, you can specify a list of files and they will all be moved in the folder path identified by the last parameter:

```bash
touch xyz.txt
touch abc.txt
mkdir alphabet
mv xyz.txt abc.txt alphabet
```

### `cp`

You can copy a file using the `cp` command, the first argument is the file you want to copy followed by the destination file.

```bash
touch abc.txt
cp abc.txt xyz.txt
```

To copy folders you need to add the `-r` option to recursively copy the whole folder contents, suppose you want to make a copy of a folder named `abc` with some data:

```bash
cp -r abc xyz
```

### `echo`

You can use the `echo` command to print something to the terminal.

```shell
echo Hello World!
```

prints "Hello World!".

### `history`

The `history` command is used to show the history of all the commands you have used in the past.

```shell
history
```

would print the last few commands you have used. This is particularly helpful to look up commands you have used in the past.

**Tip:** You can also use the up arrow key to see the commands you have used one by one. You may also try using Ctrl-r to search for commands in your history.

### `sudo`

`sudo` is commonly used to run a command as root. Think of this as similar to running a command Administrator in the context of Windows.

You must be enabled to use `sudo`, and once you are, you can run commands as root by entering your user's password (*not* the root user password).

There are some commands that cannot be run by unprivileged users. Look at the command we used to install chrome. Usually to install and update software you will have to use commands that use `sudo`.

```bash
sudo apt update
sudo apt upgrade
```


## Resources

- An amazing primer for absolute beginners https://ubuntu.com/tutorials/command-line-for-beginners. **Highly recommended**.
- Those of you who want to know more about linux commands can look at [this](https://www.freecodecamp.org/news/the-linux-commands-handbook/) or if you prefer watching a video then [this](https://www.youtube.com/watch?v=ZtqBQ68cfJc).
- A more informative (but still short) explanation of how the linux filesystem is laid out https://www.youtube.com/watch?v=42iQKuQodW4