Linux 运维必备的 40 个命令总结(波哥强推)

       文章来源 51CTO博客


点击蓝字:波哥的IT人生,关注我们



1、删除0字节文件

   
    </ul>
    <pre class="code-snippet__js" data-lang="bash"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">find -type f -size 0 -exec rm -rf {} \;</span></code></pre>
    
2、查看进程
按内存从大到小排列
   
    </ul>
    <pre class="code-snippet__js" data-lang="perl"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">PS -e -o "%C : %p : %z : %a"|sort -k5 -nr</span></code></pre>
    

3、按 CPU 利用率从大到小排列

   
    </ul>
    <pre class="code-snippet__js" data-lang="perl"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">ps -e -o "%C : %p : %z : %a"|sort -nr</span></code></pre>
    

4、打印 cache 里的URL

   
    </ul>
    <pre class="code-snippet__js" data-lang="nginx"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">grep&nbsp;-r&nbsp;-a&nbsp;jpg&nbsp;/data/cache/*&nbsp;|&nbsp;strings&nbsp;|&nbsp;grep&nbsp;"http:"&nbsp;|&nbsp;awk&nbsp;-F'http:'&nbsp;'{print&nbsp;"http:"$2;}'</span></code></pre>
    

5、查看 http 的并发请求数及其 TCP 连接状态:

   
    </ul>
    <pre class="code-snippet__js" data-lang="nginx"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'</span></code></pre>
    
6、  sed -i '/Root/s/no/yes/' /etc/ssh/sshd_config  sed 在这个文里 Root 的一行,匹配 Root 一行,将 no 替换成 yes。

7、如何杀掉 MySQL 进程

   
    </ul>
    <pre class="code-snippet__js" data-lang="perl"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">ps aux |grep mysql |grep -v grep  |awk '{print $2}' |xargs kill -9 (从中了解到awk的用途)</span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer"><br></span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">killall -TERM mysqld</span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer"><br></span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">kill -9 `cat /usr/local/apache2/logs/httpd.pid`   试试查杀进程PID</span></code></pre>
    

8、显示运行 3 级别开启的服务:

   
    </ul>
    <pre class="code-snippet__js" data-lang="nginx"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">ls /etc/rc3.d/S* |cut -c 15-   (从中了解到cut的用途,截取数据)</span></code></pre>
    

9、如何在编写 SHELL 显示多个信息,用 EOF

   
    </ul>
    <pre class="code-snippet__js" data-lang="diff"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">cat &lt;&lt; EOF</span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">+--------------------------------------------------------------+</span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">|       === Welcome to Tunoff services ===                |</span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">+--------------------------------------------------------------+</span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">EOF</span></code></pre>
    

10、for 的巧用(如给 MySQL 建软链接)

   
    </ul>
    <pre class="code-snippet__js" data-lang="bash"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">cd /usr/local/mysql/bin</span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">for i in *</span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">do ln /usr/local/mysql/bin/$i /usr/bin/$i</span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">done</span></code></pre>
    

11、取 IP 地址

   
    </ul>
    <pre class="code-snippet__js" data-lang="nginx"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">ifconfig eth0 |grep "inet addr:" |awk '{print $2}'| cut -c 6-  </span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">或者</span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'</span></code></pre>
    

12、内存的大小:

   
    </ul>
    <pre class="code-snippet__js" data-lang="nginx"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">free -m |grep "Mem" | awk '{print $2}'</span></code></pre>
    

13

   
    </ul>
    <pre class="code-snippet__js" data-lang="nginx"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">netstat -an -t | grep ":80" | grep ESTABLISHED | awk '{printf "%s %s\n",$5,$6}' | sort</span></code></pre>
    

14、查看 Apache 的并发请求数及其 TCP 连接状态:

   
    </ul>
    <pre class="code-snippet__js" data-lang="nginx"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'</span></code></pre>
    

15、因为同事要统计一下服务器下面所有的 jpg 的文件的大小,写了个 SHELL 给他来统计。原来用 xargs 实现,但他一次处理一部分。搞的有多个总和……,下面的命令就能解决。

   
    </ul>
    <pre class="code-snippet__js" data-lang="swift"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">find / -name *.jpg -exec wc -c {} \;|awk '{print $1}'|awk '{a+=$1}END{print a}'</span></code></pre>
    

CPU 的数量(多核算多个CPU,cat /proc/cpuinfo |grep -c processor)越多,系统负载越低,每秒能处理的请求数也越多。

16、CPU负载

   
    </ul>
    <pre class="code-snippet__js" data-lang="apache"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">cat /proc/loadavg</span></code></pre>
    

检查前三个输出值是否超过了系统逻辑 CPU 的4倍。

17、 CPU负载

   
    </ul>
    <pre class="code-snippet__js" data-lang="apache"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">mpstat 1 1</span></code></pre>
    

检查 %idle 是否过低(比如小于5%)。

18、内存空间

   
    </ul>
    <pre class="code-snippet__js" data-lang="apache"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">free</span></code></pre>
    

检查 free 值是否过低,也可以用 # cat /proc/meminfo

19、SWAP 空间   

free

检查 swap used 值是否过高,如果 swap used 值过高,进一步检查 swap 动作是否频繁:

   
    </ul>
    <pre class="code-snippet__js" data-lang="apache"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">vmstat 1 5</span></code></pre>
    

观察 si 和 so 值是否较大

20、磁盘空间  

df -h
检查是否有分区使用率(Use%)过高(比如超过90%)如发现某个分区空间接近用尽,可以进入该分区的挂载点,用以下命令找出占用空间最多的文件或目录:
   
    </ul>
    <pre class="code-snippet__js" data-lang="apache"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">du -cks * | sort -rn | head -n 10</span></code></pre>
    

21、磁盘 I/O 负载

iostat -x 1 2

检查I/O使用率(%util)是否超过 100%

22、网络负载

sar -n DEV

检查网络流量(rxbyt/s, txbyt/s)是否过高

23、网络错误

netstat -i

检查是否有网络错误(drop fifo colls carrier),也可以用命令:# cat /proc/net/dev

24、网络连接数目

   
    </ul>
    <pre class="code-snippet__js" data-lang="swift"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">netstat -an | grep -E “^(tcp)” | cut -c 68- | sort | uniq -c | sort -n</span></code></pre>
    

25、进程总数   

ps aux | wc -l

检查进程个数是否正常 (比如超过250)

26、可运行进程数目   

vmwtat 1 5

列给出的是可运行进程的数目,检查其是否超过系统逻辑 CPU 的 4 倍

27、进程   

top -id 1

观察是否有异常进程出现。

28、网络状态,检查DNS,网关等是否可以正常连通

29、用户

who | wc -l

检查登录用户是否过多 (比如超过50个)   也可以用命令:# uptime。

30、系统日志

# cat /var/log/rflogview/*errors

检查是否有异常错误记录   也可以搜寻一些异常关键字,例如:

grep -i error /var/log/messagesgrep -i fail /var/log/messages

31、核心日志   

dmesg

检查是否有异常错误记录。

32、系统时间   

date

检查系统时间是否正确。

33、打开文件数目   

lsof | wc -l

检查打开文件总数是否过多。

34、日志 

# logwatch –print
配置 /etc/log.d/logwatch.conf,将 Mailto 设置为自己的 email 地址,启动 mail 服务(sendmail或者postfix),这样就可以每天收到日志报告了。
缺省 logwatch 只报告昨天的日志,可以用 # logwatch –print –range all 获得所有的日志分析结果。
可以用 # logwatch –print –detail high 获得更具体的日志分析结果(而不仅仅是出错日志)。

35、杀掉80端口相关的进程

lsof -i :80|grep -v “ID”|awk ‘{print “kill -9”,$2}’|sh

36、清除僵死进程

   
    </ul>
    <pre class="code-snippet__js" data-lang="nginx"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">ps -eal | awk '{ if ($2 == "Z") {print $4}}' | kill -9</span></code></pre>
    

37、tcpdump 抓包,用来防止80端口被人攻击时可以分析数据

   
    </ul>
    <pre class="code-snippet__js" data-lang="apache"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">tcpdump&nbsp;-c&nbsp;10000&nbsp;-i&nbsp;eth0&nbsp;-n&nbsp;dst&nbsp;port&nbsp;80&nbsp;&gt;&nbsp;/root/pkts</span></code></pre>
    

38、然后检查IP的重复数并从小到大排序 注意 “-t\ +0”   中间是两个空格

   
    </ul>
    <pre class="code-snippet__js" data-lang="swift"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;"># less pkts | awk {'printf $3"\n"'} | cut -d. -f 1-4 | sort | uniq -c | awk {'printf $1" "$2"\n"'} | sort -n -t\ +0</span></code></pre>
    

39、查看有多少个活动的 php-cgi 进程

   
    </ul>
    <pre class="code-snippet__js" data-lang="nginx"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">netstat -anp | grep php-cgi | grep ^tcp | wc -l</span></code></pre>
    

40、查看系统自启动的服务

   
    </ul>
    <pre class="code-snippet__js" data-lang="nginx"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">chkconfig --list | awk '{if ($5=="3:on") print $1}'</span></code></pre>
    

41、kudzu 查看网卡型号

   
    </ul>
    <pre class="code-snippet__js" data-lang="javascript"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">kudzu --probe --<span class="code-snippet__class" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span class="code-snippet__keyword" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">class</span></span>=network</span></code></pre>
    

常用正则表达式

1.匹配中文字符的正则表达式: [\u4e00-\u9fa5]

评注:匹配中文还真是个头疼的事,有了这个表达式就好办了

2.匹配双字节字符(包括汉字在内):[^\x00-\xff]

评注:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)

3.匹配空白行的正则表达式:\n\s*\r

评注:可以用来删除空白行

4.匹配 HTML 标记的正则表达式:<(\S*?)[^>]*>.*?</\1>|<.*? />

评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力

5.匹配首尾空白字符的正则表达式:^\s*|\s*$

评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式

6.匹配Email地址的正则表达式:

   
    </ul>
    <pre class="code-snippet__js" data-lang="css"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*</span></code></pre>
    

评注:表单验证时很实用

7.匹配网址URL的正则表达式:[a-zA-z]+://[^\s]*

评注:网上流传的版本功能很有限,上面这个基本可以满足需求

8.匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$

评注:表单验证时很实用

9.匹配国内电话号码:\d{3}-\d{8}|\d{4}-\d{7}

评注:匹配形式如 0511-4405222 或 021-87888822

10.匹配腾讯QQ号:[1-9][0-9]{4,}

评注:腾讯QQ号从10000开始

11.匹配中国邮政编码:[1-9]\d{5}(?!\d)

评注:中国邮政编码为6位数字

12.匹配×××:\d{15}|\d{18}

评注:中国的×××为15位或18位

13.匹配ip地址:\d+\.\d+\.\d+\.\d+

评注:提取 IP 地址时有用

14.匹配特定数字:

^[1-9]\d*$   //匹配正整数^-[1-9]\d*$  //匹配负整数^-?[1-9]\d*$  //匹配整数^[1-9]\d*|0$ //匹配非负整数(正整数 + 0)^-[1-9]\d*|0$  //匹配非正整数(负整数 + 0)^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$  //匹配正浮点数^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ //匹配负浮点数^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ //匹配浮点数^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$  //匹配非负浮点数(正浮点数 + 0)^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ //匹配非正浮点数(负浮点数 + 0)

评注:处理大量数据时有用,具体应用时注意修正

15.匹配特定字符串:

   
    </ul>
    <pre class="code-snippet__js" data-lang="javascript"><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">^[A-Za-z]+$ //匹配由26个英文字母组成的字符串</span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串</span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">^[a-z]+$ //匹配由26个英文字母的小写组成的字符串</span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串</span></code><code style=" white-space:pre-wrap;max-width: 1000%;text-align: left;display: flex; font-family: Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace;box-sizing: border-box !important;overflow-wrap: break-word !important; "><span class="code-snippet_outer" style="max-width: 1000%;box-sizing: border-box !important;overflow-wrap: break-word !important;">^\w+$ //匹配由数字、26个英文字母或者下划线组成的字符串</span></code></pre>
    

评注:最基本也是最常用的一些表达式

扫码关注

绝对干货的公众号


推荐阅读



实战项目--kkitDeploy(客户端版)(强推,已开源)

实战项目--kkitDeploy(PaaS版)(强推)

实战项目--kkit3.0功能介绍(强推)

实战项目--kkit1.0功能介绍(强推,已开源)

面试题之---网络安全(2019年10月更新)

面试题之---k8s(2019年10月更新)

面试题之---运维工程师经典面试汇总(一)

经历贴之---外包接活却坐牢456天完整记录(强推)

脚本之---短信轰炸机

脚本之---QQ微信轰炸机

脚本之---linux流量监控利器

脚本之---linux系统巡检脚本合集

脚本之---linux批量用户管理脚本

脚本之---根据PID查看相关信息

脚本之---一键完成系统安全加固

脚本之---检查系统用户

脚本之---一键同步脚本文件夹

脚本之---一键升/降级k8s集群脚本

脚本之---一键升新增k8s集群node节点脚本

脚本之---k8s一键部署脚本(多版本通用版)(强推)

脚本之---一键部署ceph(luminous)集群脚本

脚本之---一键部署openvpn脚本

脚本之---一键(单双向)互信初始化服务器脚本

脚本之---一键初始化调优服务器脚本

脚本之---zabbixAgent一键部署脚本(windos/linux)

ansible---一键搭建redis5.0.5集群

工具之---地表最强CMDB自动抓取工具


点小花花,让他们知道你“在看”

已有 0 条评论

    欢迎您,新朋友,感谢参与互动!