终端与 Shell

Learn~35 minutes先修要求: Phase 0, Lesson 01
终端是 AI 工程师日常工作的主战场。在这里要变得得心应手。
学习目标
  • 使用管道、重定向与 grep 在命令行过滤和处理训练日志
  • 创建持久的 tmux 会话,用多个窗格同时运行训练与 GPU 监控
  • 使用 htop、nvtop 与 nvidia-smi 监控系统与 GPU 资源
  • 使用 SSH、scp 与 rsync 在本地与远程机器之间传输文件

问题

你在终端里待的时间会比任何编辑器都长。训练运行、GPU 监控、日志追踪、远程 SSH 会话、环境管理。每个 AI 工作流都离不开 shell。如果这里慢,你处处都慢。

本课覆盖 AI 工作中真正重要的终端技能。不讲 Unix 历史,不深究 Bash 脚本,只讲你需要用到的。

概念

graph TD
    subgraph tmux["tmux session: training"]
        subgraph top["Top row"]
            P1["Pane 1: Training run<br/>python train.py<br/>Epoch 12/100 ..."]
            P2["Pane 2: GPU monitor<br/>watch -n1 nvidia-smi<br/>GPU: 78% | Mem: 14/24G"]
        end
        P3["Pane 3: Logs + experiments<br/>tail -f logs/train.log | grep loss"]
    end

三件事同时运行,却只有一个终端。你可以 detach、回家、SSH 回来、再 attach。训练继续跑。

s0-shell-pipeline静态版本未包含此交互图

构建它

步骤 1:了解你的 shell

查看当前运行的 shell:

bashecho $SHELL

大多数系统使用 bash 或 zsh。两者都行。本课程的命令在两种 shell 中都适用。

需要知道的关键操作:

bash# Move around
cd ~/projects/ai-engineering-from-scratch
pwd
ls -la

# History search (most useful shortcut you'll learn)
# Ctrl+R then type part of a previous command
# Press Ctrl+R again to cycle through matches

# Clear terminal
clear   # or Ctrl+L

# Cancel a running command
# Ctrl+C

# Suspend a running command (resume with fg)
# Ctrl+Z

步骤 2:管道与重定向

管道把多个命令连接在一起。这就是处理日志、过滤输出、串联工具的方式。你会频繁用到。

bash# Count how many times "loss" appears in a log
cat train.log | grep "loss" | wc -l

# Extract just the loss values from training output
grep "loss:" train.log | awk '{print $NF}' > losses.txt

# Watch a log file update in real time, filtering for errors
tail -f train.log | grep --line-buffered "ERROR"

# Sort experiments by final accuracy
grep "final_accuracy" results/*.log | sort -t= -k2 -n -r

# Redirect stdout and stderr to separate files
python train.py > output.log 2> errors.log

# Redirect both to the same file
python train.py > train_full.log 2>&1

你需要知道的三种重定向:

SymbolWhat it does
>Write stdout to file (overwrite)
>>Append stdout to file
2>Write stderr to file
2>&1Send stderr to same place as stdout
|Send stdout of one command as stdin to the next

步骤 3:后台进程

训练要跑好几个小时。你不会想一直开着终端。

bash# Run in background (output still goes to terminal)
python train.py &

# Run in background, immune to hangup (closing terminal won't kill it)
nohup python train.py > train.log 2>&1 &

# Check what's running in background
jobs
ps aux | grep train.py

# Bring a background job to foreground
fg %1

# Kill a background process
kill %1
# or find its PID and kill that
kill $(pgrep -f "train.py")

&、nohup 与 screen/tmux 的区别:

MethodSurvives terminal close?Can reattach?
command &NoNo
nohup command &YesNo (check log file)
screen / tmuxYesYes

任何超过几分钟的任务,都用 tmux。

步骤 4:tmux

tmux 让你创建持久化的多窗格终端会话。它是管理训练运行最有用的工具。

bash# Install
# macOS
brew install tmux
# Ubuntu
sudo apt install tmux

# Start a named session
tmux new -s training

# Split horizontally
# Ctrl+B then "

# Split vertically
# Ctrl+B then %

# Navigate between panes
# Ctrl+B then arrow keys

# Detach (session keeps running)
# Ctrl+B then d

# Reattach
tmux attach -t training

# List sessions
tmux ls

# Kill a session
tmux kill-session -t training

典型的 AI 工作流会话:

bashtmux new -s train

# Pane 1: start training
python train.py --epochs 100 --lr 1e-4

# Ctrl+B, " to split, then run GPU monitor
watch -n1 nvidia-smi

# Ctrl+B, % to split vertically, tail the logs
tail -f logs/experiment.log

# Now detach with Ctrl+B, d
# SSH out, go get coffee, come back
# tmux attach -t train

步骤 5:使用 htop 与 nvtop 监控

bash# System processes (better than top)
htop

# GPU processes (if you have NVIDIA GPU)
# Install: sudo apt install nvtop (Ubuntu) or brew install nvtop (macOS)
nvtop

# Quick GPU check without nvtop
nvidia-smi

# Watch GPU usage update every second
watch -n1 nvidia-smi

# See which processes are using the GPU
nvidia-smi --query-compute-apps=pid,name,used_memory --format=csv

你会用到的 htop 快捷键:

  • F6 或 >:按列排序(按内存排序可发现内存泄漏)
  • F5:切换树状视图(查看子进程)
  • F9:结束进程
  • /:搜索进程名

步骤 6:通过 SSH 连接远程 GPU 机器

租用云 GPU(Lambda、RunPod、Vast.ai)时,你会通过 SSH 连接。

bash# Basic connection
ssh user@gpu-box-ip

# With a specific key
ssh -i ~/.ssh/my_gpu_key user@gpu-box-ip

# Copy files to remote
scp model.pt user@gpu-box-ip:~/models/

# Copy files from remote
scp user@gpu-box-ip:~/results/metrics.json ./

# Sync a whole directory (faster for many files)
rsync -avz ./data/ user@gpu-box-ip:~/data/

# Port forward (access remote Jupyter/TensorBoard locally)
ssh -L 8888:localhost:8888 user@gpu-box-ip
# Now open localhost:8888 in your browser

# SSH config for convenience
# Add to ~/.ssh/config:
# Host gpu
#     HostName 192.168.1.100
#     User ubuntu
#     IdentityFile ~/.ssh/gpu_key
#
# Then just:
# ssh gpu

步骤 7:AI 工作中实用的别名

把这些加到你的 ~/.bashrc 或 ~/.zshrc:

bashsource phases/00-setup-and-tooling/10-terminal-and-shell/code/shell_aliases.sh

或者直接复制你需要的。核心别名:

bash# GPU status at a glance
alias gpu='nvidia-smi --query-gpu=index,name,utilization.gpu,memory.used,memory.total,temperature.gpu --format=csv,noheader'

# Kill all Python training processes
alias killtraining='pkill -f "python.*train"'

# Quick virtual environment activate
alias ae='source .venv/bin/activate'

# Watch training loss
alias watchloss='tail -f logs/*.log | grep --line-buffered "loss"'

完整别名集合见 code/shell_aliases.sh。

步骤 8:AI 终端常见模式

这些在实践中反复出现:

bash# Run training, log everything, notify when done
python train.py 2>&1 | tee train.log; echo "DONE" | mail -s "Training complete" you@email.com

# Compare two experiment logs side by side
diff <(grep "accuracy" exp1.log) <(grep "accuracy" exp2.log)

# Find the largest model files (clean up disk space)
find . -name "*.pt" -o -name "*.safetensors" | xargs du -h | sort -rh | head -20

# Download a model from Hugging Face
wget https://huggingface.co/model/resolve/main/model.safetensors

# Untar a dataset
tar xzf dataset.tar.gz -C ./data/

# Count lines in all Python files (see how big your project is)
find . -name "*.py" | xargs wc -l | tail -1

# Check disk space (training data fills disks fast)
df -h
du -sh ./data/*

# Environment variable check before training
env | grep -i cuda
env | grep -i torch

使用它

本课程中,每种工具都会在以下场景派上用场:

ToolWhen you use it
tmuxEvery training run (Phases 3+)
tail -f + grepMonitoring training logs
nohup / &Quick background tasks
htop / nvtopDebugging slow training, OOM errors
SSH + rsyncWorking on cloud GPUs
Piping + redirectsProcessing experiment results
AliasesSaving time on repetitive commands

练习

  1. 安装 tmux,创建一个三窗格会话,分别运行 htop、watch -n1 date 和一个 Python 脚本。detach 后再 attach。
  2. 把 code/shell_aliases.sh 中的别名加到你的 shell 配置,然后执行 source ~/.zshrc(或 ~/.bashrc)重新加载。
  3. 用 for i in $(seq 1 100); do echo "epoch $i loss: $(echo "scale=4; 1/$i" | bc)"; sleep 0.1; done > fake_train.log 生成一条假训练日志,然后用 grep、tail 和 awk 只提取 loss 值。
  4. 为你可访问的服务器设置一条 SSH config 条目(或用 localhost 练习语法)。

提示

练习 1. 先用 tmux new -s practice 启动,然后按前缀 Ctrl+B 再按 " 和 % 拆分,用 Ctrl+B 加方向键移动。按 Ctrl+B 再按 d detach,运行 tmux ls,再用 tmux attach -t practice 连接;三个程序都必须仍在运行。

练习 2. 把 code/shell_aliases.sh 的内容复制到 ~/.zshrc 或 ~/.bashrc 末尾,然后 source 该文件。运行 alias 或 type <name> 查看某个新别名,确认 shell 已识别。

练习 3. 先运行 tail -5 fake_train.log 查看行格式。然后用 grep loss 过滤,并用 awk '{print $NF}' 只打印最后一列;输出应是 100 个逐渐变小的数字。

练习 4. 把课中的 Host gpu 块复制到 ~/.ssh/config,把 HostName 和 User 改成你的服务器,或改成 localhost 和用户名。运行 ssh -G gpu 查看 SSH 将使用的设置。如果有可连接且接受密钥的 SSH 服务器,再运行 ssh gpu 连接。

关键术语

TermWhat people sayWhat it actually means
Shell"The terminal"The program that interprets your commands (bash, zsh, fish)
tmux"Terminal multiplexer"A program that lets you run multiple terminal sessions inside one window, and detach/reattach
Pipe"The bar thing"The | operator that sends one command's output as input to another
PID"Process ID"A unique number assigned to every running process, used to monitor or kill it
nohup"No hangup"Runs a command immune to the hangup signal, so closing the terminal won't kill it
SSH"Connecting to the server"Secure Shell, an encrypted protocol for running commands on a remote machine

“Stay hungry, stay foolish.”

— Steve Jobs

esc
↑↓切换↵打开esc关闭