# Linux
###### tags: `Linux`

Reference: https://ryanstutorials.net/linuxtutorial/
[TOC]
# Installation
The default package manager for:
* Centos is **yum**
* Debian/Ubuntu/Mint/Kali is **apt-get**
**Reference:** https://www.stechies.com/aptget-command-not-foundin-linux/
# pwd Command: Where are we?
We can check which folder we are in by using **pwd**
```linux===
root@zoo2:/apache-zookeeper-3.6.2-bin/bin# pwd
# Respond:
/apache-zookeeper-3.6.2-bin/bin
```
# ls Command: What do we have?
If we use **ls**, it will show plain listing of our current location.
```linux===
root@bedbe2be73fe:/# ls
# Respond:
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
```
## ls [options] [location]
### -l option
In the above example, the square brackets ( [ ] ) mean that those items are optional, we may run the command with or without them.
By adding command line option **-l**, we indicated it should show a long listing.
**Example:**
```linux===
root@bedbe2be73fe:/# ls -l
# Respond:
total 48
lrwxrwxrwx 1 root root 7 Apr 16 05:11 bin -> usr/bin
drwxr-xr-x 2 root root 4096 Apr 15 2020 boot
drwxr-xr-x 5 root root 360 Apr 27 06:00 dev
drwxr-xr-x 1 root root 4096 Apr 27 06:00 etc
.
.
.
```
### Let's check each section of ls -l response!
* First character indicates whether it is a **normal file (-)** or **directory (d)**
* Next 9 characters are permissions for the file or directory
* The next field is the number of blocks
* The next field is the owner of the file or directory
* The next field is the group the file or directory belongs to
* Following this is the file size
* Next up is the file modification time.
* Finally we have the actual name of the file or directory.
> **drwxr-xr-x 2 root root 4096 Apr 15 2020 boot**
> It is a directory with permission rwxr-xr-x, that takes 2 blocks. Both owner and group belongs to root. This directory has file size of 4096 and was last modified in 2020 April 15. This directory is called boot. [color=#076080]
### ls [location]
When we ran ls with a command line argument (Example: /var ). When we do this it tells ls not to list our current directory but instead to list that directories contents.
**Example:**
```linux===
root@bedbe2be73fe:/# ls /var
# Respond:
backups cache lib local lock log mail opt run spool tmp
```
### ls -l [location]
We ran ls with both a command line option and argument. As such it did a long listing of the directory /var.
**Example:**
```linux===
root@bedbe2be73fe:/# ls -l /var
# Respond:
total 36
drwxr-xr-x 2 root root 4096 Apr 15 2020 backups
drwxr-xr-x 5 root root 4096 Apr 16 05:33 cache
drwxr-xr-x 1 root root 4096 Apr 16 05:11 lib
drwxrwsr-x 2 root staff 4096 Apr 15 2020 local
lrwxrwxrwx 1 root root 9 Apr 16 05:11 lock -> /run/lock
drwxr-xr-x 3 root root 4096 Apr 16 05:11 log
drwxrwsr-x 2 root mail 4096 Apr 16 05:11 mail
drwxr-xr-x 2 root root 4096 Apr 16 05:11 opt
lrwxrwxrwx 1 root root 4 Apr 16 05:11 run -> /run
drwxr-xr-x 2 root root 4096 Apr 16 05:11 spool
drwxrwxrwt 2 root root 4096 Apr 16 05:32 tmp
```
# Absolute and Relative Path
Before starting with the **2 types of path**, we have to understand **the linux file system hierarchical structure**.
## Linux File System Hierarchical Structure
The very top of structure, is what we called **root** directory. It is denoted by a slash(**/**). This one like any other directory, it will have subdirectories and so on.
## Absolute Path
This one will specify a location(file or directory) **in relation to the root directory**. This one will always **start with a forward slash(/)**.
## Relative Path
Specify a location (file or directory) **in relation to where we currently are in the system**. They **will NOT begin with a slash**.
## Absolute/Relative Path using ls
We can either use **ls /absolutePath** or **ls relativePath**, but how we use it will differ.
Let's take below example a look:
>As we can see, we ran **ls alternatives** (Relative Path) in root directory at first, but **error: No such file or directory** was shown.
>[color=orange]
>However, if we move inside the **etc folder** by usind **cd folderName**, and retry the command, it works. Here we understand that relative path only works when you are in the right folder/location. Otherwise, you will not be able to run it.
>
>On the other hand, absolute path works anywhere, but if the location is deep inside, it takes too long to write the whole path.
**Example:**

## More about Paths
### ~(tilde) - ?? Directory
### .(dot) - Current Directory
. checks current directory
**Example:**
```linux===
root@bedbe2be73fe:/etc# ls ./alternatives
# Respond:
README awk nawk pager rmt w
```
### ..(dotdot) - Parent Directory
.. Refers to parent, so when doing **ls ../../** means checking parent parent directory and list up.
**Example:**
```linux===
root@bedbe2be73fe:/etc/alternatives# ls ../../
# Respond:
bin boot dev etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
```

# Tools
## TAB completion
By using Tab, it can auto-complete names.
```linux===
[root@237409c00f01 /]# ls
anaconda-post.log bin dev etc home lib lib64 logs media mnt nohup.out opt proc root run sbin scripts srv sys tmp usr var
[root@237409c00f01 /]# vim an (Before Press Tab)
[root@237409c00f01 /]# vim anaconda-post.log (After)
```
## Quote
```linux===
helloroot@bedbe2be73fe:/# cd 'home'
root@bedbe2be73fe:/home# pwd
/home
root@bedbe2be73fe:/home#
```
## man <command to look up>
### What is it for?
The manual(man) pages are a set of pages that explain every command available on your system including what they do, the specifics of how you run them and what command line arguments they accept.
In order to access these manual pages, we use the so call **man** command.
**Note:**
>To exit the man pages press 'q' for quit. [color=red]
**Example:**
```linux===
root@bedbe2be73fe:/# man ls
# Respond:
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
.
.
.
```
### All **man commands**
>**man <command>**
// Look up the manual page for a particular command.[color=orange]
>**man -k <search term>**
Do a keyword search for all manual pages containing the given search term.[color=lightgreen]
### Install 'man' command if not found
```linux===
root@bedbe2be73fe:/# apt-get update
root@bedbe2be73fe:/# apt-get install man-db
```
# File
## Types of files
1. file.exe - an executable file, or program.
1. file.txt - a plain text file.
1. file.png, file.gif, file.jpg - an image.
## File Creation
Reference: https://linuxize.com/post/create-a-file-in-linux/
### **touch** Command
```linux===
root@bedbe2be73fe:/home# touch file1.txt file2.txt
root@bedbe2be73fe:/home# ls
file1.txt file2.txt
```
### **cat > file**
```linux===
root@bedbe2be73fe:/home# cat > file3.txt
hello (Pressed CTRL + D to save, CTRL + C to exit)
```
### Error: "bash: file: command not found"
```linux===
root@bedbe2be73fe:/# apt-get update
root@bedbe2be73fe:/# apt-get install file -y
```
## Remove file
Use **rm** to remove files.
```linux===
root@bedbe2be73fe:/home# ls
file2.txt file3.txt filedir
root@bedbe2be73fe:/home# rm file2.txt
root@bedbe2be73fe:/home# ls
file3.txt filedir
```
# Directory
## Create Directory
Use **mkdir** to create directory.
```linux===
root@bedbe2be73fe:/home# ls
file3.txt filedir
root@bedbe2be73fe:/home# mkdir test
root@bedbe2be73fe:/home# ls
file3.txt filedir test
```
## Remove Directory
Use **rmdir** to remove directory.
```linux===
root@bedbe2be73fe:/home# ls
file3.txt filedir test
root@bedbe2be73fe:/home# rmdir test
root@bedbe2be73fe:/home# ls
file3.txt filedir
```
# Copying/Moving/Renaming File/Directory
> No matter what if it is a file/directory, we will use the same command[color=pink]
## cp - copy files
**Command:**
```linux===
cp [options] <source> <destination>
```
### Copying Command Example
```linux===
root@bedbe2be73fe:/home/filedir# ls
file3.txt test2
root@bedbe2be73fe:/home/filedir# cp -r test2 test3
root@bedbe2be73fe:/home/filedir# ls
file3.txt test2 test3
```
## mv - move (rename) files
```linux===
mv [options] <source> <destination>
```
### Moving Command Example
**Example file:**
```linux=
root@bedbe2be73fe:/# ls home
file3.txt filedir test
root@bedbe2be73fe:/# mv home/file3.txt home/filedir/file3.txt
root@bedbe2be73fe:/# ls home
filedir test
root@bedbe2be73fe:/# ls home/filedir
file3.txt
```
**Example directory:**
```linux===
root@bedbe2be73fe:/# ls home
filedir test
home/test:
root@bedbe2be73fe:/# mv home/test home/filedir/test
root@bedbe2be73fe:/# ls home
filedir
```
### Renaming Command Example
You are not dreaming! Indeed, the renaming way is using the **mv** command used for movement commands. In this case we just move to same place but with different name.
```linux===
root@bedbe2be73fe:/home/filedir# ls
file3.txt test
root@bedbe2be73fe:/home/filedir# mv test test2
root@bedbe2be73fe:/home/filedir# ls
file3.txt test2
```
# vi/vim command
**vi/vim** allows us to edit files.
## vi vs vim
Vi stands for Visual. It is a text editor that is an early attempt to a visual text editor.
Vim stands for Vi IMproved. It is an implementation of the Vi standard with many additions. It is the most commonly used implementation of the standard. Most Linux distributions come with Vim already installed.
## Command Example
```linux===
root@bedbe2be73fe:/home/filedir# vi file3.txt
```
## Navigating a file in Vi/Vim
* **Arrow keys** - move the cursor around
* **j, k, h, l** - move the cursor down, up, left and right (similar to the arrow keys)
* **^ (caret)** - move cursor to beginning of current line
* **$** - move cursor to end of the current line
* **nG** - move to the nth line (eg 5G moves to 5th line)
* **G** - move to the last line
* **w** - move to the beginning of the next word
* **nw** - move forward n word (eg 2w moves two words forwards)
* **b** - move to the beginning of the previous word
* **nb** - move back n word
* **{** - move backward one paragraph
* **}** - move forward one paragraph
**:set nu**
>If you type **:set nu** in edit mode within vi it will enable line numbers. I find that turning line numbers on makes working with files a lot easier.[color=red]
## Insert Mode
Click **i** to enter insert mode, and exit insert mode by pressing **ESC**.
By using this mode, you can edit, insert, delete.
### How to check you are in insert mode?

## Deleting content
Of course you can do it in **insert** mode.
* **x** - delete a single character
* **nx** - delete n characters (eg 5x deletes five characters)
* **dd** - delete the current line
* **dn** - d followed by a movement command. Delete to where the movement command would have taken you. (eg d5w means delete 5 words)
## Undoing Changes
Undoing changes in vi is fairly easy. It is the character u.
* **u** - Undo the last action (you may keep pressing u to keep undoing)
* **U (Note: capital)** - Undo all changes to the current line
## Saving and Exiting
* **ZZ** (Note: capitals) - Save and exit
* **:q!** - discard all changes, since the last save, and exit
* **:w** - save file but don't exit
* **:wq** - again, save and exit
## Install vi command if not present!
```linux===
//Ubuntu
root@bedbe2be73fe:/home/filedir# apt-get install vim -y
//Centos
[root@237409c00f01 logs]# yum install vim
```
# View files
## **cat** command for small files
**cat** which actually stands for concatenate, is used to view files.
**Command:**
```linux===
cat <file>
```
```linux===
root@bedbe2be73fe:/home/filedir# cat file3.txt
hello
I am testing vi command
press i to change to insert mode
root@bedbe2be73fe:/home/filedir#
```
## **less** command for large files
For larger files there is a better suited command which is less.
**Command:**
```linux===
less <file>
```
>less allows you to move up and down within a file using the arrow keys.
You may go **forward a whole page using the SpaceBar or back a page by pressing b**. When you are done you can **press q for quit**. [color=orange]
```linux===
[root@237409c00f01 logs]# less info-2021-04-28.0.log
```
### How to quit less?
Quit by pressing **q**
# Permissions
## View Permissions
### Introduction
By using **ls -l**, we can check the permission.
**Where exactly is the permission stated?**
Just after the first character (- or d).
Let's take below as example:
> -rw-rw-r-- 1 root root 65 Apr 28 02:54 file3.txt [color=#B772FF]
Deducting the first character and the things after space, we get **rw-rw-r--**. This is the permission set for **user, ground and others** (stated in this same order).
### Permission Types and Permission owner
Taking last permission as example **rw-rw-r--**, we first have to understand the types of permission, someone can have.
**We mainly have ~*3 types of permission*~**
>* **r** read - you may view the contents of the file.
>* **w** write - you may change the contents of the file.
>* **x** execute - you may execute or run the file if it is a program or script.[color=#FFBBE5]
**As for who owns this permission, we have ~*3 types of people*~**
>* **owner** - a single person who owns the file. (typically the person who created the file but ownership may be granted to some one else by certain users)
>* **group** - every file belongs to a single group.
>* **others** - everyone else who is not in the group or the owner.[color=#BBF4FF]

Note:
If I do not state who do I provide permission to, then this permission will be given to all individuals. Ex: chmod +x 1.txt
However, this is not actually recommended. :-1:
**Who owns each permission?**
We have to separate the permission in 3 parts, containing 3 characters.
**Example:**
rw-rw-r-- into
1. rw- > User Permission contain read and write.
1. rw- > Group Permission contain read and write.
1. r-- > Other Permission only can read.
**Note:**
**\- (dash)** means absence of the permission.
The permission are stated in read, write, execute order (rwx), if all permissions are available **rwx** will be seen.
**Image Explanation:**

### View Permission Example
**Check one file permission:**
```linux===
root@bedbe2be73fe:/home/filedir# ls -l file3.txt
-r--r--r-- 1 root root 65 Apr 28 02:54 file3.txt
```
**Check all files permission in same directory:**
```linux===
root@bedbe2be73fe:/home/filedir# ls -l
total 16
-r--r--r-- 1 root root 65 Apr 28 02:54 file3.txt
drwxr-xr-x 2 root root 4096 Apr 28 02:20 test2
drwxr-xr-x 3 root root 4096 Apr 28 02:43 test3
drwxr-xr-x 2 root root 4096 Apr 28 02:20 test4
```
## Change Permissions
To change permission we use **
** command.
**Command:**
```linux===
chmod [permissions] [path]
```
### How to use this command?
First of all you have to be able to state:
1. To which people you are changing the permission
1. Either add or revoke permission
1. Which permissions you want to add/revoke
**To whom I give permission?**
I can give permission to one or more people at once.
chmod **o**-w file3.txt
chmod **gu**+rwx file3.txt
chmod **ugo**+rwx file3.txt
As you are guessing, **u (user)**, **g(group)**, **o(other)**. We state our people before +/- and state permissions after +/i.
**Add or revoke permission?**
We use **+(plus)** / **-(minus)** to state **add** and **revoke** permission, by writing in between our people and permission.
chmod **u**-x file3.txt = Remove execution permission to user
chmod **o**+rw file3.txt = Add read/write permission to others
**Where do we state our to be changed permissions?**
Just right after +(add)/-(revoke) in our command. Just as **View Permission** section stated. We can refer to our permissions by using **r(read), w(write), x(execute)**.
We can make one or more permission changes at same time.
### **Example**
```linux===
root@bedbe2be73fe:/home/filedir# ls -l file3.txt
-rwxrw-r-- 1 root root 65 Apr 28 02:54 file3.txt
root@bedbe2be73fe:/home/filedir# chmod o+w file3.txt
root@bedbe2be73fe:/home/filedir# ls -l file3.txt
-rwxrw-rw- 1 root root 65 Apr 28 02:54 file3.txt
```
<details>
<summary>Example Explanation</summary>
>**rwxrw-r--**
>User has permission to read, write and execute.
>Group has permission to read and write.
>Other only can read.[color=#FFD0BB]
</details>
# WildCards
## Basic Wildcards
* ***** - represents zero or more characters
* **?** - represents a single character
* **[]** - represents a range of characters
## Reference
https://ryanstutorials.net/linuxtutorial/wildcards.php
# Filter
https://ryanstutorials.net/linuxtutorial/filters.php
# Grep and Regular Expressions
## egrep [command line options] <pattern> [path]
Search a given set of data and print each line containing given pattern.

## Regular Expression
This is used to write patterns we would like to filter.
Basic blocks for regular expression:
* **. (dot)** - a single character.
* **?** - the preceding character matches 0 or 1 times only.
* ***** - the preceding character matches 0 or more times.
* **+** - the preceding character matches 1 or more times.
* **{n}** - the preceding character matches exactly n times.
* **{n,m}** - the preceding character matches at least n times and not more than m times.
* **[agd]** - the character is one of those included within the square brackets.
* **[^agd]** - the character is not one of those included within the square brackets.
* **[c-f]** - the dash within the square brackets operates as a range. In this case it means either the letters c, d, e or f.
* **()** - allows us to group several characters to behave as one.
* **|** (pipe symbol) - the logical OR operation.
* **^** - matches the beginning of the line.
* **$** - matches the end of the line.
**Example:**
```linux===
root@bedbe2be73fe:/home/filedir# egrep '^[a-zA-Z0-9]+[a-zA-Z0-9._-]+@+[a-zA-Z0-9]+.+[a-z]' file2.txt
```

# htop Command
```linux===
[root@a8a00be5fa4c /]# yum -y install epel-release
[root@a8a00be5fa4c /]# htop
```
> Note: F10 to quit [color=red]
# Cheet Sheet
```linux
mkdir
Make Directory - ie. Create a directory.
rmdir
Remove Directory - ie. Delete a directory.
touch
Create a blank file.
cp
Copy - ie. Copy a file or directory.
mv
Move - ie. Move a file or directory (can also be used to rename).
rm
Remove - ie. Delete a file.
chmod
Change permissions on a file or directory.
ls -ld
View the permissions for a specific directory.
```
# Note
> **No undo**
>The Linux command line does not have an undo feature. Perform destructive actions carefully. [color = red]