Have you mastered the 30 must-know Linux command skills?

Under Unix/Linux, the efficient way to work is not to manipulate graphical pages, but command line operations. Command line means easier automation. Friends who have used the Linux system should all know the power of its command line. Having said that, how much do you know about the use of these commands?

1. Vim automatically adds comments and smart line breaks



# vi ~/.vimrc 
set autoindent
set tabstop=4
set shiftwidth=4
function AddTitle()
call setline(1,"#!/bin/bash")
call append(1,"#====================================================")
call append(2,"# Author: lizhenliang")
call append(3,"# Create Date: " . strftime("%Y-%m-%d"))
call append(4,"# Description: ")
call append(5,"#====================================================")
endf
map <F4> :call AddTitle()<cr>

After opening the file, press F4 to automatically add a comment, which saves a lot of time:

Have you mastered the 30 must-know Linux command skills?

2. Find and delete files created 7 days ago in the /data directory


# find /data -ctime +7 -exec rm -rf {} \;
# find /data -ctime +7 | xargs rm -rf

3. The tar command compresses and excludes a certain directory


# tar zcvf data.tar.gz /data --exclude=tmp    #--exclude参数为不包含某个目录或文件,后面也可以跟多个

4. View the archive file of the tar package without decompressing it


# tar tf data.tar.gz  #t是列出存档文件目录,f是指定存档文件

5. Use the stat command to view the attributes of a file


访问时间(Access)、修改时间(modify)、状态改变时间(Change)
stat index.php
Access: 2018-05-10 02:37:44.169014602 -0500
Modify: 2018-05-09 10:53:14.395999032 -0400
Change: 2018-05-09 10:53:38.855999002 -0400

6. Unzip tar.gz in batches


方法1:
# find . -name "*.tar.gz" -exec tar zxf {} \;
方法2:
# for tar in *.tar.gz; do tar zxvf $tar; done
方法3:
# ls *.tar.gz | xargs -i tar zxvf {}

7. Filter out comments and spaces in the file



方法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. Filter all users in the /etc/passwd file


方法1:
# cat /etc/passwd |cut -d: -f1
方法2:
# awk -F ":" '{print $1}' /etc/passwd

9, iptables website jump



先开启路由转发:
echo "1" > /proc/sys/net/ipv4/ip_forward  #临时生效

内网访问外网(SNAT):
iptables –t nat -A POSTROUTING -s [内网IP或网段] -j SNAT --to [公网IP]
#内网服务器要指向防火墙内网IP为网关

公网访问内网(DNAT):
iptables –t nat -A PREROUTING -d [对外IP] -p tcp --dport [对外端口] -j DNAT --to [内网IP:内网端口]
#内网服务器要配置防火墙内网IP为网关,否则数据包回不来。另外,这里不用配置SNAT,因为系统服务会根据数据包来源再返回去。

10. iptables forwards the local port 80 to the local port 8080


# iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080

11. The find command finds the file and copies it to the /opt directory



方法1:
# find /etc -name httpd.conf -exec cp -rf {} /opt/ \;:    #-exec执行后面命令,{}代表前面输出的结果,\;结束命令
方法2:
# find /etc -name httpd.conf |xargs -i cp {} /opt     #-i表示输出的结果由{}代替

12. View files larger than 1G in the root directory



# find / -size +1024M

The default unit is b, other units such as C, K, M can be used

13. Check the number of server IP connections



# 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. Insert one line to line 391, including the special symbol "/"


# sed -i "391 s/^/AddType application\/x-httpd-php .php .html/" httpd.conf

15. List the 10 most visited IPs in nginx logs

方法1:
# awk '{print $1}' access.log |sort |uniq -c|sort -nr |head -n 10
sort :排序
uniq -c:合并重复行,并记录重复次数
sort -nr :按照数字进行降序排序   
方法2:
# awk '{a[$1]++}END{for(v in a)print v,a[v] |"sort -k2 -nr |head -10"}' access.log

16. Display the top 10 IPs with the most visits in nginx logs in a day


# awk '$4>="[16/May/2017:00:00:01" && $4<="[16/May/2017:23:59:59"' access_test.log |sort |uniq -c |sort-nr |head -n 10
# awk '$4>="[16/Oct/2017:00:00:01" && $4<="[16/Oct/2017:23:59:59"{a[$1]++}END{for(i in a){print a[i],i|"sort -k1 -nr |head -n 10"}}' access.log

17. Get the log visits one minute before the current time



# 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

18. Find the integer between 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])\>'

19. Find out the IP address



# ifconfig |grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'  #-o只显示匹配字符

20. Add information at the beginning and end of the document



# 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...

21. View network status commands


# netstat -antp #查看所有网络连接
# netstat -lntp #只查看监听的端口信息
# lsof -p pid #查看进程打开的文件句柄
# lsof -i:80  #查看端口被哪个进程占用

22. Generate 8-bit random string



方法1:
# echo $RANDOM |md5sum |cut -c 1-8
方法2:
# openssl rand -base64 4
方法3:
# cat /proc/sys/kernel/random/uuid | cut -c 1-8

23, while infinite loop


while true; do  #条件精确等于真,也可以直接用条件[ "1" == "1" ],条件一直为真
     ping -c 2 www.baidu.com
done

24.awk formatted output


将文本列进行左对齐或右对齐。

左对齐:
# awk '{printf "%-15s %-10s %-20s\n",$1,$2,$3}' test.txt
右对齐:
# awk '{printf "%15s %10s %20s\n",$1,$2,$3}' test.txt

25. Integer operations retain the decimal point


方法1:
# echo 'scale=2; 10/3;'|bc  #scale参数代表取小数点位数
方法2:
# awk BEGIN'{printf "%.2f\n",10/3}'

26. Summation of numbers


# cat a.txt
10
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}'

27. Determine whether it is a number (the same is true for string judgment)



# [[ $num =~ ^[0-9]+$ ]] && echo yes || echo no    #[[]]比[]更加通用,支持模式匹配=~和字符串比较使用通配符`
^ $:从开始到结束是数字才满足条件
=~:一个操作符,表示左边是否满足右边(作为一个模式)正则表达式

28, delete newlines and replace other characters with spaces


# cat a.txt |xargs echo -n |sed 's/[ ]/|/g'  #-n 不换行
# cat a.txt |tr -d '\n'  #删除换行符

29. View 20 to 30 lines in the text (100 lines in total)


方法1:
# awk '{if(NR > 20 && NR < 31) print $0}' test.txt
方法2:
# sed -n '20,30p' test.txt 
方法3:
# head -30 test.txt |tail

30. Replace the two column positions in the text


# cat a.txt
60.```35.1.15      www.baidu.com
45.46.26.85     www.sina.com.cn
# awk '{print $2"\t"$1}'  a.txt

Guess you like

Origin blog.51cto.com/15127501/2657018