You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Find the PID on a specific port
sudo lsof -i :8000
# Kill itkill -9 <PID># One-liner
sudo kill -9 $(sudo lsof -t -i:8000)
List Running Ports
# List all listening ports
sudo lsof -i -P -n | grep LISTEN
# List ports starting with a specific prefix (e.g., 80xx)
sudo lsof -i -P -n | grep LISTEN | grep ':80'# Alternative using ss
ss -tlnp | grep ':80'# Show all ports starting with 3xxx
sudo lsof -i -P -n | grep LISTEN | grep ':3'
# Create a permanent alias (add to ~/.bash_aliases or ~/.bashrc)alias<name>='<command>'# List all aliasesalias# Find alias source
which [alias]
Command Chaining Operators
Operator
Description
Example
&
Run command in background
ping google.com &
;
Run commands sequentially
apt update; apt upgrade
&&
Run next only if previous succeeds
ping google.com && ls
||
Run next only if previous fails
apt update || echo "Failed"
!
Exclude pattern
rm -r !(*.html)
{}
Command grouping
[ -d bin ] || { mkdir bin; }
()
Precedence grouping
(cmd1 && cmd2) || (cmd3 && cmd4)
\
Line continuation
Multi-line commands
SSH Port Forwarding
SSH tunneling allows you to forward ports between local and remote machines.
# Local port forwarding: access remote port via localhost
ssh -L [local_port]:localhost:[remote_port] user@remote_host
# Example: Forward remote PostgreSQL
ssh -L 5432:localhost:5432 user@db-server.com
# Forward through a jump server
ssh -L 9000:other-server:80 user@jump-server.com
# Remote port forwarding: expose local port to remote
ssh -R 8080:localhost:3000 user@server.com
# Dynamic port forwarding (SOCKS proxy)
ssh -D 1080 user@server.com
# Background connection (no shell)
ssh -fN -L 8080:localhost:80 user@server.com
# Multiple port forwards
ssh -L 8080:localhost:80 -L 3306:localhost:3306 user@server.com
# With specific SSH key
ssh -i ~/.ssh/my_key -L 8080:localhost:80 user@server.com
Disk Usage
# Check overall disk usage (all mounted filesystems)
df -h
# Check size of folders in current directory
du -sh *# Check size of a specific directory
du -sh /path/to/dir
# Find top 10 largest files/folders in current directory
du -sh *| sort -rh | head -10
Counting Output Lines
# Count lines of output from any command
some_command | wc -l
# Count lines in a file
wc -l filename
# Count words
wc -w filename
# Count characters
wc -c filename
# Count all (lines, words, chars)
wc filename
# Count files in a directory
ls | wc -l
# Count matches from grep
grep "error" log.txt | wc -l
# Count unique lines
sort filename | uniq | wc -l
tmux
Create new window
Ctrl + b, then c
Switch windows
Next: Ctrl + b, then n
Previous: Ctrl + b, then p
By number: Ctrl + b, then 0–9
List/select: Ctrl + b, then w
Last window: Ctrl + b, then l
Rename window
Ctrl + b, then , → type new name, press Enter
Close window
Ctrl + b, then & → press y
Or type exit / press Ctrl + d
Or: tmux kill-window
Kill sessions
Kill all sessions: tmux kill-server
Kill all except current: tmux kill-session -a
Kill specific session: tmux kill-session -t <name>