1
0
Fork 0
mirror of https://codeberg.org/Mo8it/How_To_Linux.git synced 2024-11-24 22:11:35 +00:00

Small changes to day 1

This commit is contained in:
Mo 2022-09-23 18:42:56 +02:00
parent f89fd1afeb
commit b9f5bd6d65
3 changed files with 54 additions and 48 deletions

View file

@ -4,7 +4,7 @@ _by Mo Bitar_
So you want to learn Linux? You want to fulfill your childhood dream and look like a hacker? Or you just want to manage some servers or escape Windows or Mac for an alternative system that is [free and open source](https://www.gnu.org/philosophy/free-sw.html.en)? Then this book is for you!
This book focuses on the terminal side of Linux. This means that no [GUI](https://en.wikipedia.org/wiki/Graphical_user_interface) (graphical user interface) is discussed in this book. This makes the content of the book more useful for server usage and administration since servers normally have no GUI. Nevertheless, if you want to use Linux as a desktop, then being able to deal with the terminal is not only useful, but could even be required at some point.
This book focuses on the terminal side of Linux. This means that no [GUI](https://en.wikipedia.org/wiki/Graphical_user_interface) (graphical user interface) is discussed in this book. This makes the content more useful for server usage and administration since servers normally have no GUI. Nevertheless, if you want to use Linux as a desktop, then being able to deal with the terminal is not only useful, but could even be required at some point.
You might ask, why not just use a GUI for everything? Hopefully, this book will let you understand why. A [CLI](https://en.wikipedia.org/wiki/Command-line_interface) (command line interface) is not well suited for everything. But for many tasks, a CLI might be much more powerful. It allows automation and enables you to use it as a component to build something bigger.

View file

@ -8,9 +8,9 @@ Therefore, we need to learn how to install so called packages to get access to a
### Package managers
In Linux, you don't install programs by using a search engine to find a website, then visit the website and download an installer, then run the installer and click "Next" and "Agree" 100 times without even reading what you are agreeing to.
In Linux, you don't install programs by using a search engine to find a website, then visit the website and download an installer, then run the installer and click "Next" and "Agree" repeatedly without even reading what you are agreeing to 🤮
This method is too insecure. You have to trust the website that you did visit. You have to hope that no malware is delivered while you are downloading the installer from the website. You can get the malware either from the website itself or through a man-in-the-middle attack. Then the installer might do whatever it wants. You just click "Next" and suddenly your system shows you (extra) ads or your browser search preferences are overwritten with Yahoo. Surprise! 🎉
This method is too insecure. You have to trust the website that you did visit. You have to hope that no malware is delivered while you are downloading the installer from the website. You can get the malware either from the website itself or through a man-in-the-middle attack. Then the installer might do whatever it wants. You just click "Next" and suddenly your system shows you (extra) ads or your browser search preferences are overwritten by Yahoo. Surprise! 🎉
In Linux, you (mainly) install software through the package manager that ships with your distribution. The software is then downloaded from a repository which is a collection of packages that is managed by the distribution. The distribution managers make sure that no malware is added to their repositories.
@ -18,7 +18,7 @@ The package manager does take care of verifying the downloaded packages before i
The package manager does also take care of installing any other packages that you did not specify but are required by the package that you want to install. These packages are called dependencies.
Since this book uses a Fedora system for demonstrations, the book will use Fedora's package manager `dnf`.
Since this book uses a [Fedora](https://getfedora.org) system for demonstrations, the book will use Fedora's package manager `dnf`.
If you are using another Linux distribution, you might need to replace the commands of `dnf` with `apt` for Debian based distributions for example.
@ -28,7 +28,7 @@ To install a package, you need administrative privileges. Therefore, we need som
Lets install our first package! To do so, enter `sudo dnf install cmatrix`. You will be asked for the password of your user. Type the password and then press `Enter`. For security reasons, you will not be able to see you password while entering it. So don't wonder why nothing happens while typing the password. Just type it and then press `Enter`.
The package manager might need some time to sync some data, then it will show you a summary of what it will do and ask you for confirmation. Type `y` for _yes_ and then press `Enter` for the installation to start.
The package manager might need some time to synchronize some data, then it will show you a summary of what it will do and ask you for confirmation. Type `y` for _yes_ and then press `Enter` for the installation to start.
After the installation is done, you can enter `cmatrix`. Congratulations, you are a hacker now! 💻🤓
@ -36,7 +36,7 @@ To exit the matrix, press `q`.
What if you don't like the matrix and want to remove it? You can uninstall packages using `sudo dnf remove PACKAGENAME`. In this case: `sudo dnf remove cmatrix`. You have to confirm again with `y`.
Why do we speak about _packages_ instead of programs when installing software on Linux? Because packages can contain more than one binary (the actual programs) and extra files. Take a look at [the files that are installed with `cmatrix` on Fedora](https://packages.fedoraproject.org/pkgs/cmatrix/cmatrix/fedora-rawhide.html#files) for example.
Why do we speak about _packages_ instead of programs when installing software on Linux? Because packages can contain more than one binary (the actual program) and extra files. Take a look at [the files that are installed with `cmatrix` on Fedora](https://packages.fedoraproject.org/pkgs/cmatrix/cmatrix/fedora-rawhide.html#files) for example.
> Warning ⚠️ : While installing or uninstalling packages, it is important to take a look at the summary before confirming the action with `y`. There is a reason why a confirmation is required. Sometimes, some packages depend on packages with older versions than the versions that exist on your machine. If you want to install those packages anyway, other packages that depend on the newer versions can break!
>
@ -59,7 +59,7 @@ Any action that might modify the system needs administrative privileges. Install
Sometimes, an action might not modify the system, but a user might be restricted through the permissions system to not be able to access a file for example. In this case, you would also need to use `sudo`.
Linux has a superuser, one user that exists on every Linux system and is able to do anything! This user is the `root` user. For security reasons (and also to not do something destructive by a mistake), this user is often locked. `sudo` allows you as a non root user to run your command as a `root` user.
Linux has a superuser, one user that exists on every Linux system and is able to do anything! This user is the `root` user. For security reasons (and also to not do something destructive by a mistake), this user is often locked. `sudo` allows you as a non root user to run commands as a `root` user.
You are not always allowed to use `sudo`. If you don't own the machine you are using and just share it with others, then there is a high chance that only administrators of the machine have `sudo` access.
@ -73,15 +73,15 @@ If you don't have access to `sudo` and try to use it, then you get an output lik
>
> Even commands that you know like `rm` can destroy your system if you use them with `sudo` without knowing what they are doing.
>
> You might find a "meme" in the internet that tells you to run something like `sudo rm -rf /`. This command will delete EVERYTHING on your machine. It is like delete the `C` and all other drives at the same time on Windows.
> You might find a "meme" in the internet that tells you to run something like `sudo rm -rf /`. This command would delete EVERYTHING on your machine. It is like deleting the `C` and all other drives at the same time on Windows.
>
> Linux assumes that you know what you are doing when you use `sudo`. With great power comes great responsibility!
> Linux assumes that you know what you are doing when you use `sudo`. _With great power comes great responsibility!_
### Looking for a package
If you don't exactly know the name of a package you are looking for, then you can use `dnf search PATTERN`. DNF will then return packages that match the pattern in their name or description.
For example, if you search for Julia (which is a programming language, you get the following results):
For example, if you search for Julia (a programming language) you get the following results:
```console
$ dnf search julia
@ -157,7 +157,7 @@ Architecture : x86_64
(...)
```
You get too results for Julia that seem to be identical. The difference is the _architecture_. Normal computers usually have the `x86_64` architecture, so you should look for it. But it is nice to know that other architectures are also supported.
You get two results for Julia that seem to be identical. The difference is the _architecture_. Normal computers usually have the `x86_64` architecture, so you should look for it. But it is nice to know that other architectures are also supported.
If you have a problem with a package after an update, then you can try to use an older version. To do so, refer to the documentation of `dnf downgrade`.

View file

@ -1,6 +1,8 @@
# 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.
The terminal, the console, the command line. All three terms refer to the same thing.
Get access to a Linux system and open a terminal.
Warning ⚠️ : **Don't panic!**
@ -31,11 +33,13 @@ I know, I did just brake the romance between you and your computer. I apologize.
### 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?
What if you want to take a look at the content of a different directory at different path?
The output is going to be the files and directories in your current path. `ls` stands for _list_.
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`.
What if you want to take a look at the content of a different directory at a different path?
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 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_house` really empty? Lets verify that.
@ -54,11 +58,13 @@ 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.
But wait a minute, our directory `empty_house` is not empty anymore! Lets fix that by renaming it.
But wait, 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 dots `..` refer to the parent directory in Linux.
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.
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, `mv` does move a file or directory to a new destination with the possiblity 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`.
@ -105,7 +111,7 @@ $ rm zombies
rm: cannot remove 'zombies': Is a directory
```
So it does not work. To delete an empty directory, use `mrdir`:
So it does not work. To delete an empty directory, use `rmdir`:
```console
$ rmdir zombies
@ -130,7 +136,7 @@ What to do now? Turns out, `rm` is more powerful than we thought and can delete
What is a flag? It is an option that you specify after the command name that enables some functionality.
In our case, to remove a directory that is not empty, we need the `-r` flag of `rm`:
In our case, to remove a directory that is not empty, we need the `-r` flag (also called option) of `rm`:
```console
$ tree
@ -165,7 +171,7 @@ What you see is the path to your current directory (also called working director
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`.
There are many ways to edit text files in the terminal. The simplest method is to use the editor `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.
@ -210,7 +216,7 @@ We did use `cat` to only print the content of the file `rules.txt` without openi
### 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:
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 have. To do so, we can use the command `wc` which actually stands for _word count_, but it is able to count more than words:
```console
$ wc rules.txt
@ -219,9 +225,9 @@ $ wc 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?
I do not memorize everything! I had to look up the meaning of the three numbers. Where do you look up such things?
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.
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` followed by the name of the command that we want to look up. `man` stands for _manual_.
Lets look up the command `wc`:
@ -258,11 +264,11 @@ DESCRIPTION
`(...)` 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 structure of the output is a standard for manuals (also called _man pages_) 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 second section is _SYNOPSIS_ and shows different ways 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.
@ -292,13 +298,13 @@ $ 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 see that we get the number of lines and then the number of words.
We did learn how to access and read the manual, but do we navigate the manual?
We did learn how to access and read the manual, but how 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 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 for, `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`).
If you get more than one match for your search pattern, 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.
@ -306,7 +312,7 @@ Fun fact: You can read the manual of the manual with `man man` 😃
### Help!
If you want to read a smaller version of the manual, you can run `COMMAND --help`. Almost every command supports the `--help` flag and its small version `-h`. You will then get a quick reference.
If you want to read a smaller version of the manual, you can run `COMMAND --help`. Almost every command supports the `--help` option and its small version `-h`. You will then get a quick reference.
Try `wc --help`!
@ -320,11 +326,11 @@ $ 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.
We did not use any new commands, but what is this symbol `~`? We did already mention it and you might have noticed that it was shown 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 ~`.
By entering `cd ~`, we did make sure that we are in our "home" before creating a new directory. Entering 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_:
So we did create a new house directory and 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
@ -339,9 +345,9 @@ 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.
We did learn that the two dots `..` refer to the parent directory (one directory back). One dot `.` refers to the current directory.
Here are some equivalent commands that might help you understand paths in Linux:
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`
@ -351,15 +357,15 @@ Here are some equivalent commands that might help you understand paths in Linux:
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.
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 **2. command**, we 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 **3. command**, we 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 **4. command**, we 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.
In the **5. command**, we use the two dots `..` instead of `~` which is also possible since the parent directory is `~` in this case. The two dots are useful when we are operating in directories that are nested deeper in the home directory.
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:
@ -372,20 +378,20 @@ $ 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 separators. To access the parent directory of the parent directory, you can use `../..`:
So this doesn't work. Any number of dots greater that 2 doesn't work, except if we use separators. 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`.
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 an 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 directory `..`.
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 `rm`.
One thing has to be mentioned about `cp`: To copy directories instead of just files, use the option `-r` to copy recursively like with `rm`.
### Terminal shortcuts
@ -395,7 +401,7 @@ If you have another directory in your home that is called something like `happy_
Often, commands are not long, but you can use autocompletion to complete commands too. If you type `ech` and then press `Tab`, you get the autocompletion `echo` with the space at the end to enter options or arguments. In this case, we did only save entering one character and a space, so `echo` is not the best opportunity for autocompletion. But this is only a demonstration. Some commands might be longer.
You might think that you don't need autocompletion at all. But you should use it. Not only for autocompletion, but for verification!
You might think that you don't need autocompletion at all. But you should use it. Not only for the autocompletion itself, but for verification, too!
When you type `cat hapy_house/ru` and then press `Tab`, you don't get any autocompletion although you would expect it. But did you notice the missing `p` in `happy`?
@ -403,9 +409,9 @@ This is meant by verification. If you don't get an autocompletion although you t
But what if you did run a command with a mistake and you have to correct it? Or what if you want to run the same command with small modifications? Do you have to type the whole command again?
Fortunately, no! You can use the up and down arrows to navigate through your commands history. Try it out!
Fortunately, no! You can use the up ⬆️ and down ⬇️ arrow keys to navigate through your commands history. Try it out!
If you started typing a command and you did notice that you did miss something in the middle of the command, you might try to use the mouse and click. No clicks will help you! Instead, use the left and right arrow keys.
If you started typing a command and you did notice that you did miss something in the middle of the command, you might try to use the mouse and click. No clicks will help you! Instead, use the left ⬅️ and right ➡️ arrow keys.
If you want to go to the beginning of the command, press `Ctrl + a`. Then you can go back to the end with `Ctrl + e`.
@ -429,11 +435,11 @@ Enter `python3 nonstop.py` to run the code.
Annoying, right? To stop a running program, press `Ctrl + c`. You _have to_ remember this shortcut!
You can also use `Ctrl + c` if are typing a command and you want to start over. Instead of pressing `Backspace` for a long time, just press `Ctrl + c`!
If you are typing a command and want to start over, you can use `Ctrl + c` instead of pressing `Backspace` repeatedly.
But wait, `Ctrl + c` is (normally) used for copying, right?
No, not in the terminal. If you want to copy text in the terminal, then select it with the mouse and the press `Ctrl + Shift + C`.
No, not in a terminal. If you want to copy text in a terminal, then select it with the mouse and the press `Ctrl + Shift + C`.
To paste text into the terminal, press `Ctrl + Shift + v`.