1、vim自动添加注释及智能换行
1
2
3
4
5
6
7
8
9
10
11
12
13
<code># vi /etc/vimrc</code>
<code>"</code><code>set</code> <code>autoindent</code>
<code>set</code> <code>tabstop=4</code>
<code>set</code> <code>shiftwidth=4</code>
<code>function</code> <code>addtitle()</code>
<code>call setline(1,</code><code>"#!/bin/bash"</code><code>)</code>
<code>call append(1,</code><code>"#===================================================="</code><code>)</code>
<code>call append(2,</code><code>"# author: lizhenliang - email:[email protected]"</code><code>)</code>
<code>call append(3,</code><code>"# create date: "</code> <code>. strftime(</code><code>"%y-%m-%d"</code><code>))</code>
<code>"call append(4,"</code><code># filename: " . expand("%")) call append(4,"# description: ")</code>
<code>call append(5,</code><code>"#===================================================="</code><code>)</code>
<code>endf</code>
<code>map <f4> :call addtitle()<cr></code>
# vi test.sh #打开一个测试shell脚本,按f4就会自动添加注释,省了不少时间:
2、查找并删除/data这个目录7天前创建的文件
# find /data -ctime +7 -exec rm -rf {} \;
# find /data -ctime +7 | xargs rm -rf
3、tar命令压缩排除某个目录
# tar zcvf data.tar.gz /data --exclude=tmp #--exclude参数为不包含某个目录或文件,后面也可以跟多个
4、查看tar包存档文件,不解压
# tar tf data.tar.gz #t是列出存档文件目录,f是指定存档文件
5、使用stat命令查看一个文件的访问时间(access)、修改时间(modify)、状态改变时间(change)
stat index.php
access: 2013-11-10 02:37:44.169014602 -0500
modify: 2013-06-18 10:53:14.395999032 -0400
change: 2013-06-18 10:53:38.855999002 -0400
6、批量解压.tar.gz
方法1:# find . -name "*.tar.gz" -exec tar zxf {} \;
方法2:# for tar in *.tar.gz; do tar zxvf $tar; done
方法3:# ls *.tar.gz | xargs tar zxvf
7、筛除出文件中的注释和空格
方法1:grep -v "^#" httpd.conf |grep -v "^$"
方法2:# sed -e ‘/^$/d’ -e ‘/^#/d’ httpd.conf > http.conf
或者 # sed -e '/^#/d;/^$/d' #-e 执行多条sed命令
方法3:# awk '/^[^#]/|/"^$"' httpd.conf
或者 # awk '!/^#|^$/' httpd.conf
8、筛选/etc/passwd文件中所有的用户
方法1:# cat /etc/passwd |cut -d: -f1
方法2:# awk -f ":" '{print $1}' /etc/passwd
9、iptables网站跳转
# iptables -a input -p tcp -m multiport --dport 22,80 -j accept
# echo 1 >/proc/sys/net/ipv4/ip_forward
# iptables -t nat -a prerouting -d 10.0.0.10 -p tcp --dport 80 -j dnat --to-destination 192.168.0.10:80
#将来自10.0.0.10的网站访问请求转发到目标服务器192.168.0.10。另外,内网服务器要配置防火墙内网ip为网关,否则数据包回不来。另外,这里不用配置snat,因为系统服务会根据数据包来源再返回去。
10、iptables将本机80端口转发到本地8080端口
# iptables -t nat -a prerouting -p tcp --dport 80 -j redirect --to-ports 8080
11、find命令查找文件并复制到/opt目录
方法1:# find /etc -name httpd.conf -exec cp -rf {} /opt/ \;: #-exec执行后面命令,{}代表前面输出的结果,\;结束命令
方法2:# find /etc -name httpd.conf |xargs -i cp {} /opt #-i表示输出的结果由{}代替
12、查看根目录下大于1g的文件
# find / -size +1024m
默认单位是b,可以使用其他单位如,c、k、m
13、查看服务器ip连接数
# netstat -tun | awk '{print $5}' | cut -d: -f1 |sort | uniq -c | sort -n
-tun:-tu是显示tcp和udp连接,n是以ip地址显示
cut -d:-f1:cut是一个选择性显示一行的内容命令,-d指定:为分隔符,-f1显示分隔符后的第一个字段。
uniq -c:报告或删除文中的重复行,-c在输出行前面加上出现的次数
sort -n:根据不同类型进行排序,默认排序是升序,-r参数改为降序,-n是根据数值的大小进行排序
14、插入一行到391行,包括特殊符号"/"
# sed -i "391 s/^/addtype application\/x-httpd-php .php .html/" httpd.conf
15、列出nginx日志访问最多的ip
awk '{print $1}' access.log | sort | uniq -c | sort -nr | head -n 10
16、显示访问量最多的前10位ip
# awk '{print $1}' access.log |sort |uniq -c|sort -nr |head -n 10
sort 排序
uniq -c 合并重复行,并记录重复次数
sort -nr 按照数字进行降序排序
17、显示nginx日志一天访问量最多的前10位ip
# awk '$4>="[16/may/2015:00:00:01" && $4<="[16/may/2015:23:59:59"' access_test.log |sort |uniq -c |sort-nr |head -n 10
# awk '$4>="[16/oct/2015:00:00:01" && $4<="[16/oct/2015:23:59:59"{a[$1]++}end{for(i in a){print a[i],i|"sort -k1 -nr |head -n 10"}}' access.log
18、获取当前时间前一分钟日志访问量
# date=`date +%d/%b/%y:%h:%m --date="-1 minute"` ; awk -vd=$date '$0~d{c++}end{print c}' access.log
# date=`date +%d/%b/%y:%h:%m --date="-1 minute"`; awk -vd=$date '$4>="["d":00" && $4<="["d":59"{c++}end{print c}' access.log
# grep `date +%d/%b/%y:%h:%m --date="-1 minute"` access.log |awk 'end{print nr}'
# start_time=`date +%d/%b/%y:%h:%m:%s --date="-5 minute"`;end_time=`date +%d/%b/%y:%h:%m:%s`;awk -vstart_time="[$start_time" -vend_time="[$end_time" '$4>=start_time && $4<=end_time{count++}end{print count}' access.log
19、找出1-255之间的整数
方法1:# ifconfig |grep -o '[0-9]\+' #+号匹配前一个字符一次或多次
方法2:# ifconfig |egrep -o '\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'
20、找出ip地址
# ifconfig |grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' #-o只显示匹配字符
21、给文档增加开头和结尾说明信息
# awk ‘begin{print "开头显示信息"}{print $1,$nf} end{print "结尾显示信息"}’/etc/passwd
# awk 'begin{printf " date ip\n------------------\n"} {print $3,$4} end{printf "------------------\nend...\n"}' /var/log/messages
date ip
------------------
03:13:01 localhost
10:51:45 localhost
end...
22、查看网络状态命令
# netstat -antp #查看所有网络连接
# netstat -lntp #只查看监听的端口信息
# lsof -p pid #查看进程打开的文件句柄
# lsof -i:80 #查看端口被哪个进程占用
23、生成8位随机字符串
方法1:# echo $random |md5sum |cut -c 1-8
方法2:# openssl rand -base64 4
方法3:# cat /proc/sys/kernel/random/uuid | cut -c 1-8
24、while死循环
while true; do #条件精确等于真,也可以直接用条件[ "1" == "1" ],条件一直为真
ping -c 2 www.baidu.com
done
25.awk格式化输出
将文本列进行左对齐或右对齐。
左对齐:
# awk '{printf "%-15s %-10s %-20s\n",$1,$2,$3}' test.txt
右对齐:
# awk '{printf "%15s %10s %20s\n",$1,$2,$3}' test.txt
26.整数运算保留小数点
方法1:# echo 'scale=2; 10/3;'|bc #scale参数代表取小数点位数
方法2:# awk begin'{printf "%.2f\n",10/3}'
27.数字求和
# cat a.txt
23
53
56
方法1:
#!/bin/bash
while read num;
do
sum=`expr $sum + $num`
done < a.txt
echo $sum
方法2:# cat a.txt |awk '{sum+=$1}end{print sum}'
28、判断是否为数字(字符串判断也如此)
# [[ $num =~ ^[0-9]+$ ]] && echo yes || echo no #[[]]比[]更加通用,支持模式匹配=~和字符串比较使用通配符
^ $:从开始到结束是数字才满足条件
=~:一个操作符,表示左边是否满足右边(作为一个模式)正则表达式
29、删除换行符并将空格替换别的字符
# cat a.txt |xargs echo -n |sed 's/[ ]/|/g' #-n 不换行
# cat a.txt |tr -d '\n' #删除换行符
30、查看文本中20至30行内容(总共100行)
方法1:# awk '{if(nr > 20 && nr < 31) print $0}' test.txt
方法2:# sed -n '20,30p' test.txt
方法3:# head -30 test.txt |tail
31、文本中两列位置替换
60.35.1.15 www.baidu.com
45.46.26.85 www.sina.com.cn
# cat a |awk '{print $2"\t"$1}'
32、监控目录,新创建的文件名追加到日志中
#要安装inotify-tools软件包
mon_dir=/opt
inotifywait -mq --format %f -e create $mon_dir |\
while read files; do
echo $files >> test.log
33、find一次查找多个指定文件类型
# find ./ -name '*.jpg' -o -name '*.png'
# find ./ -regex ".*\.jpg\|.*\.png"
34、字符串拆分
# echo "hello" |awk -f '' '{for(i=1;i<=nf;i++)print $i}'
# echo "hello" |sed 's/./&\n/g'
# echo "hello" |sed -r 's/(.)/\1\n/g'
35、实时监控命令运行结果
# watch -d -n 1 'ifconfig'
36、解决邮件乱码问题
# echo `echo "content" | iconv -f utf8 -t gbk` | mail -s "`echo "title" | iconv -f utf8 -t gbk`" [email protected]
注:通过iconv工具将内容字符集转换
37、在文本中每隔三行添加一个换行或内容
# sed '4~3s/^/\n/' file
# awk '$0;nr%3==0{print "\n"}' file
# awk '{print nr%3?$0:$0 "\n"}' file
38、删除匹配行及后一行或前一行
# sed '/abc/,+1d' file #删除匹配行及后一行
# sed '/abc/{n;d}' file #删除后一行
# tac file |sed '/abc/,+1d' |tac #删除前一行
39、统计总行数
效率1 # wc -l file
效率2 # grep -c . file
效率3 # awk 'end{print nr}' file
效率4 # sed -n '$=' file
40、去除文本开头和结尾空格
sed -i 's/^[ \t]*//;s/[ \t]*$//' file
41、给单个ip加单引号
# echo '10.10.10.1 10.10.10.2 10.10.10.3' |sed -r 's/[^ ]+/"&"/g'
# echo '10.10.10.1 10.10.10.2 10.10.10.3' |awk '{for(i=1;i<=nf;i++)printf "\047"$i"\047"}'
42、脚本中打印等待时间
wait(){
echo -n "wait 3s"
for ((i=1;i<=3;i++)); do
echo -n "."
sleep 1
echo
}
wait
43、删除指定行
# awk 'nr==1{next}{print $0}' file #$0可省略
# awk 'nr!=1{print}' file
# awk 'nr!=1{print $0}' 或删除匹配行:awk '!/test/{print $0}'
# sed '1d' file
# sed -n '1!p' file
44、在指定行前后加一行
在第二行前一行加txt:
# awk 'nr==2{sub('/.*/',"txt\n&")}{print}' a.txt
# sed'2s/.*/txt\n&/' a.txt
在第二行后一行加txt:
# awk 'nr==2{sub('/.*/',"&\ntxt")}{print}' a.txt
# sed'2s/.*/&\ntxt/' a.txt
45、通过ip获取网卡名
# ifconfig |awk -f'[: ]' '/^eth/{nic=$1}/192.168.18.15/{print nic}'
46、浮点数运算(数字46保留小数点)
# awk 'begin{print 46/100}'
0.46
# echo 46|awk '{print $0/100}'
# awk 'begin{printf "%.2f\n",46/100}'
# echo 'scale=2;46/100' |bc|sed 's/^/0/'
# printf "%.2f\n" $(echo "scale=2;46/100" |bc)
47、浮点数比较
if [ $(echo "4>3"|bc) -eq 1 ]; then
echo yes
else
echo no
fi
if [ $(awk 'begin{if(4>3)print 1;else print 0}') -eq 1 ]; then
48、替换换行符为逗号
$ cat a.txt
替换后:1,2,3
$ tr '\n' ',' < a.txt
$ sed ':a;n;s/\n/,/;$!b a' a.txt
$ sed ':a;$!n;s/\n/,/;t a' a.txt
while read line; do
a+=($line)
echo ${a[*]} |sed 's/ /,/g'
awk '{s=(s?s","$0:$0)}end{print s}' a.txt
#三目运算符(a?b:c),第一个s是变量,s?s","$0:$0,第一次处理1时,s变量没有赋值为假,结果打印1,第二次处理2时,s值是1,为真,结果1,2。以此类推,小括号可以不写。
awk '{if($0!=3)printf "%s,",$0;else print $0}' a.txt
49、windows下文本到linux下隐藏格式去除
:set fileformat=unix
:%s/\r*$// #^m可用\r代替
sed -i 's/^m//g' a.txt #^m的输入方式是ctrl+v,然后ctrl+m
dos2unix a.txt
50、xargs巧用
xargs -n1 #将单个字段作为一行
1 2
3 4
# xargs -n1 < a.txt
xargs -n2 #将两个字段作为一行
$ cat b.txt
string
number
a
b
$ xargs -n2 < a.txt
string number
a 1
b 2
$ awk 'ors=nr%2?"\t":"\n"' b.txt #把奇数行换行符去掉