Command Line Environment
Job Control
Killing a process
Ctrl-C: sendingSIGINTCtrl-\: sendingSIGQUITkill -TERM <PID>: usekillcommand to sendSIGTERMsignal
Pausing and backgrounding processes
Ctrl-Z: sendingSIGSTP, to pause a process- Background an already running program:
Ctrl-Z, thenbg
- Background an already running program:
fgorbgcan continue the paused job in the foreground or in the backgroundpgreplook up for processes based on name or other attributes. more about thisnohupto ignoreSIGHUP$ nohup sleep 2000 &[2] 18745appending output to nohup.outSIGKILLcannot be killed by the process and it will always terminate it immediately, but it's risky of leaving orphaned childen processes.
Terminal Multiplexers
tmux: run more than one thing at once
Sessions: an independent workspace with one or more windows
tmuxstarts a new sessiontmux new -s NAMEstarts a session with that nametmux lslists current sessionstmux aattaches the last session, use-tto specify which oneCtrl-b+ddetaches the current session
Windows: visually seperate parts of the same session
Ctrl-b+ccreates a windowCtrl-dcloses the current windowCtrl-b+Ngoes to the Nth windowCtrl-b+pgoes to the previous windowCtrl-b+ngoes to the next windowCtrl-b+'renames the current windowCtrl-b+wlists current windows
Panes: have multiple shells in the same visual display (less used)
Ctrl-b+"splits the current window horizontallyCtrl-b+%splits the current window verticallyCtrl-b+<direction>move to the pane in the corresponding directionCtrl-b+ztoggle zoom (最小化) the current pane
About Screen
Alias
- Create a short form for another command, its structure:
alias alias_name="command_to_alias arg1 arg2"- Several convenient features
# Make shorthands for common flagsalias ll="ls -lh"# Save a lot of typing for common commandsalias gs="git status"alias gc="git commit"alias v="vim"# Save you from mistypingalias sl=ls# Overwrite existing commands for better defaultsalias mv="mv -i" # -i prompts before overwritealias mkdir="mkdir -p" # -p make parent dirs as neededalias df="df -h" # -h prints human readable format# Alias can be composedalias la="ls -A"alias lla="la -l"- Disable an alias
# To ignore an alias run it prepended with \\ls# Or disable an alias altogether with unaliasunalias la- Get an alias definition
# To get an alias definition just call it with aliasalias ll# Will print ll='ls -lh'- Can configure alias in
.bashrcor.zshrc
Dotfiles
For configuration
Configuration files for tools, here are some of them:
bash-~/.bashrc,~/.bash_profilegit-~/.gitconfigvim-~/.vimrcand the~/.vimfolderssh-~/.ssh/configtmux-~/.tmux.conf
Benefits:
- Easy installation: if you log in to a new machine, applying your customizations will only take a minute.
- Portability: your tools will work the same way everywhere.
- Synchronization: you can update your dotfiles anywhere and keep them all in sync.
- Change tracking: you’re probably going to be maintaining your dotfiles for your entire programming career, and version history is nice to have for long-lived projects.
Can refer to others' dotfiles on Github
Portability
- Machine-specific configuration
if [[ "$(uname)" == "Linux" ]]; then {do_something}; fi# Check before using shell-specific featuresif [[ "$SHELL" == "zsh" ]]; then {do_something}; fi# You can also make it machine-specificif [[ "$(hostname)" == "myServer" ]]; then {do_something}; fi- Sharing alias for different programs (e.g.
bashandzsh)
# Test if ~/.aliases exists and source itif [ -f ~/.aliases ]; then source ~/.aliasesfiRemote Machines
Executing commands remotely
- Remote
ls:
ssh foobar@server lslslocally andgrepremotely
ls | ssh foobar@server grep PATTERNSSH 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_ed25519Check if you have a passphrase and validate it, run:
ssh-keygen -y -f /path/to/keyKey-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@remoteCopying files over SSH
ssh+tee
cat localfile | ssh remote_server tee serverfileNotes that tee writes the output from STDIN into a file
scp
scp path/to/local_file remote_host:path/to/remote_filersyncan improved version ofscp.- preventing copying the same file
- provides more fine grained control over symlinks, permissions
--partialresume from a previous interrupted copy
*Port forwarding
- Local port forwarding
- Remote port forwarding
- If we execute
jupyter notebookin the remote server that listens to the port8888. To forwards that to the local port9999:ssh -L 9999:localhost:8888 foobar@remote_server. Then navigate tolocalhost:9999in our local machine.
SSH Configuration
- Use alias
alias my_server="ssh -i ~/.id_ed25519 --port 2222 -L 9999:localhost:8888 foobar@remote_server~/.ssh/config
Host vm User foobar HostName 172.16.174.141 Port 2222 IdentityFile ~/.ssh/id_ed25519 LocalForward 9999 localhost:8888# Configs can also take wildcardsHost *.mit.edu User foobazOther programs like scp, rsync, mosh can read this config and convert it to the corresponding flags.