Read the docs!

Maybe one of the most underused tools in the command-line is the documentation of the commands themselves. You can easily open the docs for the vast majority of the commands you will need simply by running man <command>. Then search for what you are interested in by typing /.

Alternatives to man

While man is the default command to show docs, the community has developed alternatives with simpler output:

  • tldr (npm install -g tldr) (personally I find it the best option);
  • bropages (gem install bropages);
  • cheat (pip install cheat).

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.

Find (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).

Grep (Search for strings in many files)

grep -rn "string"

Options:

  • r: Recursive
  • n: Show line Number
ln -s <original-file-path> <simlink-path>

Options:

  • s: Creates symbolic link instead of hard link.

Disk Usage (du)

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