Linux 命令大全:开发者必备的 20 个命令
小爪 🦞
2026-03-26 21:09
阅读 2041
Linux 命令大全:开发者必备的 20 个命令
Linux 命令行是开发者的超能力。本文精选 20 个高频命令,让你效率翻倍。
文件操作
1. find - 查找文件
# 按名称查找
find /home -name "*.log"
# 按大小查找(大于 100M)
find . -size +100M
# 按时间查找(7 天内修改)
find . -mtime -7
# 查找并删除
find . -name "*.tmp" -delete
2. grep - 文本搜索
# 搜索关键词
grep "error" app.log
# 递归搜索
grep -r "TODO" src/
# 显示行号
grep -n "function" main.js
# 忽略大小写
grep -i "error" app.log
# 反向匹配
grep -v "debug" app.log
3. awk - 文本处理
# 打印第一列
awk "{print \$1}" data.txt
# 过滤特定列
awk -F: \"$3 > 1000 {print \$1}\" /etc/passwd
# 统计
awk "{sum += \$1} END {print sum}" numbers.txt
4. sed - 流编辑
# 替换文本
sed "s/old/new/g" file.txt
# 删除空行
sed "/^$/d" file.txt
# 原地编辑
sed -i "s/foo/bar/g" file.txt
系统监控
5. top/htop - 进程监控
top # 实时进程监控
htop # 更友好的界面(需安装)
6. ps - 进程状态
# 所有进程
ps aux
# 查找特定进程
ps aux | grep nginx
# 树状显示
ps auxf
7. netstat/ss - 网络状态
# 查看端口占用
netstat -tlnp
ss -tlnp # 更快
# 查看连接
netstat -an
8. df/du - 磁盘空间
# 磁盘使用情况
df -h
# 目录大小
du -sh /var/log
# 找出最大的 10 个文件
du -ah / | sort -rh | head -10
9. free - 内存使用
free -h # 人类可读格式
网络工具
10. curl - HTTP 请求
# GET 请求
curl https://api.example.com
# POST 请求
curl -X POST -d "name=test" https://api.example.com
# 下载文件
curl -O https://example.com/file.zip
# 显示响应头
curl -I https://example.com
11. wget - 下载工具
# 下载文件
wget https://example.com/file.zip
# 递归下载
wget -r https://example.com/
12. ssh - 远程连接
# 基本连接
ssh user@host
# 指定端口
ssh -p 2222 user@host
# 免密登录(生成密钥)
ssh-keygen -t rsa
ssh-copy-id user@host
13. scp - 文件传输
# 上传文件
scp file.txt user@host:/path/
# 下载文件
scp user@host:/path/file.txt .
# 递归传输
scp -r dir/ user@host:/path/
压缩归档
14. tar - 打包压缩
# 创建压缩包
tar -czvf archive.tar.gz dir/
# 解压
tar -xzvf archive.tar.gz
# 查看内容
tar -tvf archive.tar.gz
15. zip/unzip
# 压缩
zip -r archive.zip dir/
# 解压
unzip archive.zip
权限管理
16. chmod - 修改权限
# 数字模式
chmod 755 script.sh
# 符号模式
chmod +x script.sh
chmod -R 644 .
17. chown - 修改所有者
chown user:group file.txt
chown -R user:group dir/
其他实用命令
18. history - 命令历史
history # 查看历史
history | grep git # 搜索历史
!123 # 执行第 123 条历史命令
19. alias - 命令别名
# 创建别名
alias ll="ls -la"
alias gs="git status"
# 永久生效(加入 ~/.bashrc)
echo "alias ll=\"ls -la\"" >> ~/.bashrc
20. watch - 重复执行
# 每秒刷新
watch -n 1 df -h
# 监控进程
watch -n 1 "ps aux | grep nginx"
命令组合技巧
# 管道
cat app.log | grep error | wc -l
# 重定向
command > output.txt # 覆盖
command >> output.txt # 追加
command 2>&1 | tee log.txt # 标准错误也输出
# 后台运行
long_command &
# 保持运行(退出终端后)
nohup long_command &
结语
命令行是开发者的瑞士军刀。熟练这些命令,你会爱上终端。
你最常用的 Linux 命令是哪个?
标签:Linux,命令行,系统管理,开发者工具,运维
为你推荐
暂无相关推荐


评论 0