Shell命令快速参考

在 `~/.bashrc` 或 `~/.zshrc` 中添加:...

2025/05/28 21:02
11 次阅读
0 条评论
0 个点赞
📖 4 分钟阅读

博客管理员

这是博客管理员账户

Shell命令快速参考

Shell命令快速参考 🚀

⚡ 核心命令

命令功能示例
sleep n等待n秒sleep 5
curl URL发送HTTP请求curl -s http://localhost:3000
pkill pattern杀死匹配的进程pkill -f "next dev"
grep pattern搜索文本grep "error" file.log

🔗 常用组合

# 重启开发服务器
pkill -f "next dev" && sleep 2 && pnpm dev

# 测试服务器响应
curl -s http://localhost:3000 | grep "title"

# 查看进程
ps aux | grep node

# 实时监控错误
tail -f app.log | grep -i error

📋 curl常用选项

选项功能示例
-s静默模式curl -s URL
-I只获取响应头curl -I URL
-L跟随重定向curl -L URL
-o file输出到文件curl -o page.html URL
-X METHOD指定请求方法curl -X POST URL

🔍 grep常用选项

选项功能示例
-i忽略大小写grep -i "error" file
-v反向匹配grep -v "debug" file
-n显示行号grep -n "function" file
-r递归搜索grep -r "TODO" src/
-E扩展正则grep -E "(error|warning)"

⚙️ 操作符

操作符功能示例
|管道传递ps aux | grep node
&&成功后执行cmd1 && cmd2
||失败后执行cmd1 || cmd2
;顺序执行cmd1; cmd2
&后台执行cmd &

🏃‍♂️ 快捷键

快捷键功能
Ctrl+C终止命令
Ctrl+Z暂停命令
Ctrl+R搜索历史
Ctrl+L清屏
Tab自动补全
!!重复上个命令

🌐 网络调试

# 检查端口占用
lsof -i :3000
netstat -tulpn | grep :3000

# 杀死占用端口的进程
lsof -ti:3000 | xargs kill -9

# 测试网络连通性
ping google.com
curl -I https://example.com

📁 文件操作

# 查看文件内容
cat file.txt          # 显示全部内容
head -n 10 file.txt    # 显示前10行
tail -n 10 file.txt    # 显示后10行
tail -f file.txt       # 实时查看文件变化

# 文件搜索
find . -name "*.js"    # 查找JS文件
grep -r "keyword" .    # 递归搜索关键字

🔄 进程管理

# 查看进程
ps aux                 # 显示所有进程
ps aux | grep node     # 过滤node进程
top                    # 实时进程监控

# 杀死进程
kill PID              # 根据PID杀死
pkill node            # 杀死所有node进程
killall node          # 杀死所有node进程(替代方案)

📊 系统监控

# 系统资源
df -h                 # 磁盘使用情况
free -h               # 内存使用情况
top                   # CPU和内存监控
htop                  # 更好的top(需要安装)

# 网络状态
netstat -tulpn        # 查看端口监听
ss -tulpn             # 更现代的netstat

💡 开发常用别名

~/.bashrc~/.zshrc 中添加:

# 常用别名
alias ll='ls -la'
alias la='ls -A'
alias l='ls -CF'

# 开发相关
alias dev='pnpm dev'
alias build='pnpm build'
alias restart='pkill -f "next dev" && sleep 2 && pnpm dev'

# 网络相关
alias ports='lsof -i -P -n | grep LISTEN'
alias myip='curl -s ifconfig.me'

# Git相关
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'

🔧 正则表达式速查

符号含义示例
^行首grep "^Error"
$行尾grep "Error$"
.任意字符grep "a.b"
*0个或多个grep "ab*"
+1个或多个grep -E "ab+"
?0个或1个grep -E "ab?"
|grep -E "(error|warning)"
[]字符集grep "[0-9]"

📝 实际场景示例

# 开发服务器管理
pkill -f "next dev" && sleep 2 && pnpm dev

# 查看页面响应
curl -s http://localhost:3000 | head -20

# 监控错误日志
tail -f app.log | grep -i --color error

# 检查API响应
curl -X POST -H "Content-Type: application/json" \
     -d '{"test":"data"}' \
     http://localhost:3000/api/test

# 批量处理文件
find src/ -name "*.tsx" | xargs grep -l "useState"

# 系统清理
docker system prune -f
npm cache clean --force

💡 提示: 使用 man command 查看任何命令的详细手册,例如 man grep

评论