1
0
Fork 0
mirror of https://codeberg.org/Mo8it/How_To_Linux.git synced 2024-10-18 15:12:39 +00:00

Added more basics

This commit is contained in:
Mo 2022-08-21 01:41:38 +02:00
parent b4c7e93577
commit 33edf04a72
2 changed files with 265 additions and 20 deletions

View file

@ -1 +1,5 @@
# Day 1 # Day 1
In this chapter, you will learn the basics of Linux in the terminal.
But before we start with the basics, the first section discusses why you should learn Linux in the first place.

View file

@ -1,15 +1,19 @@
# Terminal basics # Terminal basics
The terminal, the console, the command line. All terms refer to the same thing. Get access to a Linux system and open a terminal. Wait, don't panic! The terminal, the console, the command line. All terms refer to the same thing. Get access to a Linux system and open a terminal.
Many find the terminal frightening. This is the cryptic thing you see hackers in films using! You might ask yourself, what am I doing here? I don't want to break my system or launch a terminator! Warning ⚠️: **Don't panic!**
Again, don't panic! What you see is just a prompt (usually on a dark background). Many find the terminal frightening. This is the cryptic thing you see hackers in films using! You might ask yourself, what am I doing here? I don't want to break my system or launch a terminator! 😯
Again, **don't panic!** What you see is just a prompt (usually on a dark background).
Now, how to interact with the terminal? You are probably not seeing a lot of buttons on your terminal. This is because you interact with it (mainly) through your keyboard. But what to type? Now, how to interact with the terminal? You are probably not seeing a lot of buttons on your terminal. This is because you interact with it (mainly) through your keyboard. But what to type?
It is like a chat with your computer. But your computer is not into memes. It is a soldier waiting for your commands! It is like a chat with your computer. But your computer is not into memes. It is a soldier waiting for your commands!
### Echo
Lets enter a command, probably your first command. Type `echo "Hello!"` and press enter. This is the output: Lets enter a command, probably your first command. Type `echo "Hello!"` and press enter. This is the output:
```console ```console
@ -17,27 +21,31 @@ $ echo "Hello!"
Hello! Hello!
``` ```
Congratulation! You did just say hi to your computer and it did even reply! What a charm! Congratulation! You did just say hi to your computer and it did even reply! What a charm! 🤠
Well, to be more precise, you gave a command to your computer to say "Hello!" and it did that. Well, to be more precise, you gave a command to your computer to say "Hello!" and it did that.
I know, I did just brake the romance between you and your computer. I appoligize. I know, I did just brake the romance between you and your computer. I appoligize. 😶
`echo` is a command that prints out what you give as an argument. It might not seem to be a useful command. Why would I want the computer to echo my words? The importance of `echo` will be clear mainly when writing scripts. More about this in later sections! `echo` is a command that prints out what you give as an argument. It might not seem to be a useful command. Why would I want the computer to echo my words? The importance of `echo` will be clear mainly when writing scripts. More about this in later sections!
### Navigation
Lets try more commands. type `ls` and press enter. What do you see? The output is going to be the files and directories in your current path. `ls` stands for _list_. Lets try more commands. type `ls` and press enter. What do you see? The output is going to be the files and directories in your current path. `ls` stands for _list_.
What if you want to take a look at the content of a different directory at different path? What if you want to take a look at the content of a different directory at different path?
To examine this, lets first create a new directory. Enter the command `mkdir empty_directory`. Well, you don't see anything? Did the command do something at all? To verify this, run `ls` again. Now you should be able to see your new directory `empty_directory` listed too! `mkdir` stands for _make directory_. `empty_directory` is just a name for our new directory. You could have used `mkdir Images` for example to create a directory called `Images`. To examine this, lets first create a new directory. Enter the command `mkdir empty_house`. Well, you don't see anything? Did the command do something at all? To verify this, run `ls` again. Now you should be able to see your new directory `empty_house` listed too! `mkdir` stands for _make directory_. `empty_house` is just a name for our new directory. You could have used `mkdir Images` for example to create a directory called `Images`.
Is `empty_directory` really empty? Lets verify that. Is `empty_house` really empty? Lets verify that.
Enter the command `cd empty_directory`. Again, you don't see an output. But maybe you did notice that a part of your prompt changed from `~` to `empty_directory`. This indicates that you are in your new directory. `cd` stands for _change directory_. Knowing the meaning of the commands you use which are often abbrevations does help you to memorize them. Enter the command `cd empty_house`. Again, you don't see an output. But maybe you did notice that a part of your prompt changed from `~` to `empty_house`. This indicates that you are in your new directory. `cd` stands for _change directory_. Knowing the meaning of the commands you use which are often abbrevations does help you to memorize them.
Now enter the command `ls`. You should not be seeing anything because the directory is indeed empty. Now enter the command `ls`. You should not be seeing anything because the directory is indeed empty.
An empty house is a sad house. Lets give the directory some friends to live within it. Enter the command `touch friend1.txt`. Now enter `ls` again: ### Create, rename, move
An empty house is a sad house. Lets give the house directory some friends to live within it. Enter the command `touch friend1.txt`. Now enter `ls` again:
```console ```console
$ ls $ ls
@ -46,42 +54,44 @@ friend1.txt
You should see the first friend of the empty directory. `touch` creates a file if it does not exist. It does also have another use case which is not relevant at this moment. You should see the first friend of the empty directory. `touch` creates a file if it does not exist. It does also have another use case which is not relevant at this moment.
But wait a minute, our directory `empty_directory` is not empty anymore! Lets fix that by renaming it. But wait a minute, our directory `empty_house` is not empty anymore! Lets fix that by renaming it.
Enter the command `cd ..` to go one directory back. The two points `..` refer to the parent directory in Linux. Enter the command `cd ..` to go one directory back. The two dots `..` refer to the parent directory in Linux.
Now that you are back in `~`, enter `mv empty_directory happy_directory`. Now enter `ls` again. You can see that `empty_directory` does not exist anymore. It was renamed to `happy_directory` (_since it has at least one friend now_). `mv` stand for _move_. Moving is not the same as renaming, right? Well, `move` does move file or directories to a new destination while making it possible to give the file or directory a new name on the destination. So we did _move_ the directory `empty_directory` to the same location, but we did give it a new name. Now that you are back in `~`, enter `mv empty_house happy_house`. Now enter `ls` again. You can see that `empty_house` does not exist anymore. It was renamed to `happy_house` (_since it has at least one friend now_). `mv` stand for _move_. Moving is not the same as renaming, right? Well, `move` does move file or directories to a new destination while making it possible to give the file or directory a new name on the destination. So we did _move_ the directory `empty_house` to the same location, but we did give it a new name.
I know, it is tricky. Lets take a look at an example that does actually _move_. Enter the command `touch friend2.txt friend3.txt`. This will create two new files at the same time. This way, you don't have to type `touch friend2.txt` **and** `touch friend3.txt`. I know, it is tricky. Lets take a look at an example that does actually _move_. Enter the command `touch friend2.txt friend3.txt`. This will create two new files at the same time. This way, you don't have to type `touch friend2.txt` **and** `touch friend3.txt`.
Now lets move one of our new text files. Enter `mv friend2.txt happy_directory`. Enter `ls` to see that `friend2.txt` did disappear. Lets verify that it now lives in `happy_directory`. You could use `mv happy_directory` and then `ls` analogously to the strategy above. But it is faster to use `ls` directly. Enter `ls happy_directory`: Now lets move one of our new text files. Enter `mv friend2.txt happy_house`. Enter `ls` to see that `friend2.txt` did disappear. Lets verify that it now lives in `happy_house`. You could use `mv happy_house` and then `ls` analogously to the strategy above. But it is faster to use `ls` directly. Enter `ls happy_house`:
```console ```console
$ ls happy_directory $ ls happy_house
friend1.txt friend2.txt friend1.txt friend2.txt
``` ```
We did verify that `friend2.txt` was moved. Lets move `friend3.txt`, too. Enter `mv friend3.txt happy_directory/loud_friend.txt`. Take a look at the content of your directory now: We did verify that `friend2.txt` was moved. Lets move `friend3.txt`, too. Enter `mv friend3.txt happy_house/loud_friend.txt`. Take a look at the content of your directory now:
```console ```console
$ ls happy_directory $ ls happy_house
friend1.txt friend2.txt loud_friend.txt friend1.txt friend2.txt loud_friend.txt
``` ```
We did not only move `friend3.txt`. We did also give it a new name in the destination. Hopefully, you have now a better understanding of renaming with `mv`. We did not only move `friend3.txt`. We did also give it a new name in the destination. Hopefully, you have now a better understanding of renaming with `mv`.
What if our directory is not really _happy_ anymore since a loud friend did move in. Lets remove that loud friend! ### Remove
Enter `mv happy_directory` and then `rm loud_friend.txt`. You will not see any output, but lets see what has changed in the directory: What if our house is not really _happy_ anymore since a loud friend did move in? Lets remove that loud friend!
Enter `mv happy_house` and then `rm loud_friend.txt`. You will not see any output, but lets see what has changed in the directory:
```console ```console
$ ls happy_directory $ ls happy_house
friend1.txt friend2.txt friend1.txt friend2.txt
``` ```
The loud friend is removed! `rm` stand for _remove_. The loud friend is removed! `rm` stand for _remove_.
> **Warning**: `rm` deletes a file directly! The file is not moved to a trash! It is gone! You can not restore it anymore! Think more than one time before using `rm`. > **Warning** ⚠️: `rm` deletes a file directly! The file is not moved to a trash! It is gone! You can not restore it anymore! Think more than one time before using `rm`.
Does `rm` also work with directories? Lets test it: Does `rm` also work with directories? Lets test it:
@ -140,4 +150,235 @@ The directory `zombies` is removed!
You probably wonder what `tree` is. As you can see in the output above, it shows you the tree of your current directory. It goes recursively through all directories starting with the current path and shows their content. You probably wonder what `tree` is. As you can see in the output above, it shows you the tree of your current directory. It goes recursively through all directories starting with the current path and shows their content.
### Simple file editing
Now, we want to write some rules to avoid having loud people or zombies again.
Lets first make sure that we are in our house directory first. To do so, enter `pwd`:
```console
$ pwd
/home/USERNAME/happy_house
```
What you see is the path to your current directory (also called working directory). _USERNAME_ here is the name of your user. `pwd` stands for _print working directory_.
We can write our rules in a simple text file. But how do we edit files in the terminal?
There are many ways to edit text files in the terminal. The simplest method is to use the command `nano`.
Enter the command `nano rules.txt`. You will be presented with a "terminal window" showing a blinking cursor and some shortcuts at the bottom.
Lets write some rules, for example:
```
No zombies!
No loud housemates!
```
How to save and exit? Lets take a look at the shortcuts at the bottom. The shortcuts starting with the symbol `^` expect the `Ctrl` key. Those starting with `M-` expect the `Alt` key. To save, we use `^X` which means that we have to press `Ctrl` and then press `X` while holding `Ctrl`.
Now, it asks us:
```
Save modified buffer?
Y Yes
N No ^C Cancel
```
A buffer is a file that is being edited in the memory. If you klick `N`, then your editions are lost! In our case, we want to save the rules. Therefore, we press `Y`.
Now it shows us:
```
File Name to Write: rules.txt
```
We want to keep the name that we did specify. Therefore, we press `Enter`. Otherwise, you can change the name and save the file under another name.
Lets take a look at our rules:
```console
$ ls
friend1.txt friend2.txt rules.txt
$ cat rules.txt
No zombies!
No loud housemates!
```
We did use `cat` to only print the content of the file `rules.txt` without opening it with an editor. `cat` stands for _concatinate_. This does not sound like printing file content!? This is because `cat` can be used to concatinate the content of different files. But this is not relevant for now.
### Manuals
After a while, the house might have many rules, more than the two that we did just enter. It would be useful to be able to count how many rules we do have. To do so, we can use the command `wc` which actually stands for _word count_, but is able to count more than words:
```console
$ wc rules.txt
2 5 32 rules.txt
```
We see three numbers. The first one is the number of lines, the second is the number of words and the last is the number of bytes. How do I know that?
I do not memorize everything! I had to look up the meaning of the three numbers. Where do you look such things up?
You could use the internet, but (almost) every Linux command comes with a manual. To access the manual of a command, we use the command `man` which stands for _manual_ followed by the name of the command that we want to look up.
Lets look up the command `wc`:
```console
$ man wc
NAME
wc - print newline, word, and byte counts for each file
SYNOPSIS
wc [OPTION]... [FILE]...
wc [OPTION]... --files0-from=F
DESCRIPTION
Print newline, word, and byte counts for each FILE, and a total line
if more than one FILE is specified.
A word is a non-zero-length sequence of printable characters
delimited by white space.
(...)
-m, --chars
print the character counts
-l, --lines
print the newline counts
(...)
-w, --words
print the word counts
(...)
```
`(...)` indicates that parts of the output were kept out to only show relevant output here.
The structure of the output is a standard for manuals with `man`.
The first section of the output is _NAME_ and shows the name of the command with a brief description of what it does.
The second section is _SYNOPSIS_ and shows the different way of using the command. The brackets indicate that a part is optional. The three dots `...` indicate that a part can be specified more than one time. This means that `[OPTION]...` tells us that the command accepts one to many optional options.
The options are presented in the section _DESCRIPTION_ that also explains the command with more details.
Lets pick one option: `-l, --lines`. The first part `-l` shows that short version of the option (not always given). The second part `--lines` shows the long version of the option which is also more verbose. The description below tells us that the option prints the newline counts. We can try it out, but first, you might already be in the panic mode because you might not find out know how to exit this manual.
HOW CAN I EXIT THIS MANUAL? DO I HAVE TO EXIT THE TERMINAL AND OPEN A NEW ONE? 😱
**Don't panic!** (_Wait until you open vi/vim/nvim_)
You can exit the manual by pressing the key `q`. You are free now!
Now, lets try the option `-l` that we got from the terminal:
```console
$ wc -l rules.txt
2 rules.txt
$ wc --lines rules.txt
2 rules.txt
```
You can see that `-l` and `--lines` are equivalent and they lead to returning the number of lines.
Lets try using more than one option:
```console
$ wc -l -w rules.txt
2 5 rules.txt
```
We see that we get the number of lines and then then number of words now.
We did learn how to access and read the manual, but do we navigate the manual?
If you open a manual with `man`, you can scroll up and down using the arrow keys. You can search by pressing `/`, then enter what you are wanting to search, `lines` for example, and then press `Enter`.
If you get more than one match for your search, you can jump to the next match with the key `n` and to the previous one with `N` (`Shift + n`).
You can learn more about how to navigate manuals (and other so called pagers) by pressing `h` in a manual. But since a lot is derived from the editor Vi/Vim, we will learn more about it when we learn this editor later.
Fun fact: You can read the manual of the manual with `man man` 😃
### Paths
Lets build one more house!
```console
$ cd ~
$ mkdir new_house
$ cd new_house
```
We did not use any new command, but what is this symbol `~`? We did already mention it and you might have noticed that it was showed in the prompt after opening the terminal. The so called tilde `~` stands for your home in Linux. Your home as a user has the path `/home/USERNAME` and it is the directory where you should place your own files.
By entering `cd ~`, we did make sure that we are in our "home" before creating a new directory. Enter only `cd` does have the same effect as `cd ~`.
So we did create a new house directory and did move into it. Lets say, we want to copy the rules from the first house to the new one while we are in the new one. To do so, we can use the command `cp` that stand for _copy_:
```console
$ pwd
/home/USERNAME/new_house
$ cp ~/happy_house/rules.txt .
$ ls
rules.txt
$ cat rules.txt
No zombies!
No loud housemates!
```
We did copy the rules, but did you notice the dot at the end of the command `cp`? What does it mean?
We did learn that `..` refers to the parent directory (one directory back). `.` refers to the current directory.
Here are some equivalent commands that might help you understand paths in Linux:
1. `cp /home/USERNAME/happy_house/rules.txt /home/USERNAME/new_house/rules.txt`
2. `cp ~/happy_house/rules.txt ~/new_house/rules.txt`
3. `cp ~/happy_house/rules.txt ~/new_house`
4. `cp ~/happy_house/rules.txt .`
5. `cp ../happy_house/rules.txt .`
All the commands above do the same thing. The difference is the way we did specify the source and destination path.
The 1. command is the most verbose one. We specify the full path of the source and destination.
In the 2. command, we did use `~` which is a shortcut to `/home/USERNAME`.
In the 3. command, we did remove the file name from the destination path. If a file name is not specified in the destination path, then the file name from the source is used.
In the 4. command, we did use the dot `.` as a shortcut to the current directory since we are currently in the directory `new_house`.
In the 5. command, we did use the two dot `..` instead of `~` which is also possible since the parent directory is `~` in this case. The double dot is useful we are operating in directories that are nested deeper in your home.
You might think: `.` is the path to the current directory. `..` is the path of the parent directory. Is `...` the path of the parent directory of the parent directory? Lets try it out:
```console
$ ls .
rules.txt
$ ls ..
happy_house new_house (...)
$ ls ...
ls: cannot access '...': No such file or directory
```
So this does not work. Anything number of dots greater that 2 does not word, except if we use separtors. To access the parent directory of the parent directory, you can use `../..`:
```console
$ ls ../..
USERNAME (...)
```
You will probably only see your user name as output, but if the system you are using has more than one user, then names of the other users would be in the output, too. Every user has his own home directory under `/home`.
The usage of `../..` or even more dots like `../../..` is not recommended since you would have to go multiple directories up in your mind and this does not work well! Use the full path for paths not in the current `.` or in the parent direcotry `..`.
What we have just learned about paths does not only apply to `cp`, but also to `ls`, `mkdir`, `mv` and other commands that deal with paths.
One thing has to be mentioned about `cp`: To copy directories instead of just files, use the option/flag `-r` to copy recursively like with `mv`.
It might be overwhelming for you to memorize all the commands. Again, if a command is an abbrevation, then knowing what it stands for is very helpful. Otherwise, just look up the command you are looking for in this book or in a search engine ;) It might be overwhelming for you to memorize all the commands. Again, if a command is an abbrevation, then knowing what it stands for is very helpful. Otherwise, just look up the command you are looking for in this book or in a search engine ;)