Linux常用命令

grep

-i 忽略大小写
-v 忽略某个关键词
-A 10 匹配关键词后10行
-B 10 匹配关键词前10行
-C 10 匹配关键词前后10行
-m 10 只显示10行
-n 显示行号
-r 当前目录及往后子目录
-w 单词搜索模式,两边不能有其他字母
-e 用于指定单个模式进行匹配。多个-e指定多个模式
-E 正则表达式

# 搜索当前目录下包含listen关键词的文件
grep -rnw listen

# 搜索当前目文件 包含listen或者port的行
grep -e listen -e port file.txt
grep -E 'listen|port' file.txt

查看TCP各状态连接的数量

ESTABLISHED 123
TIME_WAIT 45
CLOSE_WAIT 12

...

netstat -nt | awk '{wait[$NF]++}END{for(i in wait) print i,wait[i]}'
查看TIME_WAIT
netstat -n | awk '/^tcp/ {++y[$NF]} END {for(w in y) print w, y[w]}'
查看CLOSE_WAIT
netstat -an|grep CLOSE_WAIT|wc -l
查看TCP连接数
netstat -anp |grep tcp |wc -l
查看TCP初始状态
netstat -s |grep -E 'listen| resets sent| LISTEN'
sed批量重命名

将文件名包含2024-01-09改成2024-01-03

 for name in *.csv; do mv "$name" "$(echo $name | sed 's/2024-01-09/2024-01-03/')"; done
PV分析

查看nginx 访问数量

wc -l access.log 
PV分组

nginx 统计每天访问量

awk '{print substr($4,2,11)}' access.log | sort | uniq -c 
UV分析

nginx 统计唯一用户访问量

awk '{print $1}' access.log | sort | uniq | wc -l 
文本操作

简单文本操作

gedit hello.txt  # 创建一个hello.txt文件
cat hello.txt    # 看hello.txt 里的内容(tac倒看)
vi hello.txt      # 编辑hello.txt 的内容
nl hello.txt      # 显示看hello.txt里的内容并且显示行号
more hello.txt    # 一页页显示内容(less相识,但是可以往前翻页)
wc -l hello.txt    # 查看总行数
tail -n 5 hello.txt  # 查看最后5行
top -n 5 hello.txt   # 查看头5行
touch -t '2 years old' hello.txt    # 修改文档时间
chattr +iA filename    # 给文档加权限
lsattr filename      # 查看增加的权限
find / -mtime 3       # 查找三天前有改动的文件
压缩、解压文件
gzip -v hello.txt   # 压缩hello文件 后缀为gz文件
gzip -d hello.txt   # 解压hello文件 并删除压缩包
bzip2 -z hello.txt   # 压缩成后缀为bz2文件

tar -xvf file.tar   # 解压 tar包
tar -xzvf file.tar.gz   # 解压tar.gz

tar -xvf -f hello.tar.bz2 -C /path  # 解压到指定目录
tar -jcv -f hello.tar.bz2 /path     # 要压缩的目录
 
unrar e file.rar  # 解压rar
rar a file.rar file # 压缩

unzip -x file.zip   # 解压zip
zip -r file.zip /path  # 压缩目录
zip file.zip file1 file2  # 压缩多个文件
查看python所在的目录
whereis python
查看环境变量
echo $PATH
增加环境变量
vi ~/.bashrc  # 编辑文件

export PATH=/usr/bin/python/:usr/bin/python3:$PATH  # 追加环境变量
永久增加环境变量

增加AIRFLOW_VERSION到环境变量中

echo 'export AIRFLOW_VERSION=3.0 >>  ~/.bashrc && source ~/.bashrc
临时增加环境变量

增加AIRFLOW_VERSION到环境变量中

export AIRFLOW_VERSION=3.0
查看磁盘文件大小

从大到小排序

du -h|sort -rh|head
查看磁盘情况
df -h
查看内存情况
free -h
其他
# rz/sz 本地上传下载文件
rz ----> 回车 ----> 弹出选择框 ----> 选择上传的文件 ----> 点击打开 ----> 成功上传
sz file1 file2 file3 ...---->回车---->弹出窗口---->选择下载的目录---->点击确定

# 远程传输
scp -P 2222 user@host:directory/SourceFile TargetFile

# 远程登录
ssh user@host -p 2222

# 实时监控多个文件
multitail file1 file2 file3

# screen会话管理
screen -S singletask  # 创建会话
ctrl+a+d  # 退出当前窗口会话
screen -ls  # 列出当前所有会话
screen -r id或者名称重新连接会话  # 进入会话
screen -wipe  # 清理dead会话

# 系统分析
top
htop
vmstat
free -h
df -h
du -h
iotop
lsof
iostat
iftop  # 网络流量分析
netstat
ss
badblocks

posted @ 2018-12-15 11:21  TY520  阅读(23899)  评论(0编辑  收藏  举报