在 Linux 系统上,前端开发者常用的命令主要集中在 文件操作、压缩解压、网络请求、版本控制、进程管理和调试工具 等方面。以下是一些高频命令和场景:
一、文件与目录操作
1. 查看文件内容
# 查看文本文件(自动换行)
cat file.txt
# 查看文件末尾内容
tail -f file.log # 实时跟踪日志
# 查看文件开头内容
head -n 5 file.txt # 显示前5行
# 分页查看文件内容
less file.txt # 可上下滚动,按 Q 退出
2. 文件搜索
# 在当前目录递归搜索关键字
grep -r "search_term" ./
# 查找文件类型(例如 JavaScript 文件)
find . -name "*.js"
3. 压缩与解压
# 压缩文件(Tar)
tar -czvf archive.tar.gz /path/to/files
# 解压 Tar 文件
tar -xzvf archive.tar.gz
# 压缩为 ZIP
zip -r archive.zip /path/to/files
# 解压 ZIP
unzip archive.zip
二、网络相关工具
1. HTTP 请求测试
# 发送 GET 请求
curl -I https://api.example.com/data # 查看响应头
# 发送 POST 请求(带 JSON 数据)
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com/submit
# 下载文件
wget https://example.com/file.zip
2. 检查网络连通性
# 测试域名解析
dig example.com
# 检查端口开放状态
telnet example.com 80 # 或使用 nmap
nmap -p 80,443 example.com # 扫描开放端口
三、版本控制(Git)
1. 基础操作
# 克隆仓库
git clone https://github.com/user/repo.git
# 提交代码
git add .
git commit -m "Update README"
git push origin main
# 拉取最新代码
git pull origin main
2. 分支管理
# 创建并切换分支
git checkout -b feature/new-feature
# 合并分支
git merge feature/new-feature
四、进程与调试
1. 查看运行中的进程
# 查看所有进程(按内存排序)
ps aux --sort=-%mem
# 查看特定进程(例如 Node.js)
pgrep -f "node.js"
# 终止进程
kill -9 PID # 强制终止进程(PID 为进程号)
2. 前端调试工具
Chrome DevTools 远程调试:
# 启动 Chrome 并开启远程调试 google-chrome --remote-debugging-port=9222
Node.js 调试:
node inspect app.js # 启动调试会话
五、性能优化与构建
1. 压缩资源
# 使用 Gzip 压缩文件
gzip -k file.js # 压缩后保留原文件(-k)
# 使用 Webpack/Terser 等工具自动压缩代码
# (需提前配置构建脚本)
2. 监控性能
# 分析 Lighthouse 性能评分
lighthouse https://example.com --output=report.html
# 分析 Web Vitals
web-vitals https://example.com
六、实用工具
1. 文本处理
# 替换文本
sed -i 's/old_text/new_text/g' file.txt
# 分割文件
split -l 1000 large_file.txt chunk_
2. 管理服务(如 Nginx 或 PM2)
# 查看 Nginx 状态
systemctl status nginx
# 用 PM2 管理 Node.js 应用
pm2 start app.js # 启动应用
pm2 list # 查看进程列表
pm2 restart app # 重启应用
七、安全相关
1. 生成 SSH 密钥
ssh-keygen -t ed25519 -C "your_email@example.com"
2. 检查文件权限
# 查看文件权限
ls -l file.txt
# 修改权限(例如 755)
chmod 755 file.txt
八、其他高频命令
命令 | 用途 |
---|---|
pwd | 显示当前目录 |
cp file.txt dir/ | 复制文件到目录 |
mv file.txt new.txt | 重命名文件 |
rm -rf folder/ | 强制删除目录(谨慎使用) |
touch file.txt | 创建空文件 |
chmod +x script.sh | 赋予脚本执行权限 |
附录:推荐工具
- 文件管理:
ncdu
(分析磁盘占用)、tree
(可视化目录结构) - 代码编辑:
VS Code
、Sublime Text
- 包管理:
npm
/yarn
/pnpm
- 日志分析:
grep
、awk
、logrotate
根据具体需求选择工具,建议结合 Shell 配置(如 .zshrc
)提升效率!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。