linux常用指令集-持续更新...

0.查看所有java进程GC情况:for i in `jps|egrep -v "Jps|Launcher" |cut -d" " -f1`;do pwdx $i; jstat -gcutil $i ;done
1.统计每个java进程的线程数:for i in `ps aux|grep java|awk '{print $2}'` ;do echo pid:$i; jstack $i |grep java.lang.Thread.State|awk '{print $2}'|sort|uniq -c;done
2.统计TCP连接状态:netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
3.统计每个IP的连接数:netstat -anp| awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr|more
4.统计每个进程的网络连接数:netstat -anp|awk '{print $7}'|awk -F/ '{print $1}'|sort|uniq -c | sort -nr|more
5.统计日志文件里接口被调用次数:grep RestChannelHandler global.log|cut -d"," -f3|cut -d"?" -f1|sort |uniq -c |sort -nr |more
grep '14/Nov/2017:09:4[0,1,2,3,4]' access.log|awk '{print $2}'|sort -nr |uniq -c
6.替换文件里指定字符串:sed -i "s/10.10.30.247/localhost/g" `grep 10.10.30.247 -rl */conf/*` 或者:find -name *.shtml|xargs perl -pi -e 's|10.10.30.247|localhost|g'
7.同步目录:rsync -av -e 'ssh -p 10007' 192.168.1.3:/root/server103 /root/
8.实时查看正在执行的sql语句: /usr/sbin/tcpdump -i eth0 -s 0 -l -w - dst port 3306 | strings | egrep -i 'SELECT|UPDATE|DELETE|INSERT|SET|COMMIT|ROLLBACK|CREATE|DROP|ALTER|CALL'
tcpdump -i em2 port 53|grep 10.59.96.211
9.删除空目录:find /data -type d -empty | xargs -exec rmdir; 删除空文件:for emp in `/usr/bin/find -size 0` ;do /bin/rm $emp;done
删除五天前的文件:find /temp -mtime +5 -type f -exec rm -rf {} \;
10.批量kill进程 : ps -efww|grep sqlr-listener|grep -v grep|cut -c 9-15|xargs kill -9
11.列出所有jvm可选参数:java -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal -version
12.同时跟踪fork和vfork出来的进程 strace -f -F -p 123
13.网络流量的分析iptraf ,dstat
14.ldd /usr/bin/mysqldump 列出使用哪些so及路径
15.测试zk: echo stat |nc localhost 2181
16.sed 删除第一个单词
sed -r 's/[a-zA-Z]+ //1' your-file
cat soa_report.txt |grep HiveClient|awk -F'size:' '{print $2}'|sed -r 's/[0-9]+//1'
删除每行开头的所有空格
sed 's/^[ ]//g' test
17.查看网络连接状态统计:ss -ant | awk 'NR>1 {++s[$1]} END {for(k in s) print k,s[k]}'
18.视频压缩
ffmpeg -i Wildlife.wmv-y -ab 32 -ar 44100 -r 15 -b 520000 -qscale 4 Wildlife_mencoder.flv
ffmpeg -i 1115.mp4 -qscale 4 1115compress.mp4
19.代码统计:find . -name "*.m" -or -name "*.h" |xargs grep -v "^$"|wc -l
代码统计:find ./ -name "*.swift"|xargs cat| wc -l
20.分组统计
如果要统计每个网址出现的次数, 实现类似于mysql group by的效果. 将wc -l 换成:
awk '{a[$1]++}END{for (j in a) print j","a[j]}'
输出格式: 分组,出现的次数
最大数/最小数/平均数统计
基于上一个例子, 如果我们提取的是一个数字, 要进行最大数/最小数/平均数的统计, 需要用到awk管道了, 将wc -l 换成:
awk '{if(min==""){min=max=$1}; if($1>max) {max=$1}; if($1< min) {min=$1}; total+=$1; count+=1} END {print total/count, min, max}'
21.慢日志查询:
cat search_test/logs/run.log_2015-09-11.log |awk '{print $4}'|awk -F: '{if($2>1000){slow+=1};count+=1} END {print count,slow}'
统计resin accesslog大于1秒的记录
zcat access_107.105.2016-07-10.log.1650.gz |awk '{if($12>1000000){slow+=1;slowtime+=$12};count+=1}END{print slow,slowtime,slowtime/slow,count}'
分钟统计:
for i in {0..5};do DT="2016:20:$i" ; zcat access_107.*|grep $DT |awk '{if($12>2000000){slow+=1;slowtime+=$12};count+=1}END{print $4,slow,slowtime,slowtime/slow,count}'; done

22.nc 端口转发
nc -l -p $localport -c “nc $remotehost $remoteport”
nc -l -p 8000 -c “nc LAN-IP 8000″
nc -l 9009 | nc -nvv 10.10.10.239 9009

23.curl显示请求头部信息,指定Header
curl -v http://10.5.121.144/top/soa/RankService -H "soa_appid:bk" -H "soa-appsign:7e7ec59d" -d '{"jsonrpc":"2.0","method":"findAllByGameCode","params":[["1000022"]],"id":100}'
curl返回响应时间:
curl -w %{time_connect}:%{time_starttransfer}:%{time_total} http://test.newgame.173.com/soa/v1/GameService -d '{"method":"search","params":[{"page":1,"pageSize":20}],"id":1,"jsonrpc":"2.0"}' -s -o /dev/null
curl --connect-timeout 2 --keepalive-time 1 --max-time 1 -w %{time_connect}:%{time_starttransfer}:%{time_total} http://test.newgame.soa.173.com/search/soa/SearchService

24.dubbo服务状态监测
echo status -l |nc -i 1 10.5.15.222 20880

25.SYN_without_MSS 肉鸡对外洪水攻击排查
dstat+nethogs
ps aux 查找可疑进程

26.循环字符串数组,分别取文件路径和文件名
#!/bin/bash
set -o nounset
#set -o errexit
DIRNAME=/usr/bin/dirname
BASENAME=/bin/basename
BASE_PATH=`$DIRNAME $0`
cd $BASE_PATH
a="/global/soalog/20170408/20/198.sea.internal.com.log /global/soalog/20170407/15/sea5.log /global/soalog/20170709/15/10.59.107.146_v2.log"
arr=($a)
for i in ${arr[@]};
do
echo $i""
FILE_NAME=`$BASENAME $i`
echo $FILE_NAME
DIR_NAME=`$DIRNAME $i`
echo $DIR_NAME
done

27.编码转换,GBK转UTF-8
for i in *.log ;do echo $i;iconv -f GBK -t UTF-8 $i -o utf8/$i; done
28play game
[ $[ $RANDOM % 6 ] == 0 ] && echo "You die" || echo "Lucky boy";

上一篇:Spring MVC 中的 forward 和 redirect


下一篇:F2.Net工作流引擎系列索引