Command Line Environment

PrettyMeng, code

Job Control

Killing a process

Pausing and backgrounding processes

Terminal Multiplexers

tmux: run more than one thing at once

Alias

alias alias_name="command_to_alias arg1 arg2"
# Make shorthands for common flags
alias ll="ls -lh"
# Save a lot of typing for common commands
alias gs="git status"
alias gc="git commit"
alias v="vim"
# Save you from mistyping
alias sl=ls
# Overwrite existing commands for better defaults
alias mv="mv -i" # -i prompts before overwrite
alias mkdir="mkdir -p" # -p make parent dirs as needed
alias df="df -h" # -h prints human readable format
# Alias can be composed
alias la="ls -A"
alias lla="la -l"
# To ignore an alias run it prepended with \
\ls
# Or disable an alias altogether with unalias
unalias la
# To get an alias definition just call it with alias
alias ll
# Will print ll='ls -lh'

Dotfiles

For configuration

Portability

if [[ "$(uname)" == "Linux" ]]; then {do_something}; fi
# Check before using shell-specific features
if [[ "$SHELL" == "zsh" ]]; then {do_something}; fi
# You can also make it machine-specific
if [[ "$(hostname)" == "myServer" ]]; then {do_something}; fi
# Test if ~/.aliases exists and source it
if [ -f ~/.aliases ]; then
source ~/.aliases
fi

Remote Machines

Executing commands remotely

ssh foobar@server ls
ls | ssh foobar@server grep PATTERN

SSH Keys

Use public-key cryptography to prove to the server that the client owns the private key without revealing the key.

Key generation:

ssh-keygen -o -a 100 -t ed25519 -f ~/.ssh/id_ed25519

Check if you have a passphrase and validate it, run:

ssh-keygen -y -f /path/to/key

Key-based authentification

ssh will look into .ssh/authorized_keys to determine which clients it should let in. To copy a public key over:

cat .ssh/id_ed25519.pub | ssh foobar@remote 'cat >> ~/.ssh/authorized_keys'

or with ssh-copy-id

ssh-copy-id -i .ssh/id_ed25519.pub foobar@remote

Copying files over SSH

cat localfile | ssh remote_server tee serverfile

Notes that tee writes the output from STDIN into a file

scp path/to/local_file remote_host:path/to/remote_file

*Port forwarding

SSH Configuration

alias my_server="ssh -i ~/.id_ed25519 --port 2222 -L 9999:localhost:8888 foobar@remote_server
Host vm
User foobar
HostName 172.16.174.141
Port 2222
IdentityFile ~/.ssh/id_ed25519
LocalForward 9999 localhost:8888
# Configs can also take wildcards
Host *.mit.edu
User foobaz

Other programs like scp, rsync, mosh can read this config and convert it to the corresponding flags.

CC BY-NC 4.0 © PrettyMeng.RSS