⌨️ Get to know your terminal pt.1
Tags: codenewbie, tutorial, beginners, terminal
Published: 2022-02-07 22:51:35

👋🏽 Hello everyone! As always, I hope you are having an excellent day/night and I expect you are ready to learn something new today. 😉 In the following days I will be posting a series of 2 entries in which I’ll try my best to explain everything you need to know about our all-day partner: the terminal 💻. 

In this particular entry I will show you:
1. The basics (definition, basic commands) 🐣
2. How does the file management system works? 👀
3. The shell and its magic-like functionality 🦀
4. Intro to file types and permissions 🗂

Without further ado, let’s begin!

## 〉The basics 🐣

The terminal is more than the program we use to look as cool hackers in front of others. Formally, we can define it as:

> 💡 *A UI that allows us to interact directly with our operating system via input commands.*

Before we continue, I must emphasize the fact that this UI emulates a ***shell*** interface or, said with more common words, a command-line interface. But, what is a *command*? Since its usability varies depending on the context, it can and has different definitions:

> 💡 A command is: 
> *- An executable compiled program*
> *- A native shell utility*
> *- A downloaded utility (eg. `tree`)*
> *- A custom alias.*

 Thus, we can conclude that a command is, regardless of its origin, a particular action that will have an effect in our OS thanks to its introduction in the terminal 🤯. Now that we have defined some basic concepts, let’s see what we can do in practice.

### 🚀 Commands that should and must be learnt

 - To **make a directory**: `mkdir new_directory_name`  

```
$ mkdir test
$ ls
test
```

 - To **print the working directory**: `pwd`

```
$ pwd
/Users/francisco
```

 - To **list every directory in current working space**: `ls` 

```
$ ls
test
```

 - To **change to another directory**: `cd directory_name` 

```
$ cd test
~/test $ 
```

 - To **remove a directory**: `rm -r directory_name`

```
$ rm -r test
$ ls

```

 - To **create a new file**: `touch new_file`

```
$ touch test_file
$ ls
test_file
```

 - To **copy a file**: `cp file new_destination`

```
$ cp test_file copy_test_file
$ ls
copy_test_file  test_file
```

 - To **move a file to a new location or rename a file**: `mv file new_destination`

```
# RENAMING 
$ mv test_file copy
$ ls
copy_test_file  copy
----------------------
# MOVING
$ mv copy ..
$ ls
copy_test_file 
$ cd ..
$ ls
copy 
```

 - To **delete a file**: `rm file`

```
$ rm copy_test_file 
$ ls
 
```

 - To **see in an interactive manner a file**: `less file`

```
$ less DEV.txt
```

![less command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4rh3msp54krbaql4rkar.png)

 - To **concatenate files or see the content of a single file**: `cat file file2`

```
$ cat copy
This is the content of a test file
```

 - To **see the first 10 elements within a file**: `head file`

```
$ head copy
This is the content of a test file
1
2
3
4
5
6
7
8
9
```

 - To **see the last 10 elements within a file**: `tail file`

```
$ tail copy
10
11
12
13
14
15
16
17
18
19
```

 - To **see the type of a command**: `type command`

```
$ type ls
ls is an alias for ls -G
```

 - To **consult how a command works via the manual**: `man command`

```
$ man ls
```

![Iman command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eb2jns2wssl7tlu5dq5a.png)

 - To **print something in the terminal**: `echo text`

```
$ echo ls
ls 
```

In addition to what we have seen, every shell or terminal emulator has something called **wildcards**. A wildcard can be perceive as:

 > 💡 *A special or reserved character that enables a more sophisticated search within our file management system by looking up for a particular pattern.*

For example:

- The symbol `*` returns all the files and/or directories that coincide with the preceded or proceded pattern:

```
# PRECEDED
$ ls D*
Desktop Documents Downloads
---------------------------
# PROCEDED
$ ls *.txt
text.txt
```

- The symbol `?` returns all the files and/or directories that fulfill the exact number of characters represented by the mentioned symbol:

```
$ ls image?
image1
--------------------------
$ ls image???
image123
```

- The symbol `[[:upper:]]*` returns all the files and/or directories that begin with an upper letter:

```
$ ls -d [[:upper:]]*
Documents Downloads Music Pictures Desktop
```

- As before but with the expected twist. The symbol `[[:lower:]]*` returns all the files and/or directories that begin with an lower-case letter:

```
$ ls -d [[:lower:]]*
test
```

As you can see, the topic is extensive on its own. Thus, if you want to know more this [link](https://tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm) may be useful 😉.

## 〉The file management system 👀

Alright, now that you know a bunch of commands to type in your terminal, its time to go deeply in the rabbit hole of the command-line and explain why files and directories are more important than we once thought.  

In a UNIX-like operating system (eg. Linux or MacOS) not only core functionalities of it live within a file management system, but the whole OS lives within a directory. This system -or *The Linux File Hierarchy Structure* to formally named it- defines the hierarchy, content and the whole structure of our OS. This is the reason why our interactions within the terminal are limited to edit, view and navigate across our system’s files and folders. 

As everything in life, the hierarchy of our OS’s directory structure begins with a root 🌱: 

- The root directory `/` is the folder in which every existent directory and file is and will be saved.

To follow with the structure of the File System is kinda’ complicated since every operating system may have exclusive directories. However, here you have a list of some of which I consider *essential* to be aware of. In addition, you can visit [geeks for geeks](https://www.geeksforgeeks.org/linux-file-hierarchy-structure/) for more information regarding this topic:

- The `/etc` directory stores system’s configuration information as well as startup and shutdown scripts for individual programs. 
- The `/dev` directory stores files of attached media such as a USB. 
- The `/usr` directory stores applications and utilities that are shared among users.
- The `/bin` directory stores the needed executable binaries for basic commands (eg. `ls`, `ping` or `cp`) to properly work.
- The `/lib` directory stores shared libraries.
- The `/tmp` directory stores temporary files.
- The `/home` directory stores the information (like proprietary directories) of a unique user. In the terminal is represented with the `~` symbol. 

## 〉The shell functionality 🦀 

Our shell unlocks great power if you know how to use it and how it works. Any command-line is able to interpret a command, display an output or an error message if we have done something wrong. This process is handled by a **file descriptor**, which is nothing but a number that serves as a unique identifier for  three main standard events:

- **input** 👉🏽 A.K.A *stdin*, whose ID’s 0 and handles any entry from our keyboard or files. 
- **output** 👉🏽 A.K.A *stdout*, whose ID’s 1 and prints the correct output of a command. 
- **error** 👉🏽 A.K.A *stderr*, whose ID’s 2 and prints an error message whenever the command we introduced do not works. 

This knowledge is useful not only to understand why our shell works the way it does, but because it allows us to use three powerful concepts:

### 🔄 Redirections

Useful for:

> 💡 *Taking the shell’s stdin (`<`), stdout or stderr (`>`) and save it into a file.*

Examples:

#### Saving the stdout in a file
```
$ ls Pictures > images.txt
$ cat images.txt
image1.png
image0.png
image2.png
```

#### Saving the stderr in a file
```
$ ls Picture > images.txt
$ cat images.txt
ls: Picture: No such file or directory
```

#### Saving the stdin in a file
```
$ sort < images.txt
$ cat images.txt
image0.png
image1.png
image2.png
```

**NOTE:** Be aware that any time you use the individual symbols of `>` and `<` you will rewrite whatever is in your result file. To concatenate information use: `>>` or `<<` 🤓.

### 🚬 Pipes 

Useful for:

> 💡 *Concatenating commands `|`. Use the stdout as the stdin of another command.*

Example:

#### Creating a file
```
$ ls Pictures | tee result.txt # tee creates a file within pipes
$ cat result.txt
image1.png
image0.png
image2.png
```

### 🕹 Control operators

Useful for:

> 💡 *Concatenating and/or executing various commands in an asynchronous, synchronous or conditional manner.*

Examples:

#### Asynchronous (multithread execution using the `&` operator)
```
$ ls & mkdir test
[1] 1669.  # --> thread 1 
[2] 1670.  # --> thread 2
[2]  + 1670 done         mkdir test  # --> thread 2
```

#### Synchronous (Ordered execution using the `;` operator)
```
$ ls ; mkdir test
Blog   # --> command 1
       # --> command 2
$ ls
Blog   test
```

#### Conditional (Logic-based execution using the `&&` and `||` operators)
```
# executed if both of the commands do not have a stderr (fd: 2)
$ mkdir test1 && ls
Blog   test   test1
-------------------
# executed if one of the commands do not have a stderr (fd: 2)
$ cd test2 || ls
Blog   test   test1
```

These examples were simple. Thus, I will leave you with this [link](https://opensource.com/article/18/11/control-operators-bash-shell) if you are interested in knowing more about it.  

## 〉File types and permissions 🗂

To conclude with this blog entry, let’s introduce ourselves to permissions and file types and owners. A file in UNIX-like operating systems can be classified as:

- A **normal** file (represented in the shell with `-`)
- A **directory** (represented in the shell with `d`)
- A **symbolic link** (represented in the shell with `l`)
- A **special block** (represented in the shell with `b`)

For now I will not explain in detail what does these types mean. Yet, be aware that these characteristics allow us to interact in specific ways with our files. However, despite its type, every file has the same permissions options:

- **Reading** (represented in the shell with `r`)
- **Writing** (represented in the shell with `w`)
- **Executing** (represented in the shell with `x`)

These modes affect or can be configured to three different actors:

- The **owner** of the file 🤓
- A **group** of users 👯‍♀️
- The rest of the **world** (someone who is not part in one the previous actors) 🌎

This is important since each permission can be activated or deactivated (represented in the shell with `-`) to a specific actor using the octal numeric system:

![octal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9oo4dc6yh0ki0btbr2zn.png)

To change the permissions use the command `chmod octal_number file`:

```
$ ls -l
drwxr-xr-x   5 francisco  staff   160 Feb  1 19:28 Developer

$ chmod 777 Developer
$ ls -l
drwxrwxrwx   5 francisco  staff   160 Feb  1 19:28 Developer
```

## Conclusion

You did it to the end. Thank you so much ❤️! Remember that this guide is not complete yet. Nonetheless, I hope you found it useful 🤓. I would like to give special thanks to the [Platzi](https://platzi.com) community. Specially to professor Enrique Devars, who taught me everything you saw and will see in these terminal-related posts 💚😉. 

Programen bonito 💻!