With so many years of Linux, these commands use tricks you may not know yet!

Source: Internet
Author: User

Under Unix/linux, the efficient way to work is not to manipulate graphical pages, but command-line operations, which means easier automation. A friend who has used a Linux system should know the power of its command line. Now, how much do you know about the following command-using techniques?

1, Vim automatically add comments and smart line-wrapping
# vi ~/.vimrc set autoindentset tabstop=4set shiftwidth=4function 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,"#====================================================")endfmap <F4> :call AddTitle()<cr>

When you open a file, pressing F4 will automatically add a comment, saving you some time:

2. Find and delete/data This directory was created 7 days ago
# find /data -ctime +7 -exec rm -rf {} \;# find /data -ctime +7 | xargs rm -rf
3. Tar command compression excludes a directory
# tar zcvf data.tar.gz /data --exclude=tmp    #--exclude参数为不包含某个目录或文件,后面也可以跟多个
4, check the TAR package archive file, do not pressure

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

5. Use the stat command to view the properties of a file
访问时间(Access)、修改时间(modify)、状态改变时间(Change)stat index.phpAccess: 2018-05-10 02:37:44.169014602 -0500Modify: 2018-05-09 10:53:14.395999032 -0400Change: 2018-05-09 10:53:38.855999002 -0400
6, Batch decompression 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 -i tar zxvf {}  
7. Sift out the notes 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 the users in the/etc/passwd file
方法1:# cat /etc/passwd |cut -d: -f1方法2:# awk -F ":" ‘{print $1}‘ /etc/passwd
9, Iptables website jump
# 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 site access requests are forwarded to the destination server 192.168.0.10. In addition, the intranet server to configure the firewall intranet IP for the gateway, otherwise the packet will not return. In addition, there is no need to configure SNAT, because the system service will return based on the packet source.

10. Iptables forwarding the native 80 port to the local 8080 port

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

11. Find command to locate files and copy to/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, and you can use other units such as C, K, M

13. View 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 391 lines, including special symbol "/"

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

15, List Nginx log access to the most 10 IP
方法1:# awk ‘{print $1}‘ access.log |sort |uniq -c|sort -nr |head -n 10sort :排序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 Nginx log the most visited the first 10-bit IP
# 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 log traffic 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. Locate 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 the beginning and end of the document to explain the information
# 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 localhost10:51:45 localhost------------------end...
21. View Network Status command
# 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 Dead Loop
while true; do  #条件精确等于真,也可以直接用条件[ "1" == "1" ],条件一直为真     ping -c 2 www.baidu.comdone
24.awk formatted output

Aligns the text column to the left or right.

左对齐:# awk ‘{printf "%-15s %-10s %-20s\n",$1,$2,$3}‘ test.txt右对齐:# awk ‘{printf "%15s %10s %20s\n",$1,$2,$3}‘ test.txt
25. Integer operation reserved decimal point
方法1:# echo ‘scale=2; 10/3;‘|bc  
26. Sum of Numbers
# cat a.txt10235356方法1:#!/bin/bashwhile read num;        do        sum=`expr $sum + $num`done < a.txt        echo $sum方法2:# cat a.txt |awk ‘{sum+=$1}END{print sum}‘
27, judge whether it is a number (also such as String judgment)
# [[ $num =~ ^[0-9]+$ ]] && echo yes || echo no    #[[]]比[]更加通用,支持模式匹配=~和字符串比较使用通配符`^ $:从开始到结束是数字才满足条件=~:一个操作符,表示左边是否满足右边(作为一个模式)正则表达式
28. Remove line breaks and replace spaces with other characters
# cat a.txt |xargs echo -n |sed ‘s/[ ]/|/g‘  #-n 不换行# cat a.txt |tr -d ‘\n‘  #删除换行符
29. View 20 to 30 lines of text (100 lines 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. Two-column position substitution in text
# cat a.txt60.35.1.15      www.baidu.com45.46.26.85     www.sina.com.cn# awk ‘{print $2"\t"$1}‘  a.txt

More good text please visit: Http://blog.51cto.com/lizhenliang
High salary compulsory Course: http://edu.51cto.com/lecturer/7876557.html

With so many years of Linux, these commands use tricks you may not know yet!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.