aboutsummaryrefslogtreecommitdiff
path: root/content/weblog/2019-01-11_intro-to-linux-and-bash-pt3
diff options
context:
space:
mode:
authorYaroslav <contact@yaroslavps.com>2020-04-12 18:54:03 +0300
committerYaroslav <contact@yaroslavps.com>2020-04-12 18:54:03 +0300
commit6a198ebdcaad3b7f524c78f91a46679064b7faa8 (patch)
tree4c0d558aa5f9410cc0cb034a49286dd6b2291b64 /content/weblog/2019-01-11_intro-to-linux-and-bash-pt3
parent65dfb21ce4bfd6c9b3d1628adeee0acfc423b2b5 (diff)
downloadyaroslavps.com-6a198ebdcaad3b7f524c78f91a46679064b7faa8.tar.gz
yaroslavps.com-6a198ebdcaad3b7f524c78f91a46679064b7faa8.zip
linux and bash pt 3
Diffstat (limited to 'content/weblog/2019-01-11_intro-to-linux-and-bash-pt3')
-rw-r--r--content/weblog/2019-01-11_intro-to-linux-and-bash-pt3/index.md557
-rw-r--r--content/weblog/2019-01-11_intro-to-linux-and-bash-pt3/index.ru.md542
2 files changed, 1099 insertions, 0 deletions
diff --git a/content/weblog/2019-01-11_intro-to-linux-and-bash-pt3/index.md b/content/weblog/2019-01-11_intro-to-linux-and-bash-pt3/index.md
new file mode 100644
index 0000000..eaf2757
--- /dev/null
+++ b/content/weblog/2019-01-11_intro-to-linux-and-bash-pt3/index.md
@@ -0,0 +1,557 @@
++++
+title = "Intro to Linux and the Bash command line, pt III"
+date = 2019-01-11T08:23:00Z
++++
+
+New year, new post. In this third, and most probably final part of these
+tutorial/guide series I will be mentioning some useful commands and programs
+usually present in most standard linux installations. I will be talking
+especially about programs/commands to manipulate text output from programs and
+files. I will also talk a little bit about regular expressions, a powerful tool
+to perform searches inside text strings.
+
+<!-- more -->
+
+## Filters
+
+These programs that perform operations on input text and then write them to
+standard output are commonly known as filters. You may already be familiar with
+one of these commands, which the first one that I'm going to talk about.
+
+### cat
+
+This command allows you the see the content of a text file (or files). It
+stands for con*cat*enate, and not for the house pet. The most basic use of this
+command is to view the contents of a text file, just by typing cat followed by
+the path of the file we wish to see. However, as it name implies, it also has
+the ability to concatenate the contents of multiple text files, for example
+text file 'sample.txt'
+
+```sh
+user@host:~/Documents/notes$ cat sample.txt
+Pepe cool
+Tide Pods lame
+Uganda Knuckles cool
+Thanos cool
+JPEG ok
+Despacito lame
+Bowsette cool
+Harold cool
+Sans coolest
+Minions lamest
+NPC cool
+```
+
+And we want to concatenate its content with file 'sample2.txt' to standard
+output
+
+```sh
+user@host:~/Documents/notes$ cat sample.txt sample2.txt
+Pepe cool
+Tide Pods lame
+Uganda Knuckles cool
+Thanos cool
+JPEG ok
+Despacito lame
+Bowsette cool
+Harold cool
+Sans coolest
+Minions lamest
+NPC cool
+Troll Face old
+Can haz chezburger really old
+ROFLcopter super old
+Dancing baby ancient
+```
+
+As usual, this command accepts different options, like for example the -n
+option to display line
+
+```sh
+user@host:~/Documents/notes$ cat -n sample.txt
+ 1 Pepe cool
+ 2 Tide Pods lame
+ 3 Uganda Knuckles cool
+ 4 Thanos cool
+ 5 JPEG ok
+ 6 Despacito lame
+ 7 Bowsette cool
+ 8 Harold cool
+ 9 Sans coolest
+ 10 Minions lamest
+ 11 NPC cool
+```
+
+As always, you can check other options by looking up cat and any other command
+with man, as explained in the previous part.
+
+### head
+
+This is a really simple command, it shows the first n lines of a text
+file/output. To use it, type head, followed by -n, then the number of lines to
+show, and then the file. For example, let's say we want to see the first 5
+lines of sample.txt
+
+```sh
+user@host:~/Documents/notes$ head -n 5 sample.txt
+Pepe cool
+Tide Pods lame
+Uganda Knuckles cool
+Thanos cool
+JPEG ok
+```
+
+If we use the command without passing the number of lines we wish to see, it
+outputs the first 10 lines by default. For this just type head followed by the
+path of the file.
+
+### tail
+
+Basically the same as head, except it shows the last lines. Let's say we want
+to see the last three lines
+
+```sh
+user@host:~/Documents/notes$ tail -n 3 sample.txt
+Sans coolest
+Minions lamest
+NPC cool
+```
+
+As with head, the default is to output 10 lines.
+
+### sort
+
+This command is as obvious as it seems. It sorts output. For example
+
+```sh
+user@host:~/Documents/notes$ sort sample.txt
+Bowsette cool
+Despacito lame
+Harold cool
+JPEG ok
+Minions lamest
+NPC cool
+Pepe cool
+Sans coolest
+Thanos cool
+Tide Pods lame
+Uganda Knuckles cool
+```
+
+### sed
+
+This one is a really powerful utility to transform and manipulate text,
+however, to keep this tutorial short, I will only be showing a couple of the
+most used cases. sed stands for "stream editor".
+
+The way to use sed, is to pass it a kind of script (a sed script) that tells it
+what to do with the text. The first and one of the most basic uses of sed, is
+to basically perform the same task as head, to get the first n number of lines.
+For example, let's say we want the first 7 lines of sample.txt
+
+```sh
+user@host:~/Documents/notes$ sed '7q' sample.txt
+Pepe cool
+Tide Pods lame
+Uganda Knuckles cool
+Thanos cool
+JPEG ok
+Despacito lame
+Bowsette cool
+```
+
+Of course what I've just told it you it does is a simplification of what it
+really does. Most accurately, the command or script that we passed to sed tells
+it to output the first seven lines, and the q tells it to stop after that.
+
+Another basic use of sed, and arguably the most common one, is to perform
+search and replace operations on text. The basic syntax for this operations is
+'s/<search>/<replace>/' where <search> is the term you want to search for, and
+<replace> is the term you wish to replace it with.
+
+By default it will replace only the first occurrence in each line, however, we
+can specify which or how many occurrences we want to replace by adding a number
+and/or letter to the end. For example, if we add a two
+('s/<search>/<replace>/2') it will replace only second occurrences of each
+line.
+
+But what if we want to replace each and every occurrence in all of the text?
+For that we would use the letter g at the end. Let's say for example, that we
+want to replace all occurrences of "cool" in our sample.txt file, for "dank".
+In this case we would type something like this
+
+```sh
+user@host:~/Documents/notes$ sed 's/cool/dank/g' sample.txt
+Pepe dank
+Tide Pods lame
+Uganda Knuckles dank
+Thanos dank
+JPEG ok
+Despacito lame
+Bowsette dank
+Harold dank
+Sans dankest
+Minions lamest
+NPC dank
+```
+
+A thing to keep in mind, is that you should be enclosing the sed script in
+single quotes. Of course these are only some of the most basic uses of this
+command.
+
+### grep
+
+This is the last program to manipulate text output that I want to mention. I
+will demonstrate its basic use in this section, but I will show you a little
+bit more about it in the next section when I will be writing about regular
+expressions.
+
+Back to grep, it is a program that basically searches a pattern that you give
+it, and it will print to you the lines that contain that pattern. For example,
+let's say that we want to see only the cool (or dank) memes in our file to be
+displayed
+
+```sh
+user@host:~/Documents/notes$ grep 'cool' sample.txt
+Pepe cool
+Uganda Knuckles cool
+Thanos coolcharacter
+Bowsette cool
+Harold cool
+Sans coolest
+NPC cool
+```
+
+This line of text that we passed it, is actually the most basic form of regular
+expression, of which we will be looking into detail next.
+
+## Regular expressions
+
+A regular expression, or regex for short, is a string of text, that define a
+search pattern for a larger set of text. Regexes are used in many programs,
+such as in text editors, and search engines, and can be also of great use in
+the terminal
+
+### An intermission
+
+Before going into actual regular expressions in grep, I want to mention a
+couple of characters that can make your life easier when dealing with files in
+the terminal. They are called wildcards, and they are the asterisk (*) and the
+question mark (?). If you've ever wondered why you can't use those characters
+in any of your files' names, that's why.
+
+I'll start by explaining the asterisk. When you use the asterisk, you are
+asking to look at or take all files that contain the any number of any
+combination of symbols in the place where you put it. For example, we could be
+looking at files that start with sa
+
+```sh
+user@host:~/Documents/notes$ ls sa*
+saturday.txt sample.txt sample2.txt sample.png
+```
+
+Or another example, we could be looking for files that just contain sa in their
+name
+
+```sh
+user@host:~/Documents/notes$ ls *sa*
+asado.png saturday.txt sample.txt sample2.txt sample.png
+```
+
+Now the question mark. The question mark indicates that there should be a
+character in its place, just any character. Let's say that we want to see all
+files with name "sample" that have a three character extension
+
+```sh
+user@host:~/Documents/notes$ ls sample.???
+sample.txt sample.png
+```
+
+Wildcards come really handy when you need to manipulate multiple files with
+similar names. If the files that you wish to manipulate don't really have
+similar names, you might want to use curly braces to indicate a list of files
+to manipulate, separated by commas. For example
+
+```sh
+user@host:~/Documents/notes$ rm {monday.txt,december1999.txt,saturday.txt}
+```
+
+### Back to regex
+
+Now I'll explain some things about regular expressions, and I'll demonstrate
+some basic uses with grep. Here are some basic concepts
+
+* `.` - The dot means a single character (any character). e.g. 'be.r' would match
+ bear, beer, befr, etc.
+* `*` - The preceding element matches 0 or more times. e.g. 'an*t' would match
+ at, ant, annt, annnt, etc.
+* `+` - The preceding element matches one or more times. e.g. 'an+t' would match
+ ant, annt, annnt, etc.
+* `?` - The preceding element matches 0 or one time. e.g. 'an?t' would match at,
+ and ant.
+* `{n}` - The preceding element matches exactly n times.
+* `{min, }` - The preceding element matches at least min times.
+* `{min, max}` - The preceding element matches at least min times, and no more
+ than max times.
+* `|` - The pipe, logical OR operator. e.g. 'gray|grey' would match gray and grey
+* `()` - The parenthesis group multiple characters as one element. e.g.
+ 'gr(a|e)y' would match gray and grey.
+* `[abc]` - It matches if a character is one of those inside the brackets.
+* `[^abc]` - It matches if none of the characters is one of those inside the
+ brackets.
+* `[a-d]` - A range of characters. i.e. a, b, c, or d.
+* `^` - Matches the beginning of the line.
+* `$` - Matches the end of the line.
+
+So now let's suppose for a practical example with grep, that we want to find
+all lines that have "cool" or "ok" in them. In this case we would use the "|"
+pipe symbol. However, if we use normal grep, we would have to escape the pipe
+symbol like this "\|". That's why it is better that we use "grep -E" to enable
+extended regex, or its shorter alias "egrep". It would look something like this
+
+```sh
+user@host:~/Documents/notes$ egrep 'cool|ok' sample.txt
+Pepe cool
+Uganda Knuckles cool
+Thanos cool
+JPEG ok
+Bowsette cool
+Harold cool
+Sans coolest
+NPC cool
+```
+
+Let's suppose, for another example, that we want to match those lines with a 't' as the last character
+
+```sh
+user@host:~/Documents/notes$ egrep 't$' sample.txt
+Sans coolest
+Minions lamest
+```
+
+I have already mentioned and shown you the use of regexes with grep (and/or
+egrep). Now I would like to show a more practical example with sed. Yes, sed
+uses its own script language to alter text input, however, it also makes use of
+regular expressions.
+
+Let's suppose that we have a file that looks like this
+
+```sh
+user@host:~/Documents/notes$ cat shortcuts
+# Some shortcuts
+
+d ~/Documents
+D ~/Downloads
+m ~/Music
+pp ~/Pictures
+vv ~/Videos
+
+
+s ~/.scripts # My scripts
+cf ~/.config # My configs
+```
+
+As we can see there is a lot of whitespace, and although comments might be of
+help to humans, they are of no use to machine. Let's begin by getting rid of
+the comments, for that first need to remember the search and replace command of
+sed, 's/<search>/replace/g', since we basically want to get rid of any
+comment-looking string and replace it with, well, nothing. Now we have to think
+of a regex that will match comments, for that '#.*' will do. What regex means
+is, match '#' and everything after it. Now let's put it together, and
+
+```sh
+user@host:~/Documents/notes$ sed 's/#.*//g' shortcuts
+
+
+d ~/Documents
+D ~/Downloads
+m ~/Music
+pp ~/Pictures
+vv ~/Videos
+
+
+s ~/.scripts
+cf ~/.config
+```
+
+Bam, there it is. However, we still have the blank lines left, and, if you pay
+close attention, the comments have been deleted, but, the spaces that used to
+be before some of the comments are still there.
+
+So first, let's improve our current sed command, if we want to match 0 or more
+spaces (zero because not every comment has a space before it) we would use the
+`*` symbol, but what symbol would we use for spaces? Well, that's an easy one, in
+sed we escape spaces like this '\s', so now our sed command looks like this
+'s/\s*#.*//g'.
+
+Let's take care of the last part, getting rid of blank lines. For this we would
+need to issue a separate command, but fortunately we can stack commands in one
+line with a semicolon (;). Now that we know that we need a way to match empty
+lines with a regex, that's very easy - '^$' just match the beginning and the
+end of line together, after that, we add a sed command for deleting lines which
+I haven't mentioned (d), and our one liner is ready...
+
+```sh
+user@host:~/Documents/notes$ sed 's/\s*#.*//g; /^$/d' shortcuts
+d ~/Documents
+D ~/Downloads
+m ~/Music
+pp ~/Pictures
+vv ~/Videos
+s ~/.scripts
+cf ~/.config
+```
+
+Of course, issuing this command will not replace the original file, it will
+simply output the result to the terminal screen. If you want to overwrite the
+original file with the result of the sed command, you can pass sed the '-i'
+option.
+
+## Piping and redirecting output
+
+This post is already getting too long, however there's one more useful thing
+about *nix systems that I'd like to mention - the pipeline. The pipeline in
+Unix and Unix-like OSs is a chain of redirected output to the input of another
+program. Along with that, there are operators to redirect standard output to
+files (and viceversa).
+
+### Redirecting to and from files
+
+Let's suppose that we want to repeat the last example, and want to clean the
+file of comments and blank lines. We already now how to overwrite that file,
+however, what if we want to save it to another file using common Unix operators
+in bash. For that we can use the '>' and '>>' operators. For example, let's we
+want to save the result to a second file called "shortcuts_clean"
+
+```sh
+user@host:~/Documents/notes$ sed 's/\s*#.*//g; /^$/d' shortcuts > shortcuts_clean
+```
+
+Since there was no "shortcuts_clean" file, it has been created automatically.
+However, if the file had already existed, it would have overwritten it, unless
+we had used the '>>' operator, in that case, it would have appended the output
+to the already existent file.
+
+Just as there's '>' to redirect TO files, there's also the '<' to redirect from
+files to a program's standard input. However, must of the times you would just
+pass the name/path of the file to the program as an argument.
+
+### Piping
+
+Now that we know how to redirect from and to files, we can learn how to
+redirect from one program to another, with pipes. The pipe operator in *nix
+systems is the vertical bar symbol (|). Let's suppose that we want to see the
+first three files in our current directory, for that, we can pipe the output of
+ls into head, like this
+
+```sh
+user@host:~/Documents/notes$ ls | head -n 3
+asado.png
+monday.txt
+sample.txt
+```
+
+Now let's get back to our sample.txt file. Let's imagine that we first want to
+sort our lines, and we want to preserve only those lines that contain "cool" or
+"lame". Then let's suppose we want to modify to contain legit terms, and not
+some antiquated boomer slang, so we want to replace cool with dank, and lame
+with normie. Finally we want that to be output to a file instead of the screen.
+Whew! Sounds like a lot of stuff to do, but it is quite simple, and it looks
+like this
+
+```sh
+user@host:~/Documents/notes$ egrep 'cool|lame' sample.txt | sort | sed 's/cool/dank/g;s/lame/normie/g' > memes.txt
+```
+
+So if we now take a look at the file...
+
+```sh
+user@host:~/Documents/notes$ cat memes.txt
+Bowsette dank
+Despacito normie
+Harold dank
+Minions normiest
+NPC dank
+Pepe dank
+Sans dankest
+Thanos dank
+Tide Pods normie
+Uganda Knuckles dank
+```
+
+And that's basically it.
+
+## Post scriptum
+
+Before ending it for good, I want to show some other programs that might be of
+use in the Bash command line.
+
+### less
+
+This command might come in handy when there's another command that outputs a
+lot of text that overfills the terminal screen. You can pipe (as we have just
+learned) the output of that command to less, so that you can navigate with your
+arrow keys, or better yet with vim keys (hjkl). You can also search for terms
+by typing slash (/), just like with man.
+
+### tar
+
+This program is used in Linux to create and extract archives with the .tar
+format, usually also compressing them using gunzip (.gz).
+
+There usually two ways you will be using the program. One to extract files from
+a compressed archive
+
+```sh
+user@host:~/Documents/notes$ tar -xzvf oldnotes.tar.gz
+```
+
+And the other to archive and compress files
+
+```sh
+user@host:~/Documents/notes$ tar -czvf allnotes.tar.gz *
+```
+
+To learn more about the different options of this program, I recommend you
+check the man pages of tar ('man tar').
+
+### ssh and scp
+
+You may have already heard about ssh, which stands for "secure shell", even if
+you are new to Linux or Unix/Unix-like systems. This program is used to connect
+to other computers over a network (or the internet for instance), especially to
+servers.
+
+Let's suppose that you have a server with ip address 180.80.8.20 and your user
+is tux
+
+```sh
+user@host:~$ ssh tux@180.80.8.20
+```
+
+Of course, here we have assumed that the standard ssh port (22) is being used,
+otherwise you will have to specify it by passing -p followed by the port
+number.
+
+Now let's talk about scp, which stands for "secure copy". This command uses the
+same protocol as ssh, and it's used to copy files from one computer to another
+over a network. Let's suppose that you want to copy a file from your current
+computer to the server we used in the previous example
+
+```sh
+user@host:~$ scp somefile tux@180.80.8.20:/home/tux/directory/
+```
+
+If we were trying to do it the other way around, that is, from the remote
+computer to your local computer, it would look like this
+
+```sh
+user@host:~$ scp tux@180.80.8.20:/home/tux/directory/somefile directory/
+```
+
+Just as with ssh, if you are not using standard port 22, you need to say to the
+program to which port you are trying to connect, except that in the case of
+scp, the flag is '-P' instead of '-p', and goes right after "scp".
+
+Well, that's it for this tutorial/guide series, I really hope it was of use to
+you.
diff --git a/content/weblog/2019-01-11_intro-to-linux-and-bash-pt3/index.ru.md b/content/weblog/2019-01-11_intro-to-linux-and-bash-pt3/index.ru.md
new file mode 100644
index 0000000..3621a14
--- /dev/null
+++ b/content/weblog/2019-01-11_intro-to-linux-and-bash-pt3/index.ru.md
@@ -0,0 +1,542 @@
++++
+title = "Введение в Linux и Bash, часть III"
+date = 2019-01-11T08:23:00Z
++++
+
+Новый год, новый пост. В данной, третьей и последней, чатси этого туториала,
+будут несколько команд и программ которые являются стандартной частью
+большинтсва дистрибутивов Линукса. Больше всего внимания я буду выделять
+программ, которые манипулируют вывод текст других программ и файлов. Также в
+этом посте мы поговорим о регулярных выражений (коротко - regex, от английского
+Regular Expression), очень мощный инструмент для поиска текста
+
+<!-- more -->
+
+## Фильтры
+
+Программы, которые манипулируют потоки текста и возвращают его в стандартный
+вывод, часто зовут фильтрами. Скорее всего вы уже знакомы с первой командой, о
+которой я будут писать.
+
+### cat
+
+Данная команда позволяет посмотреть содержимое текстового файла (или файлов).
+Название программы ни как не связанна с котейками, а происходит от слово
+конкатенация. Самое простое применение данной программы - просмотр текстовых
+файлов. Просто пишите cat а затем путь файла. Однако, как можно предположить
+исходя из названии программы, она также позволяет соединять несколько текстовых
+файлов или потоков в один. Например
+
+```sh
+user@host:~/Documents/notes$ cat sample.txt
+Pepe cool
+Tide Pods lame
+Uganda Knuckles cool
+Thanos cool
+JPEG ok
+Despacito lame
+Bowsette cool
+Harold cool
+Sans coolest
+Minions lamest
+NPC cool
+```
+
+А теперь допустим что мы хотим соединить соджержимое данного файла с файлом
+'sample2.txt'
+
+```sh
+user@host:~/Documents/notes$ cat sample.txt sample2.txt
+Pepe cool
+Tide Pods lame
+Uganda Knuckles cool
+Thanos cool
+JPEG ok
+Despacito lame
+Bowsette cool
+Harold cool
+Sans coolest
+Minions lamest
+NPC cool
+Troll Face old
+Can haz chezburger really old
+ROFLcopter super old
+Dancing baby ancient
+```
+
+Как обычно, у данной команды есть несколько параметров, как например параметр
+-n чтобы показать номер строки
+
+```sh
+user@host:~/Documents/notes$ cat -n sample.txt
+ 1 Pepe cool
+ 2 Tide Pods lame
+ 3 Uganda Knuckles cool
+ 4 Thanos cool
+ 5 JPEG ok
+ 6 Despacito lame
+ 7 Bowsette cool
+ 8 Harold cool
+ 9 Sans coolest
+ 10 Minions lamest
+ 11 NPC cool
+```
+
+И как я уже рассказывал в предыдущей части, вы можете узнать больше о команде,
+используя программу man ('man cat').
+
+### head
+
+Данная команда довольно простая, она показывает первые n строки текстового
+файла/потока. Введите head, затем -n и количество строк вы желаете увидеть, а
+затем петь файла. Например, допустим что мы хотим увидеть первые 5 строк
+
+```sh
+user@host:~/Documents/notes$ head -n 5 sample.txt
+Pepe cool
+Tide Pods lame
+Uganda Knuckles cool
+Thanos cool
+JPEG ok
+```
+
+Если мы не передаем количетсво строк, по умолчанию она выводит первые 10 строк.
+Для просто напишите head а затем путь файла.
+
+### tail
+
+Работает также как head, но в отличие от нее, данная команда показывает
+последние строки текста. Допустим мы жотим увидеть последние три строки
+
+```sh
+user@host:~/Documents/notes$ tail -n 3 sample.txt
+Sans coolest
+Minions lamest
+NPC cool
+```
+
+Также как и с предыдущей командой, по умолчанию выводятся 10 строк.
+
+### sort
+
+С данной командой все очень просто. Она сортирует текст. Например
+
+```sh
+user@host:~/Documents/notes$ sort sample.txt
+Bowsette cool
+Despacito lame
+Harold cool
+JPEG ok
+Minions lamest
+NPC cool
+Pepe cool
+Sans coolest
+Thanos cool
+Tide Pods lame
+Uganda Knuckles cool
+```
+
+### sed
+
+sed довольная мощная утилита которая позволяет манипулировать и модифицировать
+текст. Однако, в данном туториале я не буду углубляться в подробнастях работы с
+программой, но покажу пару пару самых используемых команд.
+
+Для работы с данной программой, необходимо передать своего рода скрирта (sed
+script), который укажет программе что делать с текстом. Первый и самый простой
+способ использовать sed, это ипользовать его в качестве программы head, то
+есть, получить первые b строк текста. Например, допустим что нам необходимо
+получить первые 7 строк из файла sample.txt
+
+```sh
+user@host:~/Documents/notes$ sed '7q' sample.txt
+Pepe cool
+Tide Pods lame
+Uganda Knuckles cool
+Thanos cool
+JPEG ok
+Despacito lame
+Bowsette cool
+```
+
+А если более подробно объяснить что данная команда делает, то она говорит
+программе чтобы она вывела 7 строк, и затем вышла (q).
+
+Другой случай, и сокрее всего самое распространенное применение данной
+команды, это операция поиска и замены текста. Основная синтаксис данной
+операции выгладит следующим образцом - 's/<поиск>/<замена>/'.
+
+По умолчанию данная команда заменяет только первое совпадение в каждой строке,
+тем не менее мы может указать какие и сколько совпаднении мы хотим заменить.
+Например, если мы добавим двойку в конце команды ('s/<search>/<replace>/2')
+она заменит только вторые совпадения в каждой строке.
+
+Но а что если мы хотим заменить все совпадения во всех строках? Все очень
+просто - достаточно добавить букву g в конец. Допустим что мы хотим заменить
+все подстроки "cool" на "dank" в файле sample.txt
+
+```sh
+user@host:~/Documents/notes$ sed 's/cool/dank/g' sample.txt
+Pepe dank
+Tide Pods lame
+Uganda Knuckles dank
+Thanos dank
+JPEG ok
+Despacito lame
+Bowsette dank
+Harold dank
+Sans dankest
+Minions lamest
+NPC dank
+```
+
+Обратите внимание на то что команду которую мы передаем sed'у находится в
+кавычках. Конечно это только одни из самых основных применении.
+
+### grep
+
+Это последняя программа-фильтр, о которой я расскажу. Я покажу самое основное
+применение данной программы, а затем, после того как расскажу о регулярных
+выражений, покажу ее более интересные применения.
+
+grep - программа, которая выполняет поиск выражения переданный ей
+пользователем, и выводит на экран строки, совпадавщие с условиями поиска.
+Допустим, например, что мы хотим увидить только крутые (cool) мемы
+
+```sh
+user@host:~/Documents/notes$ grep 'cool' sample.txt
+Pepe cool
+Uganda Knuckles cool
+Thanos coolcharacter
+Bowsette cool
+Harold cool
+Sans coolest
+NPC cool
+```
+
+То, что мы передали grep'у является самым простым видом регулярного выражения.
+По факту мы указали программе чтобы нам показала строки где содержится
+подстрока "cool".
+
+## Регулярные выражения
+
+Регулярное выражение - язык поиска и манипуляции подстроками в тексте. Для
+поиска используется строка текста, часто называемая строка-образец или
+"шаблон", состоящая из символов, которые задают правило поиска. Регулярные
+выражения применяются в многих программах и языках программирования, таких как
+например редакторов текста и поисков, и естественно, может быть довольно
+полезным в терминале.
+
+### Антракт
+
+Перед тем как объяснить как работать с регулярными выражениями, хочу показать
+пару символов, облегчающие жизнь при работе с файлами в терминале -
+символы-джокеры (на англ. wildcards). В терминале они - звездочка (*) и
+вопросительный знак (?). Как раз из-за их специального применения, их нельзя
+использовать в названиях файлов.
+
+Начнем со звездочки. Звездочка озночает что на ее месте может быть любое
+количество любых символов. Например, чтобы искать файлы, чье название
+начинается с sa
+
+```sh
+user@host:~/Documents/notes$ ls sa*
+saturday.txt sample.txt sample2.txt sample.png
+```
+
+Другой пример, найти файлы чье название просто содержит подстроку sa
+
+```sh
+user@host:~/Documents/notes$ ls *sa*
+asado.png saturday.txt sample.txt sample2.txt sample.png
+```
+
+Вопросительный знак указывает на то что, на его месте должен быть символ, любой
+символ. Допустим что, мы хотим посмотреть все файлы которые называются sample,
+у которых есть трех-символьное расширение
+
+```sh
+user@host:~/Documents/notes$ ls sample.???
+sample.txt sample.png
+```
+
+Символы-джокеры очень полезные когда необходимо манипулировать несколько файлов
+с похожими названиями. Однако, если названия файлов не похоже никак, нам
+придется использовать фигурные скобки, чтобы указать список файлов. Например
+
+```sh
+user@host:~/Documents/notes$ rm {monday.txt,december1999.txt,saturday.txt}
+```
+
+### Обратно к regex
+
+А теперь я объясню основные части и символы регулярных выражении, а затем вам
+покажу как их использовать в grep
+
+* `.` - Точка означает один символ (любой символ). Например, 'be.r' найдет нам
+ bear, beer, befr, и т.д.
+* `*` - Предыдущий элемент совпадает 0 или больше раз. Например 'an*t' нам
+ найдет at, ant, annt, annnt, и т.д.
+* `+` - Предыдущий элемент совпадает один или больше раз. Например 'an+t' нам
+ найдет ant, annt, annt, и т.д.
+* `?` - Предыдущий элемент совпадает 0 или один раз. Например 'an?t' нам найдет
+ at и ant.
+* `{n}` - Предыдущий элемент совпадает ровно n раз.
+* `{min, }` - Предыдущий элемент совпадает хотя бы min раз
+* `{min, max}` - Предыдущий элемент совпадает хотя бы min раз и не больше max
+ раз.
+* `|` - Логический оператор ИЛИ. Например, 'gray|grey' нам найдет gray и grey.
+* `()` - Круглые скобки группируют несколько символов в один элемент. Например
+ 'gr(a|e)y' нам найдет gray и grey.
+* `[abc]` - Совпадает если один из символов, который внутри скобок,
+ присуствует.
+* `[^abc]` - Совпадает если нет ни одного, из которых внутри скобок.
+* `[a-d]` - Диапазон символов. То есть a, b, c и/или d.
+* `^` - Начало строки.
+* `$` - Конец строки.
+
+А теперь, ради примера с grep, допустим что, мы хотим найти все строки в
+которых содержится "cool" или "ok" в них. В этом случае нам нужна вертикальная
+черта "|". Однако, если использовать grep без параметров, нам нужно будет
+напечатать слеш перед ней "\|". Поэтому лучше использовать команду egrep,
+которая является сокращением "grep -E", затем чтобы включить расширенные
+регулярные выражения. Например
+
+```sh
+user@host:~/Documents/notes$ egrep 'cool|ok' sample.txt
+Pepe cool
+Uganda Knuckles cool
+Thanos cool
+JPEG ok
+Bowsette cool
+Harold cool
+Sans coolest
+NPC cool
+```
+
+А теперь допустим что, нам неоюходимо найти все строки с буквой 't' в качестве
+последнего символа в строке
+
+```sh
+user@host:~/Documents/notes$ egrep 't$' sample.txt
+Sans coolest
+Minions lamest
+```
+
+Я уже продемонстрировал применение regex'ов с grep'ом (и/или egrep). А теперь я
+хочу вам показать более практический пример с sed'ом. Да, в sed'e помимо
+собственного языка, можно еще и регулярные выражения использовать.
+
+Допустим у нас есть файл который выглядит следующим образом
+
+```sh
+user@host:~/Documents/notes$ cat shortcuts
+# Some shortcuts
+
+d ~/Documents
+D ~/Downloads
+m ~/Music
+pp ~/Pictures
+vv ~/Videos
+
+
+s ~/.scripts # My scripts
+cf ~/.config # My configs
+```
+
+Как вы могли заметить, в нашем файле много пустых строк, и много комментарри,
+которые полезные для человека, но бесполезные для компьютера. Начнем с того
+что, удалим комментарии, для этого мы будем использовать команду sed'а для
+поиска и замены, только в нашем случае мы хотим заменить комментарии на
+пустоту. Затем нам понадобится вставить регулярное выражение, которое поможет
+нам найти комментарии - '#.*'. Этот шаблон переводится как - найди символ '#' и
+все что после него находится. А теперь давайте мы все совместим
+
+```sh
+user@host:~/Documents/notes$ sed 's/#.*//g' shortcuts
+
+
+d ~/Documents
+D ~/Downloads
+m ~/Music
+pp ~/Pictures
+vv ~/Videos
+
+
+s ~/.scripts
+cf ~/.config
+```
+
+Вот и мы избавились от комментариев. Тем не менее нам еще осталось избавится от
+пустых строк, и если вы заметили, комментарии были удалены, но перед некоторыми
+из них остались пробелы.
+
+И так, давайте мы сначала улучшим нашу команду, для этого нам нужно найти там
+где есть 0 или больще пробелов перед комментарии, но как нам обозначит пробелы
+в sed'е? Все очень просто с '\s', таким образом наша команда а теперь выглядет
+вот так 's/\s*#.*//g'.
+
+А теперь нам нужно избавится от пустых строк. ДЛя этого нам нужно ввести еще
+одну команду sed'а, но мы можем ее ввести вместе с предыдущей командой
+отделяя их точкой и запетой (;). Нам осталось придумать как найти пустую
+строку, все очень просто - '^$', то есть, найти ту строку где ее начало и ее
+конец находятся вместе. Нам осталось указать sed чтобы он удалил эту строку, и
+вот я вам расскажу еще одну команду sed - команду для удаления (d), и так...
+
+```sh
+user@host:~/Documents/notes$ sed 's/\s*#.*//g; /^$/d' shortcuts
+d ~/Documents
+D ~/Downloads
+m ~/Music
+pp ~/Pictures
+vv ~/Videos
+s ~/.scripts
+cf ~/.config
+```
+
+Конечно, когда мы вводим эту команду, она не переписывает файл, она всего лишь
+выводит на экран результат операции. Если вам необходимо переписать исходный
+файл вы можете передать sed параметр '-i.
+
+### Конвейер и перенаправление
+
+Последнее о чем я буду писать в этом туториале не команда и не программа, но
+очень полезная вещь, которая есть во всех Unix и Unix-подобных системах -
+конвейер. Конвейер - это цепочка перенаправляющая вывод процесса или процессов
+в стандартный ввод других проццессов. В том числе есть и операторы которые
+позволяют перенаправлять вывод программы в файл и наоборот.
+
+### Перенаправление в и из файлов
+
+Допустим что мы хотим повторить последний пример, но в этот раз мы хотим
+записать результат в файл. Мы уже знаем как переписать оригинальный файл, но в
+этот раз мы хотим записать результат в новый файл. Для этого нам понадобяться
+операторы перенаправления Unix'а '>' и '>>'. Первый оператор переписывает файл,
+если указанный файл уже существует, а второй просто добавляет поток текста в
+конец файла. В этом случае безралично какой из этих операторов использовать,
+ибо мы хотим записать поток текста на новый файл
+
+```sh
+user@host:~/Documents/notes$ sed 's/\s*#.*//g; /^$/d' shortcuts > shortcuts_clean
+```
+
+Так же существует оператор перенаправления ИЗ файла в стандартный ввод
+программы - '<'. Однако, в большинство случаях достаточно передать путь файла в
+качестве аргумента.
+
+### Конвейер
+
+А теперь я вам покажу как перенаправить вывод одной программы в другую. Чтобы
+перенаправить вывод из одной программы в другую, достаточно ввести команду
+первой программы, затем ввести вертикальную черту (|), а затем команду второй
+программы. Допустим мы хотим увидит первые три файлы в нашей директории, для
+этого мы можем направить вывод ls в head, вот так
+
+```sh
+user@host:~/Documents/notes$ ls | head -n 3
+asado.png
+monday.txt
+sample.txt
+```
+
+А теперь вернемся к файлу sample.txt. Допустим что сначала мы хотим
+отсортировать все строки файла, и хотим оставить только те строки с "cool" и
+"lame". Затем, мы хотим заменить неправильные старые термины, на современные,
+то есть "cool" на "dank" и "lame" на "normie". И наконец, мы хотим это
+сохранить в файл. И вот как все это выгладит
+
+```sh
+user@host:~/Documents/notes$ egrep 'cool|lame' sample.txt | sort | sed 's/cool/dank/g;s/lame/normie/g' > memes.txt
+```
+
+Взглядываем в файл и...
+
+```sh
+user@host:~/Documents/notes$ cat memes.txt
+Bowsette dank
+Despacito normie
+Harold dank
+Minions normiest
+NPC dank
+Pepe dank
+Sans dankest
+Thanos dank
+Tide Pods normie
+Uganda Knuckles dank
+```
+
+И вот и все
+
+## Post scriptum
+
+Перед тем как закончить с этим туториалом, я хочу вам показать еще несколько
+программ, которые могут вам пригодится
+
+### less
+
+Данная команда вам пригодится когда, например, некоторая программа выводит
+слишком много текста. В таком случае, достаточно перенаправить вывод текста
+первой программы к less используя оператор конвейера (|), также как я вам
+только что показал. Используя эту команду вы можете листать текст используя
+стрелочки или vim-клавиши (hjkl). Так же вы можете искать термины введя слеш
+(/).
+
+### tar
+
+Это de facto официальная программа Линукса для архивирования и сжатия файлов в
+формате .tar. Чаще всего используется формат сжатия gunzip (.gz), но есть и
+другие форматы сжатия.
+
+Есть два основных способов использовать эту программу. Первый чтобы
+разархивировать
+
+```sh
+user@host:~/Documents/notes$ tar -xzvf oldnotes.tar.gz
+```
+
+Второй чтобы архивировать и сжать
+
+```sh
+user@host:~/Documents/notes$ tar -czvf allnotes.tar.gz *
+```
+
+Как обычно, вы можете узнать больше об этой программе используя утилиту man
+('man tar').
+
+### ssh and scp
+
+Скорее всего вы уже слышали или даже работали с ssh, даже если вы не работали
+до этого с Ликусом или Unix/Unix-подобных системах. Это программа позволяет
+подключится через терминал к другим компьютером через сеть (например,
+интернет), в том числе и к серверам.
+
+Допустим вы хотите подключится к серверу с ip 180.80.8.20 и пользователь tux
+
+```sh
+user@host:~$ ssh tux@180.80.8.20
+```
+
+Здесь мы предпологаем что мы подключаемся через стандартный порт ssh (22), иначе
+придется его передать используя параметр -p а затем номер порта.
+
+А теперь поговорим о scp. Это программа позволяет передать файлы из одного
+компьютера в другого используя протокол ssh'а. Допустим что мы хотим отправить
+файл с локального компьютер в тот же самый сервер, что и в прошлом примере
+
+```sh
+user@host:~$ scp somefile tux@180.80.8.20:/home/tux/directory/
+```
+
+Если бы мы хотели наоборот, получить, а не отправить, то мы просто поменяли
+порядок аргументов, примерно вот так
+
+```sh
+user@host:~$ scp tux@180.80.8.20:/home/tux/directory/somefile directory/
+```
+
+Так же как и с ssh порт по умолчанию 22, то есть если использовать другой порт,
+нужно его указать, только в отличие от ssh, параметр -P вместо -p и нужно его
+ввести сразу после "scp.
+
+
+Вот и на этом все. Надеюсь вам этот туториал помог. С наступившим 2019.