Compress files (e.g. making a backup)

tar -zcvf archive-name.tar.gz directory-name

Options:

  • z: Compress archive using gzip program
  • c: Create archive
  • v: Verbose i.e display progress while creating archive
  • f: Archive File name

Uncompress files (e.g. restoring a backup)

tar -zxvf archive-name.tar.gz

Options:

  • x: Extract files

Accessing a remote machine

Having a .pem file:

ssh -i key.pem ubuntu@0.0.0.0

Copy files between machines

Having a .pem file and using scp:

scp -i key.pem file1 (...) fileN ubuntu@0.0.0.0://home/ubuntu

Using rsync via ssh:

rsync -avz -e "ssh -p 22000" remote_user@remote_host:/remote/path/ ~/local/path/

rsync options:

  • a: Archive mode (see this answer)
  • v: Verbose
  • z: Compress data during transfer
  • e: Specify remote shell to use (ssh)

See this blog post for a great depiction of many other different ways to use rsync.

Search for files

find $directory -name '*.json'

The command above will search for all files that have the extesion ‘json’ in the directory specified (including subdirectories).

Search for strings in many files

grep -rn "string"

Options:

  • r: Recursive
  • n: Show line Number

Space usage

du -sh [file_path]

Options:

  • s (--summarize): display only a total of each argument
  • h (--human-readable): print sizes in human readable format (e.g., 1K 234M 2G)

Screen (terminal sessions in the background)

List current screen sessions:

screen -ls

Start a new screen session with name [name]:

screen -S [name]

Resume (reconnect) to the screen named [name]

screen -r [name]

Commands when you are inside a screen

Detach from current screen: Ctrl-a d

Rename current screen to [name]: Ctrl-a : sessionname [name]

System statistics

Performance statistics for all logical processors:

mpstat -P ALL 1

Memory usage statistics:

vmstat -s

Memory and CPU usage per process:

top

Git

Pretty logging

git log --pretty=oneline --all --decorate --graph

Pull only a specific directory:

git fetch REMOTE_NAME
git checkout REMOTE_NAME/BRANCH -- relative/path/to/dir

Download only a specific directory (without having to clone the whole repo): check this answer.

Packages on Ubuntu

See where a package is installed:

dpkg -L <packagename>

Sorting mp3 in a USB stick

Check path to USB stick (search for device name):

lsblk

Unmount:

sudo umount /dev/sdb1

Sort using fatsort:

sudo fatsort /dev/sdb1

Prevent SSH connection to break

Add to your ~/.ssh/config (create if it doesn’t exist) the following:

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 2

For more details see 1, 2.