Linux 常用命令入门——开发者必会的 30 个命令
Linux 常用命令入门——开发者必会的 30 个命令
作者: CaoZH
日期: 2022-08-15
本文为原创教程
无论是后端开发、运维还是前端开发者,Linux 命令都是绕不开的基本功。本文整理了日常开发中最常用的 30 个 Linux 命令,按功能分类,搭配示例,帮你快速上手。
一、文件管理
1. ls — 列出文件和目录
1 2 3 4 5
| ls ls -l ls -a ls -lh ls -lt
|
2. cd — 切换目录
1 2 3 4
| cd /home cd ~ cd - cd ..
|
3. pwd — 显示当前路径
4. mkdir — 创建目录
1 2
| mkdir mydir mkdir -p a/b/c
|
5. cp — 复制文件/目录
1 2 3
| cp file.txt backup/ cp -r src/ dest/ cp -a /data /data_backup
|
6. mv — 移动/重命名
1 2 3
| mv file.txt /tmp/ mv old.txt new.txt mv -i file.txt dest/
|
7. rm — 删除
1 2 3 4
| rm file.txt rm -r dir/ rm -f file.txt rm -rf dir/
|
8. find — 查找文件
1 2 3
| find . -name "*.log" find /var -size +100M find . -mtime -7
|
9. grep — 搜索文件内容
1 2 3 4 5 6
| grep "error" app.log grep -r "TODO" src/ grep -i "warning" log.txt grep -n "exception" app.log grep -c "ERROR" app.log grep "error" app.log | grep -v "timeout"
|
二、文本处理
10. cat — 查看文件内容
1 2
| cat file.txt cat -n file.txt
|
11. tail/head — 查看文件开头/结尾
1 2 3
| tail -f app.log tail -n 100 app.log head -n 20 app.log
|
12. less — 分页查看
1 2
| less app.log less -N app.log
|
13. vim/nano — 文本编辑
1 2
| nano config.yml vim config.yml
|
三、系统管理
14. ps — 查看进程
1 2 3
| ps aux ps aux | grep nginx ps -ef
|
15. top — 实时系统监控
16. df/du — 磁盘管理
1 2 3
| df -h du -sh /home du -h --max-depth=1
|
17. systemctl — 服务管理
1 2 3 4 5 6
| systemctl start nginx systemctl stop nginx systemctl restart nginx systemctl status nginx systemctl enable nginx systemctl disable nginx
|
18. chmod — 修改权限
1 2 3
| chmod 755 script.sh chmod +x script.sh chmod -R 644 /var/www
|
四、网络相关
19. curl — HTTP 请求
1 2 3 4 5 6
| curl https://api.example.com curl -X POST -d '{"key":"value"}' \ -H "Content-Type: application/json" \ https://api.example.com/create curl -I https://example.com curl -o file.zip https://example.com/file.zip
|
20. wget — 下载文件
1 2
| wget https://example.com/file.zip wget -c https://example.com/big.zip
|
21. netstat/ss — 查看网络连接
1 2
| ss -tlnp netstat -tulpn
|
22. ping — 网络连通性测试
五、压缩与归档
23. tar — 打包压缩
1 2 3 4
| tar -czf archive.tar.gz /path tar -xzf archive.tar.gz tar -cjf archive.tar.bz2 /path tar -xf archive.tar.gz -C /dest
|
24. zip/unzip — ZIP 操作
1 2 3
| zip -r archive.zip dir/ unzip archive.zip unzip archive.zip -d /dest
|
六、实用工具
25. ssh — 远程连接
1 2 3 4
| ssh user@192.168.1.100 ssh -p 2222 user@host ssh-keygen -t rsa -b 4096 ssh-copy-id user@host
|
26. scp — 远程复制
1 2 3
| scp file.txt user@host:/path/ scp user@host:/path/file.txt ./ scp -r user@host:/remote/dir ./
|
27. alias — 命令别名
1 2 3 4
| alias ll='ls -lah' alias gs='git status' alias gc='git commit -m'
|
28. history — 命令历史
1 2 3 4
| history history | grep docker !! !100
|
七、高频组合用法
1 2 3 4 5 6 7 8 9 10 11
| tail -f app.log | grep ERROR
find / -type f -exec du -h {} + 2>/dev/null | sort -rh | head -10
for f in *.txt; do mv "$f" "${f%.txt}.md"; done
find . -name "*.java" | xargs wc -l | tail -1
|
总结
记住这些命令不需要死记硬背,多用几次就熟悉了。建议把常用的 alias 写入 ~/.bashrc,效率翻倍。
下一步: 学习 Shell 脚本编程,把重复操作自动化。
首发于 CaoZH 的笔记