picks up where I left of during sick leave

This commit is contained in:
waldek 2022-05-09 17:39:23 +02:00
parent 4f9e0ccf3d
commit 74fac49ec2
4 changed files with 180 additions and 1 deletions

BIN
assets/nano_01.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

BIN
assets/nano_02.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

BIN
assets/nano_03.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

View File

@ -1395,8 +1395,42 @@ waldek@hellodebian:~/Documents$
### `nano`
The *easiest* to use command line editor that comes installed with most Linux distributions is called `nano`.
Below is a screenshot of an empty text file, opened up in `nano`.
![nano new file](./assets/nano_01.png)
In this window we can write anything we want.
I added two lines of text, but how do I *save and quit* this program?
At the bottom of the screen there is a *menu*.
`^X` is the *button* to exit `nano` but how do we invoke it?
Well, by pressing **CRTL-x**.
By logical consequence to search the text file we can use **CTRL-w**.
When you try to exit, dialog will pop up to ask you to save.
If you answer **Y**, you'll be prompted for a filename.
Here you can just hit **ENTER**.
![nano quit](./assets/nano_02.png)
![nano save](./assets/nano_03.png)
# Exercise
Create some text files with `nano`.
Move them around with `mv`, delete them with `rm` and copy them with `cp`.
Can you edit the `/etc/passwd` file?
### `vi` and `vim`
On of the most [popular](https://www.journaldev.com/41292/top-best-text-editors-linux) text editors is by far `vim`.
It is an *improvement* on an older editor called `vi`, vim(proved).
Personally it is my favorite editor, or even program, on Linux and I can highly advise you to become somewhat comfortable with it.
The learning curve is quite *steep* but so very much worth it!
The reason *why* it is a bit complicated at first is because `vim` is a [modal](https://www.wikidata.org/wiki/Q80592893) text editor.
The paragraph below is taken from the [wikipedia](https://en.wikipedia.org/wiki/Vi) page of `vi`.
> vi is a modal editor: it operates in either insert mode (where typed text becomes part of the document) or command mode (where keystrokes are interpreted as commands that control the edit session). For example, typing i while in command mode switches the editor to insert mode, but typing i again at this point places an "i" character in the document. From insert mode, pressing ESC switches the editor back to command mode. A perceived advantage of vi's separation of text entry and command modes is that both text editing and command operations can be performed without requiring the removal of the user's hands from the home row. As non-modal editors usually have to reserve all keys with letters and symbols for the printing of characters, any special commands for actions other than adding text to the buffer must be assigned to keys that do not produce characters, such as function keys, or combinations of modifier keys such as Ctrl, and Alt with regular keys. Vi has the property that most ordinary keys are connected to some kind of command for positioning, altering text, searching and so forth, either singly or in key combinations. Many commands can be touch typed without the use of Ctrl or Alt. Other types of editors generally require the user to move their hands from the home row when touch typing:
# Exercise
The only way to learn `vim` is to *use* it.
@ -1404,17 +1438,162 @@ Luckily `vim-nox` comes with a dedicated *program* to learn it called `vimtutor`
It's actually a text file that you open up in `vim` where you just need to **read** and **execute** what you've read.
Enjoy!
Once you have completed the `vimtutor` I urge you to have a look at one of the *many* cheat sheets [online](https://vim.rtorr.com/).
# Opening files
We can use `vim` or `nano` to open text files and write to them, but there are a few programs worth knowing that can *just* show the content of a text file.
You'll use these all the time!
Before we've seen that `head` shows the first few lines of a file.
To see the *last lines* of a file we use `tail`.
Get it, *head and tail*?
The linux world is full of little inside jokes like this.
Anyway, the two most used programs to **read** file content are `cat` and `less`.
`cat` prints the content of a file straight to the terminal, where as `less` is what we call a [pager](https://en.wikipedia.org/wiki/Terminal_pager).
If you need to search or read a long file you'll find that a pager is more practical.
Reading the `man less` page we see the following.
```
LESS(1) General Commands Manual LESS(1)
NAME
less - opposite of more
```
Again, the joke here is that `less` can actually do *more* than `more`.
# Exercise
Open up some files with `cat` and `less`.
If you need some inspiration, try a few files in the `/etc` directory.
# Finding stuff
## Finding files
### The easy way
On most distributions `locate` will not be installed by default but I advise you to install it, especially in the beginning of your linux career.
The syntax is super simple and it fetches results from a database instead of the hard drive so it's quite quick and doesn't have permission problems.
To install and use it, you should proceed as follows.
```
waldek@debian:~$ sudo apt install locate
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
locate is already the newest version (4.8.0-1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
waldek@debian:~$ locate bin/zip
/usr/bin/zipdetails
waldek@debian:~$ locate zip | grep bin # this is a weird thing...
/usr/bin/bunzip2
/usr/bin/bzip2
/usr/bin/bzip2recover
/usr/bin/gunzip
/usr/bin/gzip
/usr/bin/streamzip
/usr/bin/zipdetails
/usr/lib/klibc/bin/gunzip
/usr/lib/klibc/bin/gzip
waldek@debian:~$
```
You'll probably be a bit confused by the `|` part of the commands above.
Don't worry, give it a few hours paragraphs and it will become fully transparent!
Next, there is a very high probability that the commands above don't immediately work for you.
This is because `locate` functions with a database which needs to be updated from time to time.
This is handled automatically by a [cron](https://en.wikipedia.org/wiki/Cron) job.
We'll go into detail on how to schedule jobs ourselves but for now just remember you can trigger a refresh of the database yourself with the following command.
```
waldek@debian:~$ locate nano.html
waldek@debian:~$ sudo updatedb
waldek@debian:~$ locate nano.html
/usr/share/doc/nano/nano.html
waldek@debian:~$
```
### The hard way
A program you'll find on most Linux distributions to search for files is `find`.
It performs a real time search a crossed the entire file system and prints out the path of the file found.
A simple example can be seen below.
```
waldek@debian:~$ find /usr -iname "nano.html"
/usr/share/doc/nano/nano.html
waldek@debian:~$
```
The real power lies in it's various command line arguments.
We can for example search for all files which are bigger than 100MB, owned by a specific user or of a certain permission.
All of these conditions can be combined to improve the specificity of your query.
There are a few exercises below so I won't give too many spoiler.
My best advise is to read `man find` and experiment.
## Searching inside files
`locate` and `find` are used to search **for** files.
To search for content **inside** files we can use an other very powerful tool called `grep`.
It is often combined with [regular expressions](https://www.gnu.org/software/grep/manual/html_node/Regular-Expressions.html), or regex, for search for patterns.
The syntax of regex's is a bit challenging but, like vim, it is very much worth the effort.
Even more, you can use regex's *inside* vim to search and replace text!
This being said, you can use `grep` without regular expressions as well and this is what we'll do first.
Most distributions will come with some dictionaries installed.
We can find the American one with the `locate` command.
```
waldek@debian:~$ locate american
/usr/lib/ispell/american.aff
/usr/lib/ispell/american.hash
/usr/share/dict/american-english
/usr/share/doc/iamerican
/usr/share/doc/wamerican
/usr/share/doc/wamerican/NEWS.Debian.gz
/usr/share/doc/wamerican/README.Debian
/usr/share/doc/wamerican/changelog.Debian.gz
/usr/share/doc/wamerican/copyright
/usr/share/doc/wamerican/wamerican.scowl-word-lists-used
/usr/share/ispell/american.med+.mwl.gz
/usr/share/ispell/american.mwl.gz
/usr/share/man/man5/american-english.5.gz
/var/lib/dictionaries-common/ispell/iamerican
/var/lib/dictionaries-common/wordlist/wamerican
/var/lib/dpkg/info/iamerican.config
/var/lib/dpkg/info/iamerican.list
/var/lib/dpkg/info/iamerican.md5sums
/var/lib/dpkg/info/iamerican.postinst
/var/lib/dpkg/info/iamerican.postrm
/var/lib/dpkg/info/iamerican.preinst
/var/lib/dpkg/info/iamerican.templates
/var/lib/dpkg/info/wamerican.config
/var/lib/dpkg/info/wamerican.list
/var/lib/dpkg/info/wamerican.md5sums
/var/lib/dpkg/info/wamerican.postinst
/var/lib/dpkg/info/wamerican.postrm
/var/lib/dpkg/info/wamerican.templates
/var/lib/ispell/american.compat
/var/lib/ispell/american.hash
/var/lib/ispell/american.remove
```
## Wildcards and regular expressions
# Exercise
TODO find some specific files and some specific pattern in config files
Inside a clean minimal installation can you please:
* find all files that are 3286 bytes
* find all files not owned by `root` **and** `$USER` (yourself)
* find all symbolic links
* find all files that contain *ssh* in the filename **and** are smaller that 10 bytes
* find all files in `/usr` that contain the word *hippo*
* find all files in `/usr` that contain the word *hippop* or *hiphop*
* find all Belgian email addresses in `/usr`
* [exercise from Linux long](https://gitea.86thumbs.net/waldek/linux_course_doc/src/branch/master/modules/qualifying/learning_regex.md)