正在加载,请稍候…

Linux Terminal Productivity: Commands, Shortcuts, and Tools Every Dev Needs

Master the Linux terminal: essential commands, shell shortcuts, tmux sessions, file manipulation, process management, and productivity tools like fzf, ripgrep, and zsh plugins.

Why Terminal Mastery Still Matters

With polished IDEs and GUI tools, you might wonder why terminal skills still matter. The answer: speed and power. A developer comfortable in the terminal can accomplish in 5 seconds what takes 30 seconds in a GUI. And many server environments have no GUI at all.

Essential Navigation

# Smart navigation
cd -           # Jump to previous directory
cd ~           # Home directory
pushd /path    # Push directory to stack and cd there
popd           # Pop and cd back

# List efficiently
ls -lahF       # -l=long format, -a=hidden, -h=human sizes, -F=type indicators
ls -lt         # Sort by modification time (newest first)

# Find files fast
find . -name "*.ts" -newer package.json  # Modified after package.json
find . -type f -size +1M                  # Files larger than 1MB
find . -name "*.log" -mtime +7 -delete   # Delete logs older than 7 days

Text Processing Powertools

# grep — search file contents
grep -r "TODO" src/           # Recursive search
grep -rn "pattern" .          # Show line numbers
grep -l "pattern" *.ts        # Show only filenames with match
grep -v "pattern" file.txt    # Lines NOT matching (invert)

# ripgrep (rg) — much faster grep
rg "TODO" src/                # Same but faster, respects .gitignore
rg "pattern" -t ts            # Only TypeScript files
rg "TODO" --glob "!*.test.ts" # Exclude test files

# sed — stream editor
sed 's/oldtext/newtext/g' file.txt        # Replace in stdout
sed -i 's/localhost/production.host/g' config.env  # Edit in place
sed -n '10,20p' bigfile.txt               # Print lines 10-20

# awk — column-based processing
ps aux | awk '{print $2, $11}'            # Print PID and command
cat data.csv | awk -F',' '{print $1, $3}' # Print columns 1 and 3
awk '/pattern/ {count++} END {print count}' file.txt  # Count matching lines

# jq — JSON processing (essential!)
cat data.json | jq '.users[].name'        # Extract names from array
cat response.json | jq '.data | length'   # Array length
curl api.example.com | jq '.[] | select(.active == true)'

Process Management

# Find and kill processes
ps aux | grep node             # Find node processes
pgrep -f "npm run dev"         # Get PID by command
pkill -f "npm run dev"         # Kill by command pattern

lsof -i :3000                  # What's using port 3000?
kill -9 $(lsof -ti:3000)       # Kill process on port 3000

# Background processes
./server.js &                  # Run in background
nohup ./server.js &            # Run in background, survive logout
jobs                           # List background jobs
fg %1                          # Bring job 1 to foreground
bg %1                          # Resume job 1 in background

# Resource monitoring
top                            # Real-time process view
htop                           # Better top (install separately)
iotop                          # Disk I/O by process
nethogs                        # Network usage by process

tmux: Terminal Multiplexer

tmux lets you have multiple terminals in one SSH session and survive disconnections:

# Session management
tmux new -s myproject          # Create named session
tmux attach -t myproject       # Reattach to session
tmux ls                        # List sessions
tmux kill-session -t myproject

# Inside tmux (Ctrl+b is the prefix key)
Ctrl+b c     # New window
Ctrl+b n/p   # Next/previous window
Ctrl+b 1-9   # Switch to window by number
Ctrl+b ,     # Rename window

Ctrl+b %     # Split vertically
Ctrl+b "     # Split horizontally
Ctrl+b arrows # Navigate between panes
Ctrl+b z     # Zoom/unzoom current pane
Ctrl+b d     # Detach (session keeps running!)

# Scroll mode
Ctrl+b [     # Enter scroll mode (q to exit)
# ~/.tmux.conf — useful config
set -g mouse on                  # Enable mouse
set -g status-style bg=black,fg=white
set -g default-terminal "screen-256color"

# Better key bindings
bind | split-window -h           # Split with | instead of %
bind - split-window -v           # Split with - instead of "
bind r source-file ~/.tmux.conf  # Reload config

Shell Productivity

# History shortcuts
Ctrl+r          # Reverse search through history
!!              # Repeat last command
!$              # Last argument of previous command
!*              # All arguments of previous command
Ctrl+u          # Clear line
Ctrl+a/e        # Jump to beginning/end of line
Alt+b/f         # Jump word backward/forward

# Useful one-liners
# Run last command as root
sudo !!

# Quick backup
cp file.txt{,.bak}      # Creates file.txt.bak

# Create directory and cd into it
mkdir -p new/nested/dir && cd $_

# Run command on all files
for f in *.ts; do echo "Processing $f"; done

# Time a command
time npm run build

fzf: Fuzzy Finder (Game Changer)

# Install
brew install fzf
$(brew --prefix)/opt/fzf/install  # Install key bindings and completions

# After install:
Ctrl+r          # Fuzzy search through command history
Ctrl+t          # Fuzzy search files (insert into command line)
Alt+c           # Fuzzy cd into directories

# In scripts
file=$(fzf)             # Select a file interactively
process=$(ps aux | fzf | awk '{print $2}')
kill -9 "$process"      # Kill selected process

# Preview files
fzf --preview 'cat {}'
fzf --preview 'bat --color=always {}'  # bat = prettier cat

# Git with fzf
# Fuzzy checkout branch
git checkout $(git branch | fzf)
# Fuzzy show commit
git show $(git log --oneline | fzf | awk '{print $1}')

SSH Tips

# ~/.ssh/config — avoid typing long commands
Host myserver
    HostName 192.168.1.100
    User deploy
    IdentityFile ~/.ssh/id_ed25519
    ServerAliveInterval 60    # Prevent timeout

Host prod-bastion
    HostName bastion.example.com
    User ec2-user
    ForwardAgent yes          # Forward SSH key to remote

Host prod-api
    HostName 10.0.0.5         # Private IP
    User ubuntu
    ProxyJump prod-bastion    # Jump through bastion!

# Usage: just type:
ssh myserver
ssh prod-api  # Automatically jumps through bastion

# SSH tunneling
ssh -L 5432:localhost:5432 myserver  # Access remote postgres locally
ssh -L 8080:internal-host:80 bastion # Access internal web service

File Manipulation

# rsync — better than cp for large operations
rsync -avz --progress source/ dest/    # Sync with progress
rsync -avz --exclude="node_modules" src/ user@server:/deploy/

# tar — archive and compress
tar czf archive.tar.gz directory/     # Create
tar xzf archive.tar.gz                # Extract
tar tzf archive.tar.gz                # List contents

# Disk usage
du -sh *                              # Size of each item in current dir
du -sh */ | sort -h                   # Sort by size
df -h                                 # Disk space on mounted volumes
ncdu                                  # Interactive disk usage (install separately)

# Quick file operations
wc -l file.txt                        # Count lines
sort file.txt | uniq -c | sort -rn    # Count occurrences, sort by frequency
head/tail -n 20 file.txt              # First/last 20 lines
tail -f /var/log/app.log              # Follow a log file live

→ Calculate Linux file permissions with the chmod Calculator.