Table of Contents

Linux

Useful Linux commands, with no particular order

Check which program is using a particular port

sudo lsof -i:port

Kill a particular PID

sudo kill -9 PID

Check the current folder size

du -hs .

Show filesystem information

df -h

SSH

More info: https://www.ssh.com/ssh/tunneling/example/

Copying local keys to a remote server

ssh-copy-id user@server

Create a remote tunnel into localhost

ssh -L local-port:localhost:remote-port user@server -N

Note: AllowTcpForwarding and PermitOpen have to be enabled in /etc/ssh/sshd_config

Forward a local port to a remote host (like ngrok)

ssh -N -T -R local-port:localhost:remote-port user@server

SCP

scp -r /folder/to/upload user@server:/destination/on/server

tar & untar

tar

tar -czvf file.tar.gz file/or/folder/to/tar/

untar

tar -xvf file.tar.gz

gpg encrypt/decrypt

Encrypt

gpg -c --cipher-algo AES256 file/to/encrypt

You will be asked to enter a password

Decrypt

gpg file/to/decrypt

You will be asked to enter a password

Download an entire website using wget

More: https://gist.github.com/mikecrittenden/fe02c59fed1aeebd0a9697cf7e9f5c0c

wget \
     --mirror \ # Makes (among other things) the download recursive.
     --page-requisites \ # Get all assets/elements (CSS/JS/images).
     --adjust-extension \ # Save files with .html on the end.
     --span-hosts \ # Include necessary assets from offsite as well.
     --convert-links \ # Update links to still work in the static version.
     --restrict-file-names=windows \ # Modify filenames to work in Windows as well.
     --domains yoursite.com \ # Do not follow links outside this domain.
     --no-parent \ # Don't follow links outside the directory you pass in.
         yoursite.com/whatever/path # The URL to download

Create a X.509 sha256 self signed certificate

openssl req \
  -x509 \
  -newkey rsa:4096 \
  -sha256 \
  -keyout mykeyname.key \
  -out mycertname.pem \ 
  -days 365 
  -nodes # only if you need no password

Find

Find a specific file in the specified folder

You can use:

find mypath -type f -name "myfile.extension"

xargs

xargs is used to create new commands from the output of another command. For example, if I'm performing a find command, I could use xargs to issue a new command for each line of the output of find.

find . -type f -name \"*.json\" | xargs --verbose -I % sh -c 'cat % | jq -c || exit 255'

What's happening?

./folder/this_is_a_json.json
./another.json

This command in particular is very usefull to check if all the JSON files in a repository are well formated in a CI/CD step.

ncdu

Ncdu is a disk usage analyzer with an ncurses interface. It is designed to find space hogs on a remote server where you don’t have an entire graphical setup available, but it is a useful tool even on regular desktop systems. Ncdu aims to be fast, simple and easy to use, and should be able to run in any minimal POSIX-like environment with ncurses installed.

Usage:

ncdu -x /

Where / is the filesystem you want to check

awk

echo "This is a string" | awk '{print ($1)}'
# Output: "This"
echo "This Is A CaPiTaLiZeD String" | awk '{print tolower($0)}'
# Output: "this is a capitalized string"
 
echo "This Is A CaPiTaLiZeD String" | awk '{print toupper($0)}'
# Output: "THIS IS A CAPITALIZED STRING"