作为后端开发,Linux 命令是必备技能。
今天整理最常用的命令,建议收藏。
文件与目录
目录操作
# 查看当前目录pwd# 切换目录cd /var/wwwcd ~ # 回到家目录cd - # 回到上一个目录cd .. # 上级目录# 列出文件lsls -l # 详细信息ls -la # 包含隐藏文件ls -lh # 人性化显示大小ls -lt # 按时间排序# 创建目录mkdir mydirmkdir -p a/b/c # 递归创建# 删除目录rmdir mydir # 只能删空目录rm -rf mydir # 强制递归删除(危险!)
文件操作
# 创建文件touch file.txtecho"hello" > file.txt # 覆盖写入echo"world" >> file.txt # 追加写入# 复制cp file.txt file2.txtcp -r dir1 dir2 # 递归复制目录# 移动/重命名mv file.txt newname.txtmv file.txt /tmp/# 删除rm file.txtrm -f file.txt # 强制删除rm -rf dir/ # 递归删除目录# 查看文件cat file.txt # 全部内容head -n 20 file.txt # 前 20 行tail -n 20 file.txt # 后 20 行tail -f file.txt # 实时查看(看日志)less file.txt # 分页查看
查找文件
# find 命令find /var -name "*.log"# 按名称find /var -name "*.log" -mtime -7 # 7 天内修改find /var -size +100M # 大于 100Mfind /var -type f -name "*.php"# 只找文件# locate 命令(更快,需要更新数据库)updatedblocate nginx.conf# which/whereiswhich php # 查找命令位置whereis nginx # 查找程序相关文件
文本处理
grep 搜索
# 基本搜索grep "error" app.loggrep -i "error" app.log # 忽略大小写grep -n "error" app.log # 显示行号grep -r "TODO" ./src # 递归搜索目录grep -v "debug" app.log # 排除匹配行grep -c "error" app.log # 统计匹配数# 正则表达式grep -E "error|warning" app.loggrep "^2024" app.log # 以 2024 开头grep "php$" file.txt # 以 php 结尾
awk 处理
# 打印指定列awk '{print $1}' file.txt # 第一列awk '{print $1, $3}' file.txt # 第一和第三列awk -F: '{print $1}' /etc/passwd # 指定分隔符# 条件过滤awk '$3 > 100 {print $0}' file.txtawk '/error/ {print $0}' app.log# 统计awk '{sum += $1} END {print sum}' file.txtawk 'END {print NR}' file.txt # 行数
sed 替换
# 替换sed 's/old/new/' file.txt # 替换第一个sed 's/old/new/g' file.txt # 替换所有sed -i 's/old/new/g' file.txt # 直接修改文件# 删除sed '/pattern/d' file.txt # 删除匹配行sed '1d' file.txt # 删除第一行sed '1,5d' file.txt # 删除 1-5 行# 插入sed '1i\new line' file.txt # 在第一行前插入sed '1a\new line' file.txt # 在第一行后插入
系统信息
系统状态
# 系统信息uname -a # 内核信息cat /etc/os-release # 系统版本hostname # 主机名uptime # 运行时间# CPUcat /proc/cpuinfolscpunproc # CPU 核数# 内存free -hcat /proc/meminfo# 磁盘df -h # 磁盘使用du -sh /var/* # 目录大小du -sh * | sort -hr # 按大小排序
进程管理
# 查看进程ps auxps aux | grep nginxps -ef | grep php# 实时监控tophtop # 更好用,需安装# 杀死进程kill PIDkill -9 PID # 强制杀死killall nginx # 按名称杀死pkill -f "php artisan"# 后台运行nohup php script.php &nohup php script.php > output.log 2>&1 &
网络相关
网络状态
# IP 地址ip addrifconfig # 旧命令# 端口监听netstat -tlnpss -tlnp # 更快# 连接状态netstat -an | grep ESTABLISHEDss -s # 统计# 路由route -nip route
网络测试
# pingping google.comping -c 4 google.com # 只 ping 4 次# 端口测试telnet 192.168.1.100 3306nc -zv 192.168.1.100 3306# DNSnslookup google.comdig google.com# 下载wget https://example.com/file.zipcurl -O https://example.com/file.zipcurl -I https://example.com # 只看响应头
权限管理
文件权限
# 查看权限ls -l file.txt# -rw-r--r-- 1 user group 1234 Jan 15 10:00 file.txt# 修改权限chmod 755 script.sh # rwxr-xr-xchmod +x script.sh # 添加执行权限chmod -R 755 dir/ # 递归修改# 修改所有者chown user:group file.txtchown -R www-data:www-data /var/www
权限数字
r = 4 (读)w = 2 (写)x = 1 (执行)755 = rwxr-xr-x (所有者全部,其他读和执行)644 = rw-r--r-- (所有者读写,其他只读)777 = rwxrwxrwx (所有人全部权限,危险!)
压缩解压
# tartar -cvf archive.tar dir/ # 打包tar -xvf archive.tar # 解包tar -czvf archive.tar.gz dir/ # 打包并压缩tar -xzvf archive.tar.gz # 解压# zipzip -r archive.zip dir/unzip archive.zipunzip archive.zip -d /target/# gzipgzip file.txt # 压缩gunzip file.txt.gz # 解压
实用技巧
管道和重定向
# 管道cat file.txt | grep "error" | wc -lps aux | grep nginx | awk '{print $2}'# 重定向command > file.txt # 覆盖command >> file.txt # 追加command 2>&1 # 错误输出到标准输出command > /dev/null 2>&1 # 丢弃所有输出
快捷键
Ctrl + C 中断命令Ctrl + Z 暂停命令Ctrl + D 退出Ctrl + L 清屏Ctrl + R 搜索历史命令Ctrl + A 光标到行首Ctrl + E 光标到行尾Tab 自动补全
总结
| |
|---|
| ls, cd, cp, mv, rm, cat, tail |
| |
| |
| |
| |
| |
下一篇我们来聊 OPcache 配置指南,零成本提升 PHP 性能。