Code

Useful code snippets and gists

DevOps Terminal Management

bash

Best practices and tools for managing terminals in DevOps workflows, including multiplexers, remote sessions, container tools, and shell customization

Terminal window
# ============================================
# DEVOPS TERMINAL MANAGEMENT
# ============================================
# --- 1. TERMINAL MULTIPLEXERS ---
# Tmux basics
tmux new -s mysession # Create new session
tmux attach -t mysession # Attach to session
tmux detach # Detach from session (Ctrl+b, d)
tmux ls # List sessions
tmux kill-session -t mysession # Kill session
# Tmux key bindings (prefix: Ctrl+b)
# c New window
# n/p Next/previous window
# % Split vertical
# " Split horizontal
# o Switch pane
# x Kill pane
# [ Scroll mode (q to quit)
# Screen basics
screen -S mysession # Create new session
screen -r mysession # Reattach
screen -ls # List sessions
# Detach: Ctrl+a, d
# --- 2. SHELL CUSTOMIZATION ---
# .bashrc / .zshrc essentials
export HISTSIZE=100000
export HISTFILESIZE=200000
export HISTCONTROL=ignoredups:erasedups
export EDITOR=vim
export VISUAL=vim
# Useful aliases
alias ll='ls -alF --color=auto'
alias la='ls -A --color=auto'
alias l='ls -CF --color=auto'
alias ..='cd ..'
alias ...='cd ../..'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
alias h='history'
alias ports='netstat -tulanp'
alias meminfo='free -h -l -t'
alias df='df -Th'
alias du='du -sh'
# Git aliases
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git pull'
alias gd='git diff'
alias gco='git checkout'
alias gb='git branch'
alias glo='git log --oneline --graph'
# --- 3. REMOTE SERVER MANAGEMENT ---
# SSH essentials
ssh user@hostname
ssh -p 2222 user@hostname # Custom port
ssh -i ~/.ssh/key.pem user@host # Key-based auth
ssh -A user@host # Agent forwarding
ssh -L 8080:localhost:80 user@host # Port forwarding
ssh -J jumpuser@jumphost user@target # Jump host
# SSH config (~/.ssh/config)
# Host myserver
# HostName 192.168.1.100
# User admin
# Port 2222
# IdentityFile ~/.ssh/mykey
# ServerAliveInterval 60
# ServerAliveCountMax 3
# SSH agent
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa
ssh-add -l # List keys
# --- 4. CONTAINER TOOLS ---
# Docker shortcuts
alias d='docker'
alias dc='docker compose'
alias dps='docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"'
alias dlogs='docker logs -f'
alias dstats='docker stats'
alias dprune='docker system prune -af'
alias dexec='docker exec -it'
# Kubernetes shortcuts
alias k='kubectl'
alias kg='kubectl get'
alias kd='kubectl describe'
alias kdel='kubectl delete'
alias kaf='kubectl apply -f'
alias klogs='kubectl logs -f'
alias kexec='kubectl exec -it'
alias kctx='kubectl config current-context'
alias kns='kubectl config set-context --current --namespace'
# Watch resources
watch -n 1 kubectl get pods
watch -n 1 docker ps
# --- 5. LOG MONITORING ---
# tail / journalctl
tail -f /var/log/syslog
tail -f /var/log/nginx/access.log | grep ERROR
journalctl -u myservice -f
journalctl --since "1 hour ago"
journalctl -u myservice --since today --until "2 hours ago"
# Multi-file log tailing
multitail /var/log/nginx/access.log /var/log/nginx/error.log
# --- 6. PROCESS MANAGEMENT ---
# Process monitoring
ps aux | grep process_name
pgrep -f process_name
pkill -f process_name
lsof -i :8080 # What's using port 8080
fuser -k 8080/tcp # Kill process on port
# System resources
htop # Interactive process viewer
iotop # I/O monitoring
nload # Network load
iftop # Network connections
dstat --cpu --mem --net --disk # System stats
sar -u 1 10 # CPU usage every 1s, 10 times
# --- 7. FILE OPERATIONS ---
# rsync for transfers
rsync -avz --progress /local/path user@host:/remote/path
rsync -avz --delete /source/ /dest/ # Sync with deletion
# tar archiving
tar -czvf archive.tar.gz /path/to/dir # Create
tar -xzvf archive.tar.gz # Extract
tar -tzvf archive.tar.gz # List contents
# find
find /var/log -name "*.log" -mtime +7 -delete
find . -type f -size +100M
find / -type f -newer /tmp/reference_file
# --- 8. NETWORK TOOLS ---
# Quick checks
curl -I https://example.com # Headers only
curl -o /dev/null -s -w "%{http_code}\n" https://example.com
nc -zv host port # Port check
ss -tuln # Listening sockets
ss -tulnp # With process names
dig +short example.com # DNS lookup
traceroute example.com # Route trace
mtr example.com # Advanced traceroute
# --- 9. TEXT PROCESSING ---
# jq for JSON
cat file.json | jq '.items[] | {name: .name, status: .status}'
kubectl get pods -o json | jq '.items[].metadata.name'
# yq for YAML
yq '.services.web.ports' docker-compose.yml
yq -i '.services.web.image = "nginx:latest"' docker-compose.yml
# Common pipelines
grep ERROR /var/log/app.log | awk '{print $1, $2, $5}' | sort | uniq -c | sort -rn
ps aux | awk '{print $2, $4, $11}' | sort -k2 -rn | head -20
# --- 10. SAFETY & BEST PRACTICES ---
# Safer rm
alias rm='rm -i'
# Or use trash-cli
# alias rm='trash-put'
# Confirm destructive operations
set -o noclobber # Prevent > overwrite
shopt -s histverify # Verify history expansion
# Exit on error in scripts
set -euo pipefail
# Dry runs
rsync -avzn --dry-run /source/ /dest/ # rsync dry run
terraform plan # Preview changes
kubectl diff -f manifest.yaml # Preview k8s changes