查看文件的内容
cat:正序查看
cat access.log
tac:倒序查看
tac access.log
分页显示文件
more
more access.log
Enter显示文件下一行
空格显示下一页
F显示下一屏内容
B显示上一屏内容less
/GET 查找GET字符串
显示文件尾
tail
tail -f -n 500 access.log
-f 持续查看
-n 显示最后n行
显示文件头
- head
head -n2 access.log
内容排序
sort
sort -n -r access.log
-n 按照数字排序
-r 逆序排序sort -k 2 -t “ “ -n access.log
-k 指定排序的列
-t 指定列分隔符
-n 按照数字排序
字符统计
wc
wc -l access.log
-l 统计文件中的行数
wc -c access.log
-c 显示文件的字节数
wc -L access.log
-L得出最长的行长度
wc -w access.log
-w 查看文件包含多少单词
重复行
uniq
sort uniqfile| uniq -c
-c 用来在每一行前面加上该行出现的次数
sort uniqfile | uniq -c -u
-u 只会显示出现一次的行
sort uniqfile | uniq -c -d
-d 只会显示重复出现的行
字符串查找
grep
> grep earyant access.log earyant 为指定的查找串 > grep -c earyant access.log -c 可以显示查找到的行数 > grep 'G.\*T' access.log 支持正则表达式
文件查找
find
find /home/earyant -name access.log
在/home/earyant 目录下查找文件名为access.log的文件
find /home/earyant -name “*.txt”
find . -print
打印当前目录所有文件
whereis
whereis java
- which
which java
表达式求值
- expr
expr 10 * 3
expr 10 % 3
expr index “earyant.github.io” earyant
压缩
tar
tar -cf aaa.tar tmp1 tmp2
将当前目录下的tmp1和tmp2目录打包成aaa.tar -c 表示生成新包 -f 指定包名称
tar -tf aaa.tar
-t 能够列出包中文件的名称
tar -xf aaa.tar
-x 将打包文件解压
url 访问工具
curl
curl www.baidu.com
curl -i www.baidu.com
-i 返回带header的文档
curl -I www.baidu.com
-I 只返回页面的header信息
查看请求访问量
cat access.log | cut -f1 -d “ “ | sort | uniq -c | sort -k 1 -n -r | head -10
访问量排名前10的ip地址
cat access.log | cut -f4 -d “ “ | sort | uniq -c | sort -k 1 -n -r | head -10
页面访问量排名前10的url
查看最耗时的页面
cat access.log | sort -k 2 -n -r |head -10
统计404请求的占比
1 | export total_line = `wc -l access.log | cut -f1 -d " "` && export not_found_line = `awk '$6=='404'{print $6}' access.log | wc -l` && expr $ not_found_line \*100 / $total_line |