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

作者:微信小助手

发布时间:2020-11-30T08:18:46

       文章来源 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: