Skip to content

Latest commit

 

History

History
254 lines (178 loc) · 5.97 KB

File metadata and controls

254 lines (178 loc) · 5.97 KB

Process Management

Kill a Process by Port

# Find the PID on a specific port
sudo lsof -i :8000

# Kill it
kill -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'

File and Directory Operations

Command Description
ls List all folders and files
ls -l Long format (detailed information)
ls -a Show all files, including hidden files
mkdir [folder-name] Create a new folder
rm [file_path] Delete a file
rmdir [folder_path] Delete an empty folder
rm -rf [path] Force delete everything (use with caution!)

Terminal Navigation Shortcuts

Shortcut Description
Ctrl + A Jump to start of the line
Ctrl + E Jump to end of the line
Ctrl + R Search command history
Ctrl + U Clear before cursor
Ctrl + K Clear after cursor
cd - Go back to previous directory

Command History

Command Description
!! Repeat the last command
history View command history
history | grep [keyword] Search history for a keyword

Searching and Filtering Text

# Basic pattern search
grep "error" log.txt

# Case-insensitive search
grep -i "warning" log.txt

# Recursive search in directories
grep -r "pattern" /path/to/directory

String Manipulation

# Trim a string
str='This is an example sentence'
echo "${str:0:15}" # Output: "This is an exam"

File Manipulation

# Combine multiple text files into one
cat file1 file2 file3 > combined.txt
cat *.txt > combined.txt

# Copy files with visual progress
rsync --progress file1.txt /opt

Certificate Conversion (OpenSSL)

# Convert .cer (DER) to .pem
openssl x509 -in cert.cer -inform DER -out cert.pem -outform PEM

# Convert .crt (PEM) to .pem
openssl x509 -in cert.crt -inform PEM -out cert.pem -outform PEM

Alias Management

# Create a permanent alias (add to ~/.bash_aliases or ~/.bashrc)
alias <name>='<command>'

# List all aliases
alias

# 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>