Shell Productivity
Shell Productivity — Quick Reference
Section titled “Shell Productivity — Quick Reference”history — Command History
Section titled “history — Command History”history # list all previous commandshistory 20 # last 20 commandshistory | grep git # search history!! # re-run last command!42 # re-run command #42 from history!git # re-run last command starting with "git"!$ # last argument of previous commandCtrl+R # interactive reverse search through historyalias — Command Shortcuts
Section titled “alias — Command Shortcuts”alias ll='ls -lah' # define aliasalias gs='git status'alias ..='cd ..'alias grep='grep --color=auto'
unalias ll # remove aliasalias # list all aliasesTo make aliases permanent, add them to ~/.bashrc or ~/.zshrc.
source — Load a Script into Current Shell
Section titled “source — Load a Script into Current Shell”source ~/.bashrc # reload shell configsource .env # load env file into current shell. ~/.bashrc # same as source (shorthand)tee — Write to File AND stdout
Section titled “tee — Write to File AND stdout”command | tee output.txt # write to file and print to terminalcommand | tee -a output.txt # append instead of overwritecommand | tee file.txt | grep ERROR # branch the pipeUseful when you want to save output while still seeing it live.
watch — Repeat a Command on Interval
Section titled “watch — Repeat a Command on Interval”watch -n 2 df -h # run df -h every 2 secondswatch -n 1 'ps aux | grep node' # monitor a processwatch -d free -h # highlight changes between runsset — Shell Script Behavior
Section titled “set — Shell Script Behavior”Put at the top of bash scripts:
set -e # exit immediately on any errorset -u # treat unset variables as errorsset -x # print each command before running (debug mode)set -o pipefail # catch errors in piped commands (set -e misses these)
# All together (recommended for scripts)set -euo pipefailKeyboard Shortcuts (bash / zsh)
Section titled “Keyboard Shortcuts (bash / zsh)”| Shortcut | Action |
|---|---|
Ctrl+R | Reverse search history |
Ctrl+C | Kill current process |
Ctrl+Z | Suspend current process |
Ctrl+D | Exit / EOF |
Ctrl+L | Clear screen |
Ctrl+A | Move cursor to start of line |
Ctrl+E | Move cursor to end of line |
Ctrl+W | Delete word before cursor |
Ctrl+U | Delete from cursor to start of line |
Ctrl+K | Delete from cursor to end of line |
Alt+. | Insert last argument of previous command |
Tab | Autocomplete |
Redirection & Pipes
Section titled “Redirection & Pipes”command > file.txt # stdout to file (overwrite)command >> file.txt # stdout to file (append)command 2> err.txt # stderr to filecommand &> file.txt # stdout + stderr to filecommand 2>/dev/null # discard stderrcommand1 | command2 # pipe stdout of cmd1 to cmd2command1 |& command2 # pipe stdout + stderrtee file.txt # write to file AND pass throughBrace Expansion
Section titled “Brace Expansion”mkdir -p src/{components,utils,hooks} # create multiple dirstouch file{1..5}.txt # file1.txt ... file5.txtcp config.yaml config.yaml.bak # quick backupmv file.{txt,md} # rename extensionecho {a,b,c}.log # a.log b.log c.logParameter Expansion
Section titled “Parameter Expansion”file="report.tar.gz"
${file%.*} # remove shortest suffix match → report.tar${file%%.*} # remove longest suffix match → report${file#*.} # remove shortest prefix match → tar.gz${file##*.} # remove longest prefix match → gz${file/old/new} # replace first match${file//old/new} # replace all matches${#file} # length of string → 13${file:-default} # use default if unsetUseful One-Liners
Section titled “Useful One-Liners”# Run last command as sudosudo !!
# Create a timestamped backupcp config.yaml config.yaml.$(date +%Y%m%d)
# Run a command every time a file changes (using watch)watch -n 1 cat output.txt
# Find and kill a process on a portkill $(lsof -ti :8080)
# Print lines 10-20 of a filesed -n '10,20p' file.txt
# Count unique IPs in an access logawk '{print $1}' access.log | sort | uniq -c | sort -rn | head -10
# Monitor disk usage every 5 secondswatch -n 5 df -hset -euo pipefailat the top of every bash script — catches most silent failures.Ctrl+Ris the fastest way to re-run a long command you used recently.- Store
aliasdefinitions in~/.bashrc(bash) or~/.zshrc(zsh) to persist across sessions. teeis invaluable for debugging pipelines — branch output to a file without breaking the chain.