Linux
Adding a user
SED
AWK
Archive files
Extend disk
Extend disk LVM
Disk space
VMs & Containers
Docker
Kubernetes
Podman
Vim
Closing
Changing the color scheme
Show line numbers
Run commands
Bash
Loops
Conditional commands
Variables
Options
Redirection
SQL
MySQL
Python
Dictionary
Screen
Create new session
tmux
tmux sessions
Tools
Git
Linux
Adding a user
Just the user
useradd username
User with homedir
useradd -m username
SED
RegEx on stdout
cat file | sed -e 's/\(o\)/-\1-/g'
L-o-rem ipsum d-o-l-o-r sit amet, c-o-nsetetur sadipscing elitr, sed diam
RegEx in file
sed -i -e 's/\(o\)/-\1-/g' file
cat file L-o-rem ipsum d-o-l-o-r sit amet, c-o-nsetetur sadipscing elitr, sed diam
AWK
Order data
cat file | awk '{ print $2 " " $1 " " $4 " " $3 " " $6 " " $5 }'
Field2 Field1 Field4 Field3 Field6 Field5
Custom field seperator
Content seperated by ';' cat file | awk -F ';' '{ print $2 " " $1 " " $4 " " $3 " " $6 " " $5}'
Field2 Field1 Field4 Field3 Field6 Field5
Adding inject variables
var1="$(hostname -f)" cat file | awk -v hostname="$var1" '{ print hostname " column1: " $1}'
vimdiesel.de column1: Field1
Add a Header and footer
cat file | awk 'BEGIN { print "Header" } { print $1} END { print "Footer" }'
Header Field1 Footer
Archive files
Create archive (tar gzip)
tar zcvf file.tar.gz file
ls file file.tar.gz
Extract whole archive (tar gzip)
tar zxf file.tar.gz
ls file file.tar.gz
Extract single file from archive (tar gzip)
tar xzf files.tar.gz file3
ls file3 files.tar.gz
Show archive content (tar gzip)
tar ztf file.tar.gz
file
Extend disk
Get disks
fdisk -l
Extend partition
fdisk /dev/sda d n p p w
Resize filesystem
resize2fs /dev/sda
Extend disk LVM
Get newly added disk
fdisk -l
Add make new pv from disk
pvcreate /dev/sdb
Extend Volume Group
vgextend my_cool_vg /dev/sdb
Extrend logical volume
lvextend -l +100%FREE /dev/my_cool_vg/root
Resize Filesystem
resize2fs /dev/my_cool_vg/root
Disk space
Normal
df
Filesystem 1K-blocks Used Available Use% Mounted on overlay 1055762868 9754812 992304584 1% / tmpfs 65536 0 65536 0% /dev tmpfs 8166136 0 8166136 0% /sys/fs/cgroup shm 65536 0 65536 0% /dev/shm /dev/sde 1055762868 9754812 992304584 1% /etc/hosts tmpfs 8166136 0 8166136 0% /proc/acpi tmpfs 8166136 0 8166136 0% /sys/firmware
Human-readable flag
df -h
Kilo Mega and Gigabyte instead of bytes
Inode flag
df -i
Inodes instead of space
Show largest files/folders
du -xh / | sort -rh | head -n 5
135M . 111M ./usr 56M ./usr/share 39M ./usr/lib 36M ./usr/lib/x86_64-linux-gnu
VMs & Containers
Docker
Start a container
docker run -e 'MYSQL_ALLOW_EMPTY_PASSWORD=true' -d mysql:latest
75470e66ae05235b1724f99d2eba4d453d0d94b59d3661dfb0f47e216da95a25 docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 75470e66ae05 mysql:latest "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 3306/tcp, 33060/tcp beautiful_joliot9
Get shell into a container
docker exec -it 75470e66ae05 bash
bash-4.4#
Dockerfile
cat << "EOF" > Dockerfile FROM debian:latest ENV MY_VARIABLE="Hello" WORKDIR /work COPY . /work RUN /bin/bash EOF docker build . -t my_image
=> [internal] load build definition from Dockerfile => => transferring dockerfile: 127B => [internal] load .dockerignore => => transferring context: 2B => [internal] load metadata for docker.io/library/debian:latest => [1/4] FROM docker.io/library/debian:latest => [internal] load build context => => transferring context: 907B => [2/4] WORKDIR /work => [3/4] COPY . /work => [4/4] RUN /bin/bash => exporting to image => => exporting layers => => writing image sha256:02e19f5bec3cec137c85e7364e4cc1174b3020cd48e7ce5fa78a57e223269241 docker run -it --rm 02e19f5bec3c bash root@939c94556f02:/work# ls Dockerfile config_file output
Kubernetes
Start a pod
kubectl run -it --rm docker.io/debian -- /bin/bash
Podman
Start a pod
podman run -it --rm docker.io/debian -- /bin/bash
Vim
Closing
With saving in command mode
:wq ZZ
Safe and quit the file with either command
Without saving in command mode
:q! ZQ
Quit without saving the file with either command
Changing the color scheme
Command :colorscheme
:colorscheme darkblue :colorscheme
# to see all color schemes
Show line numbers
Relative line numbers
enable :set relativenumber toggle :set relativenumber!
Absolut line numbers
enable :set number toggle :set number!
Run commands
Run a command
:!echo "Hi"
Hi Press ENTER or type command to continue
Command output to file
:r !echo "Command output"
Command output
Bash
Loops
Item in list
list=('item1' 'item2' 'item3' 'item4') for item in $list[@]; do echo This is "$item" done
This is item1 This is item2 This is item3 This is item4
Conditional commands
AND
(return 0) && echo "Bla" Bla (return 1) && echo "Bla" # no output since the first condition failed
OR
(return 0) || echo "Bla" # returns nothing since the first condition was true (return 1) || echo "Bla" Bla
IF
NAME="DirtyDan" if [[ $NAME == "Marvin" ]]; then echo "Hi Marvin" elif [[ $NAME == "DirtyDan" ]]; then echo "I am the dirty Dan" else echo "Who tf are you" fi
I am the dirty Dan
IF conditions
# Empty string [[ -z STRING ]] # Not empty string [[ -n STRING ]] # Equal [[ STRING == STRING ]] # Not Equal [[ STRING != STRING ]] # Equal [[ NUM -eq NUM ]] # Not equal [[ NUM -ne NUM ]] # Less than [[ NUM -lt NUM ]] # Less than or equal [[ NUM -le NUM ]] # Greater than [[ NUM -gt NUM ]] # Greater than or equal [[ NUM -ge NUM ]] # Regexp [[ STRING =~ STRING ]] # Numeric conditions (( NUM < NUM )) # If OPTIONNAME is enabled [[ -o noclobber ]] # Not [[ ! EXPR ]] # And [[ X && Y ]] # Or [[ X || Y ]] # Exists [[ -e FILE ]] # Readable [[ -r FILE ]] # Symlink [[ -h FILE ]] # Directory [[ -d FILE ]] # Writable [[ -w FILE ]] # Size is > 0 bytes [[ -s FILE ]] # File [[ -f FILE ]] # Executable [[ -x FILE ]] # File 1 is more recent than 2 [[ FILE1 -nt FILE2 ]] # File 2 is more recent than 1 [[ FILE1 -ot FILE2 ]] # Same files [[ FILE1 -ef FILE2 ]]
Variables
Declare
# Integer declare -i # Array declare -a # Dictionary declare -A # Lower case letters declare -l # Uppercase letters declare -u # Print attributes and options of variable declare -p # Export variable declare -x # Function declare -f # Print name and attributes of function declare -F # Variable that references other variable declare -n
Options
General shell behavior
# Avoid overlay files (redirect via ">") set -o noclobber # Used to exit upon error, avoiding further execution set -o errexit # Unveils hidden failures set -o pipefail # Exposes unset variables set -o nounset
Glob behavior
# Non-matching globs are removed ("*.txt" => "") shopt -s nullglob # Non-matching globs throw errors shopt -s failglob # Case insensitive globs shopt -s nocaseglob # Wildcards match dotfiles ("*.txt" => ".file.txt") shopt -s dotglob # Allow ** for recursive matches ("dir/**/*.txt" => "dir/subdir/subdir/file.txt") shopt -s globstar
Redirection
Standard out to file (overwrite)
echo "Hey I am content" > file
cat file Hey I am content
Standard out to file (append)
echo "Hey I am new content" >> file
cat file Hey I am content Hey I am new content
Advanced to file (overwrite)
cat << EOF > config_file # My config ip=10.1.1.1/32 EOF
cat config_file # My config ip=10.1.1.1/32
Advanced to file (append)
cat << EOF >> config_file # Appended config port=9001 EOF
cat config_file # My config ip=10.1.1.1/32 # Appended config port=9001
Advanced to file and also escape variables (append)
MY_VARIABLE="Content of my variable" cat << "EOF" >> config_file # Appended escaped config file="example" directory=$MY_VARIABLE EOF
cat config_file # My config ip=10.1.1.1/32 # Appended escaped config file="example" directory=$MY_VARIABLE
SQL
MySQL
Get tables from database
use database altislife; show tables;
+---------------------+ | Tables_in_altislife | +---------------------+ | containers | | gangs | | houses | | players | | vehicles | | wanted | +---------------------+ 6 rows in set (0.000 sec)
Generate sha password
select sha("password");
+------------------------------------------+ | sha("password") | +------------------------------------------+ | 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8 | +------------------------------------------+
Create a User
create user "username"@"%" identified by "password";
Query OK, 0 rows affected (0.01 sec)
Create Database
create database database_name;
Query OK, 1 row affected (0.00 sec)
Grant permission on database to user
grant all privileges on database_name.* to "username"@"%"; flush privileges;
Query OK, 0 row affected (0.00 sec) Query OK, 0 row affected (0.00 sec)
Python
Dictionary
With initialized values
dictionary = { 'key1': 'value1', 'key2': 'value3', }
Screen
Create new session
Just create a session
screen
Create with name
screen -S screen_name
Create an run command
screen /usr/bin/some_script.sh
tmux
tmux sessions
Just create session
tmux
Create session with name
tmux new -s session_name
List sessions
tmux ls
0: 1 windows (created Sun May 28 15:57:15 2023) session1: 1 windows (created Sun May 28 15:57:46 2023)
Attach to session
tmux attach -t session_name
Detach from session
Keyboard shortcut: CTRL + b + d
Delete session
tmux kill-session -t session_name
Tools
Git
Clone a repository
# Clone repository git clone git@vmgitlab.fritz.box:gene/playground.git # Clone repository with specific folder name git clone git@vmgitlab.fritz.box:gene/playground.git my_repositroy
Add changes/files
# Add single file git add README.md # Add all files (including sub directories) git add .
Commit changes
git commit -m 'message'
Pull changes
git pull
Push changes
git push
Show branches
git branch
Create branches
git branch -c new_branch
Delete branches
git branch -d old_branch
Switch to branch/commit/label
# Change to branch git checkout branch # Change to commit git checkout 8350feb7ef876f6a644a8f99ff3ad3fda48f87a1 # change to tag git checkout v1.0.0
Initialize repository
git init
Git ignore file
# Create gitignore file in the root of the repository touch .gitignore Ignore file named .env echo '.env' >> .gitignore Ignore files with .txt suffix echo '*.txt' >> .gitignore Ignore files with .txt suffix including sub directories echo '**.txt' >> .gitignore
Remove local untracked files
git clean
Reset to previous state
git reset