ubuntu, updated
Terminal shortcuts and history
The keyboard shortcuts, history tricks and small habits that make the Bash terminal fast.
Bash, sh and the terminal
- Terminal: the window (or SSH session) you type into.
- Shell: the program that reads your commands. On Ubuntu that is Bash (
/bin/bash). - sh: a minimal shell for scripts. On Ubuntu
/bin/shis dash, not Bash, so Bash features like[[ ]]or arrays fail in#!/bin/shscripts. Use#!/bin/bashif you need them.
Keyboard shortcuts
| Keys | Does |
|---|---|
Tab / Tab Tab | Complete a command or path / show all options |
Ctrl+R | Search your history; press again for older matches |
Ctrl+A / Ctrl+E | Jump to start / end of line |
Alt+B / Alt+F | Jump one word back / forward |
Ctrl+W | Delete the word before the cursor |
Ctrl+U / Ctrl+K | Delete to start / to end of line |
Ctrl+Y | Paste what you just deleted |
Alt+. | Insert the last argument of the previous command |
Ctrl+L | Clear the screen |
Ctrl+C | Cancel the running command |
Ctrl+D | Log out (end of input) |
Ctrl+Z | Pause the running command (fg resumes it) |
History tricks
| Type | Means |
|---|---|
!! | The last command (sudo !! is the classic) |
!$ | The last argument of the last command |
!123 | Run command number 123 from history |
history | grep netplan | Find an old command |
cd - | Go back to the previous folder |
Clearing history and keeping secrets out of it: see Shell history.
Small habits that pay off
mkdir -p /srv/app/{logs,data,conf} # create several folders at once
cp nginx.conf{,.bak} # quick backup: nginx.conf -> nginx.conf.bak
watch -n 2 df -h # rerun a command every 2 seconds
tail -f /var/log/syslog # follow a log live (Ctrl+C to stop)
less /var/log/syslog # read a file: /search, G = end, q = quit
Aliases
Put your own shortcuts at the end of ~/.bashrc, then run source ~/.bashrc:
alias ll='ls -alFh'
alias ports='sudo ss -tulpn'
alias update='sudo apt update && sudo apt upgrade'
Don’t lose work when SSH drops
Long jobs (upgrades, big copies) die when your SSH connection breaks. Run them inside tmux (installed on Ubuntu Server by default):
| What | Command |
|---|---|
| Start a session | tmux new -s work |
| Detach (leave it running) | Ctrl+B, then D |
| List sessions | tmux ls |
| Re-attach | tmux attach -t work |
More in the tmux cheat sheet.