Commit 0982110f authored by Philip Linghammar's avatar Philip Linghammar
Browse files

Updates basics_of_linux.md

Auto commit by GitBook Editor
parent 34b8a004
Loading
Loading
Loading
Loading
+24 −36
Original line number Diff line number Diff line
@@ -6,6 +6,8 @@ This is a huge chapter. I could divide it up in many subchapters but I like to h

The shell, or the terminal is a really useful tool. Bash is the standard shell on most Linux distros.

One really useful trick when working with bash is to search for old commands that you have used. You can access this search function by doing `ctr-r` in the terminal. 

### Navigating

`pwd` - Print working directory
@@ -32,7 +34,6 @@ Use `/searchterm` to search. It is the same command as in vim. `n` to scroll to

`more` - Output file but just little bit at a time. `less` is better.


### Working with files

`touch` - Create a new file.
@@ -57,7 +58,6 @@ Watch the command destroy an entire machine: [https://www.youtube.com/watch?v=D4

`rmdir` - Remove empty directory


### A little bit of everything

`history` - Show commands history
@@ -108,21 +108,22 @@ which bash
# Usually outputs: /bin/bash
```


## 2. Editing text

First let's just clear out something about **standard streams**, or **I/O**-streams. Standard streams are the streams that are used to interact between the human computer-user and the machine. There are three standard streams: standard input (stdin), standard output (stdout), and standard error (stderr).The stdin stream can be seen as an abstractions of the real keyboard input. So when you issue a command/program that requires input the program does not read straight from the keyboard input, instead it reads from the file STDIN.
First let's just clear out something about **standard streams**, or **I/O**-streams. Standard streams are the streams that are used to interact between the human computer-user and the machine. There are three standard streams: standard input \(stdin\), standard output \(stdout\), and standard error \(stderr\).The stdin stream can be seen as an abstractions of the real keyboard input. So when you issue a command/program that requires input the program does not read straight from the keyboard input, instead it reads from the file STDIN.

### Stdin

Stdin is the data that gets inputed into the program. An example of a program that requires stdin data is `cp`. In order for the program to do anything it needs input data. For example `cp file1 copy_of_file1`. Here `file1` and `copy_of_file1` is the stdin.

So the default Stdin comes from the STDIN-file that is a text-file representation of the keyboard input. But often times we do not want to input stuff from the keyboard, sometimes we want to input something into a program that comes from another file. That is when we can use redirection symbol: `>`.

So an example could be `cat < my_text_file.txt`. The data from my_text_file.txt will now be used as input instead of the keyboard input. 
So an example could be `cat < my_text_file.txt`. The data from my\_text\_file.txt will now be used as input instead of the keyboard input.

The file descriptor for **stdin** is: **0**

### Stdout

Stdout is the data that get ouputed from the program.  
For example, when you use the command `cat file1` that data/text that gets outputed is the stdout The same with the program `ls`. Not all programs have stdout. For example when you use `mv` or `cp` successfully you get no stdout back from the program.

@@ -146,6 +147,7 @@ This will take the stdout from `ls -la` and forward/redirect it into the `less`
The file descriptor for **stdout** is: **1**

### Stderr

Stderr is the stream used for outputting error messages. So if a program fails for whatever reason. For example, if we try to copy a file that does not exist, this will be the stdrr output:

```
@@ -193,11 +195,8 @@ cat filename | sort -u > newFileName

`sed`



### Editing text


#### sed

Can perform basic editing on streams, that is to say, text.
@@ -208,9 +207,6 @@ Remove first line of file/stream
sed "1d"
```




#### cut

Cut by column
@@ -229,8 +225,6 @@ cut -d" " -f4

`-d` stands for delimiter. and `-f` for field.



#### tr - Translate

Transform all letter into capital letters
@@ -253,7 +247,6 @@ cat file.txt | tr "." "_"

[http://www.thegeekstuff.com/2012/12/linux-tr-command/](http://www.thegeekstuff.com/2012/12/linux-tr-command/)


#### awk

So awk is an advanced tool for editing text-files. It is its own programming language to it can become quite complex. Awk iterates over the whole file line by line.
@@ -294,7 +287,6 @@ We can use the -F flag to add a custom delimiter.
awk -F ':' '{print $1}' test.txt
```


So if you are manipulating some text you might want to start the output with some info about the columns or something like that. To do that we can use the BEGIN-keyword.

```
@@ -304,7 +296,6 @@ awk 'BEGIN{printf "IP-address \tPort\n"} /nop/ {print $3} END {printf "End of th

Here we are printing IP-address    PORT to the first line of the file.


## 3. User management

To add a user we do:
@@ -367,15 +358,15 @@ The next three letters are for read, `w` for write and `x` for execute. The firs

To display information regarding the systems processes you can use the `ps` command.


```
ps -aux
```

`-a` stands for all  
`-u` stands for all processes by all users  
`-x` stands for all processes that don't run a `tty`

If you run this command you will probably see a pretty big output. In the column for **command** you will see what command has been run. Every process has a Process Identification Number (**PID**). Something you will also see in the output.
If you run this command you will probably see a pretty big output. In the column for **command** you will see what command has been run. Every process has a Process Identification Number \(**PID**\). Something you will also see in the output.

All of theses processes can actually be found in `/proc`. You just go to `/proc/[pid]`. In `/proc` you can find information about the system, and you can actually change the system if you change those files! But more on that later. What I wanted to explain is that if we look at the output from `ps` we see that some commands are in brackets. Like this:

@@ -388,7 +379,6 @@ root 13 0.0 0.0 0 0 ? S ene14 0:00 [ksoftirqd/1]

Those are usually kernel processes, and you can safely assume that no user has started them.


If you want to monitor processes in real time you can use `top` or `htop`. `top` comes preinstalled on most distros. But `htop` is really a lot nicer.

For `htop` the F1-10 keys might trigger OS-events. So you can use the shortcuts instead.
@@ -455,7 +445,6 @@ When you remove some package it might have requires some other dependencies. To
sudo apt-get autoremove
```


### Organizing your $path variable

I am talking about debian/ubuntu here. On other systems I don't know.
@@ -466,7 +455,7 @@ You can define your path in `/etc/environment`. If you don't have it you can cre
source /etc/environment && export PATH
```

If you are using zsh (which you should) you have to add it here
If you are using zsh \(which you should\) you have to add it here

```
sudo vim /etc/zsh/zshenv
@@ -486,7 +475,6 @@ This is a non-persistent way to add binaries to your path. Might be useful if yo
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
```


### Installing custom packages

If you download a package that is not in the official repository you can put the binary in `/opt`. That is good place to put your binaries.
@@ -670,7 +658,7 @@ sudo apt-get update && sudo apt-get dist-upgrade
sudo apt-get update && sudo apt-get upgrade
```

If you are using a distro that is Long Term Supported (LTS). You will not get the latest Kernel version, but you will get the latest Long Term Supported version.
If you are using a distro that is Long Term Supported \(LTS\). You will not get the latest Kernel version, but you will get the latest Long Term Supported version.

## 14. Logging