My own linux cheatsheet

cause im lazy and have alzheimers

(this list will constantly be expanded)
  1. ffmpeg
  2. MySQL
  3. Linux
  4. NodeJS & npm
  5. Python3

ffmpeg

ffmpeg converts videos. Simple examples:

ffmpeg -i input.mov -vcodec h264 -acodec aac output.mp4
ffmpeg -i input.mov -vcodec libvpx -acodec libvorbis output.webm
ffmpeg -i input.mp4 -b:a 96k -vf scale="iw/4:-1" -crf 80 out.mp4

The first one converts to mp4, the second to webm, the third just heavily compresses the video and audio.
A list of possible Arguments:
Usage: ffmpeg -i input.file X output.file
where X equals a number of 'arguments':

// Convert Music to lower bitrate
-map 0:a:0 -b:a 96k

//Aspect ratio
ffmpeg -i input.mp4 -aspect 16:9 output.mp4

//Double Speed (0.5 = x2)
ffmpeg -i input.mp4 -vf "setpts=0.5*PTS" output.mp4

//Double audio speed
ffmpeg -i input.mp4 -filter:a "atempo=2.0" -vn output.mp4

//Set bitrate (compresses if lower than source, a = audio, v = video)
-b:v 1M
-b:a 96k

//Volume of audio
ffmpeg -i input.mp3 -af 'volume=1.5' output.mp3

//Change scale of video (iw = width, ih = height, -1 means auto)
-vf scale="iw/2:-1"
-vf scale="1280:720"

//Crop (Cut) Video, w=width, x=startpoint for the rectangle w/h
ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4

//Quicker video scaling
-i input.mp4 -filter:v scale=1280:720 -c:a copy output.mp4

//Video to images (Names are image-001.png, image-002.png, ...)
//-r = framerate in which screenshots are taken, default=25
//%3d = name, 001 - 999
ffmpeg -i input.mp4 -r 1 -f image2 image-%3d.png

//Add Text to the video
-vf drawtext="text='MyTextHere': fontcolor=white: fontsize=24: x=(w-text_w)/2: y=(h-text_h)/2"

//Combine 2 videos
ffmpeg -i in_one.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts _1.ts
ffmpeg -i in_two.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts _2.ts
ffmpeg -i "concat:_1.ts|_2.ts" -c copy -bsf:a aac_adtstoasc together.mp4

//Compress video (Should be between 20 and 28, 51 is max and looks ew)
-crf 24
-crf 51


//Change Video Codec ('copy' means copy the input video codec)
-vcodec h264
-vcodec libvpx
-vcodec copy


//Change Audio Codec
-acodec aac
-acodec mp3
-acodec copy


//Remove Audio / Video
-an
-vn

//Remove video would be:
ffmpeg -i input_video.mp4 -vn output_audio.mp3

//Example mp4 to mp3 convert
ffmpeg -i in.mp4 -vn -acodec mp3 out.mp3

//Convert a video from 30 to 60 FPS
ffmpeg -i Input.mp4 -filter:v "minterpolate='fps=60'" Out60.mp4

//Make 2 input files side by side (-r makes the framerate be 60)
ffmpeg -i 30.mp4 -i 60.mp4 -r 60 -filter_complex hstack comparison.mp4

//Cut Video (time either in 00:01:23.000 or in seconds format, here seconds are used)
//This would cut the video, so that only second 3 to 8 plays (5 seconds long)
// -t = duration, -ss = starttime
-ss 3 -t 5

//Burn in subtitles
ffmpeg -i in.mkv -vf subtitles=in.mkv out.mp4

//Meme top and bottom text with white padding background
ffmpeg -i input.mp4 -filter_complex \
"[0:v]pad=iw:ih+100:0:(oh-ih)/2:color=white, \
 drawtext=text='ONE DOES NOT SIMPLY':fontsize=24:x=(w-tw)/2:y=(50-th)/2, \
 drawtext=text='STOP ME FROM FILTERING':fontsize=24:x=(w-tw)/2:y=h-25-(th/2)" \
output.mp4

//Convert a folder from mp4 to mp3
for i in *.mp4; do ffmpeg -i "$i" "$(basename "$i" .mp4)".mp3  ; done

// Copy codes (copy video and audio stream, makes video cutting way quicker)
-c copy


MySQL

After installing mysql-server:

mysql_secure_installation

Creating a new user in mysql:
mysql -u root -p
CREATE DATABASE mydb;
CREATE USER 'martin'@'localhost' IDENTIFIED BY 'MyOwnPasswordHere?';
GRANT ALL PRIVILEGES ON mydb.* TO 'martin'@'localhost';
FLUSH PRIVILEGES;


Linux

//change folder
cd folder

//go back one folder
cd ..

//you can string folders together
cd ../folder1/folder2

//edit text files
nano file

//list files in your current folder
ls

//list files in a folder
ls folder

// list all files, in a list, with human readable filesizes
ls -ahl

//output file to console
cat file.txt

//output file to console but with scroll option
less file.txt

//create folder
mkdir folder

//create file
touch file

//remove file
rm file

//open a "task manager" (or install htop, it's better)
top

//show IP Addresses
ip a

//want to know if you have internet? ping something
ping orf.at

//want to stop a process currently on console? press CTRL+C
tail -f access.log
CTRL+C

//download a web page
wget https://google.com

//get help about a command (in this example: command1)
man command1

//find files recursively
find . -name "*.txt"

//change permissions of folder + subfolders to user
sudo chown -R user:user folder/

//search through a log file for user
cat access.log | grep "user"

//continuously output log file
tail -f access.log

//show filesize of current folder + subfolders
du -sh

//show available storage space
df -h

//move files from folder to current directory
mv folder/* .

//delete folder and everything inside it
rm -r folder

//zip folder
zip -r folder.zip folder
unzip master.zip

//copy folder
cp -r folder archive/folder

//only output the first few lines / the last few lines
head file.txt
tail file.txt

//only output the last 3 lines
tail -n 3 file.txt

//restart system services
systemctl restart apache2

//get status of service
systemctl status apache2

//add service to autostart (enable = autostart, disable = no autostart)
systemctl enable apache2

//systemctl for older systems:
service apache2 status

//update program list for installations
apt-get update

//update older programs
apt-get upgrade

//install a new program (for example htop)
apt-get install htop

//search for packages containing name (for example htop)
apt-cache search htop

//make a file executeable
chmod +x file

//list open ports
netstat -plunt

//make a command run in the background with & (not recommended for tail -f and similar)
zip -r folder.zip folder &
nordvpn connect germany &

//make a command run in a seperate "window"
apt-get install screen
screen -S minecraft
./minecraft-server
CTRL+A
D
//This creates a new "screen" with the name minecraft
//executes the server there
//and returns from the screen back to your normal console (CTRL+A & D)
//screen is like a new "window" that you can open
//to return to a previous screen
screen -r minecraft

//Move every file from "foldername" that ends with ".txz" to "/dest/path"
find foldername -name '*.txz' -type f -exec mv {} '/dest/path' \;

//Download list of youtube videos seperated with \n:
youtube-dl -x -f best --batch-file filename.txt

Example iptables (CentOS has this in /etc/sysconfig/iptables):
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

Run a command x times:
for run in {1..100}; do command; done

Example fail2ban (in /etc/fail2ban/jail.local):
//For Debian
[DEFAULT]
bantime = 900
maxretry = 3
[sshd]
enabled = true

//For CentOS
[DEFAULT]
bantime = 900
maxretry = 3
banaction = iptables-allports
[sshd]
enabled = true

Getting SSL Certificates (for https):
sudo apt-get install certbot python-certbot-apache
certbot --apache

NodeJS & npm

Install node and npm via binary:
Source: https://github.com/nodejs/help/wiki/Installation
Get the newest version of node&npm here:https://nodejs.org/en/download/ (Linux Binaries (x64))

VERSION=v12.18.0
DISTRO=linux-x64
sudo mkdir -p /usr/local/lib/nodejs
sudo tar -xJvf node-$VERSION-$DISTRO.tar.xz -C /usr/local/lib/nodejs 

Add the following to the end of ~/.profile:
  # Nodejs
  VERSION=v12.18.0
  DISTRO=linux-x64
  export PATH=/usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin:$PATH

Then refresh your profile:
  source ~/.profile

Now you can use npm & node:
  npm version
  node -v

If you have a ~/.bash_profile file you have to add the following to it:
  # Load user profile file
  if [ -f ~/.profile ]; then
    . ~/.profile
  fi

Then your .profile (aka. nodejs) file will load automatically upon login.

It's also good to link node and npm to locations the systems expect them to be in.
In this example with x64 v12.18:
  sudo ln -s /usr/local/lib/nodejs/node-v12.18.0-linux-x64/bin/node /usr/local/bin/node
  sudo ln -s /usr/local/lib/nodejs/node-v12.18.0-linux-x64/bin/node /usr/bin/node
  sudo ln -s /usr/local/lib/nodejs/node-v12.18.0-linux-x64/bin/node /usr/lib/node
  sudo ln -s /usr/local/lib/nodejs/node-v12.18.0-linux-x64/bin/npm /usr/bin/npm
  sudo ln -s /usr/local/lib/nodejs/node-v12.18.0-linux-x64/bin/npm /usr/local/bin/npm

Python 3

#Read file with codec "latin-1" line for line
with open("file.txt","r",encoding="latin-1") as f:
  data = f.readlines()
  for line in data:
    print(line)