Command Line Environment
Job Control
Killing a process
Ctrl-C
: sendingSIGINT
Ctrl-\
: sendingSIGQUIT
kill -TERM <PID>
: usekill
command to sendSIGTERM
signal
Pausing and backgrounding processes
Ctrl-Z
: sendingSIGSTP
, to pause a process- Background an already running program:
Ctrl-Z
, thenbg
- Background an already running program:
fg
orbg
can continue the paused job in the foreground or in the backgroundpgrep
look up for processes based on name or other attributes. more about thisnohup
to ignoreSIGHUP
$ nohup sleep 2000 &[2] 18745appending output to nohup.outSIGKILL
cannot 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
tmux
starts a new sessiontmux new -s NAME
starts a session with that nametmux ls
lists current sessionstmux a
attaches the last session, use-t
to specify which oneCtrl-b
+d
detaches the current session
Windows: visually seperate parts of the same session
Ctrl-b
+c
creates a windowCtrl-d
closes the current windowCtrl-b
+N
goes to the Nth windowCtrl-b
+p
goes to the previous windowCtrl-b
+n
goes to the next windowCtrl-b
+'
renames the current windowCtrl-b
+w
lists 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
+z
toggle 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
.bashrc
or.zshrc
Dotfiles
For configuration
Configuration files for tools, here are some of them:
bash
-~/.bashrc
,~/.bash_profile
git
-~/.gitconfig
vim
-~/.vimrc
and the~/.vim
folderssh
-~/.ssh/config
tmux
-~/.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.
bash
andzsh
)
# Test if ~/.aliases exists and source itif [ -f ~/.aliases ]; then source ~/.aliasesfi
Remote Machines
Executing commands remotely
- Remote
ls
:
ssh foobar@server ls
ls
locally andgrep
remotely
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
ssh
+tee
cat localfile | ssh remote_server tee serverfile
Notes that tee
writes the output from STDIN into a file
scp
scp path/to/local_file remote_host:path/to/remote_file
rsync
an improved version ofscp
.- preventing copying the same file
- provides more fine grained control over symlinks, permissions
--partial
resume from a previous interrupted copy
*Port forwarding
- Local port forwarding
- Remote port forwarding
- If we execute
jupyter notebook
in 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:9999
in 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 foobaz
Other programs like scp
, rsync
, mosh
can read this config and convert it to the corresponding flags.