写在前面
Linux命令在日常工作使用的频率极高,今天就简简单单的总结一下作为一名程序员日常高频使用的命令。
正文开始
1. Linux基础命令
-
进程相关
ps -efUID 用户号 PID 进程ID PPID 父进程号 C CPU占用率 TTY 输入设备的文件名称 TIME 进程CPU占用时间 COMMAND 启动这个进程的命令
ps -aux
USER 用户
PID 进程ID
CPU CPU占用率
MEM 内存使用量
VSZ 如果一个程序完全驻留在内存的话需要占用多少内存空间
RSS 当前实际占用了多少内存
TTY: 终端的次要装置号码 (minor device number of tty)
STAT 进程当前的状态("S":中断 sleeping,进程在睡眠状态,表明这些进程在等待某些事件发生--可能是用户输入或者系统资源的可用性;"D":不可中断 uninterruptible sleep;"R":运行 runnable;"T":停止 traced or stopped;"Z":僵死 a defunct zombie process)
START 启动命令的时间点
TIME 进程CPU占用时间
COMMAND 启动这个进程的命令
kill
-1 终端断线
-2 中断(同 Ctrl + C)
-3 退出(同 Ctrl + \)
-9 强制终止
-15 终止
-18 继续(与STOP相反, fg/bg命令)
-19 暂停(同 Ctrl + Z)
# 进程状态
$ ps -rf
UID PID PPID C STIME TTY TIME CMD
501 60808 60807 0 3:56PM ttys002 0:00.83 -zsh
$ ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 192180 4452 ? Ss 2019 228:56 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
# 结束进程号为100的进程
kill -9 100
- 网络监控 (流量、协议、端口号等)
netstat -anp
Proto 协议类型
Recv-Q 接受队列
Send-Q 发送队列
Local Address 本地计算机地址和端口
Foreign Address 远程计算机地址和端口
State TCP连接的状态
PID/Program name 进程ID或进程名
# 查看端口流量等信息
netstat -anp
(No info could be read for "-p": geteuid()=1000 but you should be root.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN
# mac 查看端口8080
lsof -i tcp:8080
-
查找符合条件的字符串
grep-e 正则 -v 反向查找
# 查看端口8000的网络状态
netstat -anp | grep 8000
# 查看python进程的状态
ps -ef| grep python
- 远程登录 (scp ssh 部分参数共享)
# 登录服务器shell环境
ssh -p 22 root@host.example.com
# 将本地 ~/.ssh/id_rsa.pub 上传到 服务器/etc/ssh/
scp ~/.ssh/id_rsa.pub root@host.example.com:/etc/ssh/
# 服务器/etc/ssh/id_rsa.pub 下载到当前目录
scp root@host.example.com:/etc/ssh/ ./
- 文件操作 (服务器操作文件 这里介绍实用的简单的操作)
# echo > file 覆盖文件
echo "" > file # 这个操作可以快速清空某一文件
echo "Hello World" > file
# echo >> file 追加文件
echo "Hello World" >> file
# cat 配合 EOF (> 覆盖 >> 追加)
cat << EOF >> file
heredoc> 1
heredoc> 2
heredoc> 3
heredoc> EOF
# 查看日志文件
tail -f log.log
# 查看日志文件最后100行 (对于大日志文件非常有用)
tail -n 100 notes.log
# 查看日志文件从20行开始
tail -n +20 notes.log
2. Git 命令
常规命令
# 设置提交代码时的用户信息
git config [--global] user.name "[name]"
git config [--global] user.email "[email address]"
# 添加代码到暂存区
git add [file1] [file2] ...
# 提交暂存区代码到本地仓库
git commit -m [message]
# 提交本地仓库代码到远程仓库
git push
# 切换到master分支
git checkout master
# 创建develop分支
git checkout -b develop master
# 从远程创建develop分支
git checkout --track origin/develop
# 回退到上一次提交 并将提交写入暂存区
git reset --soft HEAD
submodule 命令
# 初始化项目
git submodule add <url> <path>
# 提交子模块的相关配置
git add . & git commit
# 如果需要更新子模块的代码
cd submodule & git pull
# 远程服务器上初始化
git submodule update --init --recursive
3. Docker 命令
# 启动命令 后台运行ubuntu并开启bash (自动重启 开启80端口 共享目录 自定义名称)
docker run -itd -p 80:80 --restart always -v ./opt:/opt --name u_test ubuntu /bin/bash
# 查看所有运行中的docker
docker ps -a
# 停止所有的container,这样才能够删除其中的images:
docker stop $(docker ps -a -q)
# 如果想要删除所有container的话再加一个指令:
docker rm $(docker ps -a -q)
# 启动程序
docker-compose up -d
# 重新build + 启动
docker-compose up -d --build
# 查看日志 指定容器
docker-compose logs -f --tail=100 api
4. 其他命令
# 批量杀掉python进程
ps -ef | grep python | grep -v grep | awk '{print $2}' | xargs kill -9
# 批量删除名称中包含python的镜像
docker rmi --force `docker images | grep python | awk '{print $3}'`
# 查看当前目录文件大于10M的文件
find . -type f -size +10M
# 列出当前目录文件并显示文件大小
ls -lh
# 查看当前目录文件夹大小
du -h
其他
- git fetch 与 git pull 的区别
In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD.
大致的意思就是 git pull 是 git fetch之后加入一步 merge的操作。这也就解释本地仓库的代码在有冲突的时候会需要手动处理冲突。
详情见 Stackoverflow