<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <title>Linux｜林鼎淵 Dean Lin</title>
    <link>https://deanlin.net/tags/linux/</link>
    <image><url>https://deanlin.net/images/author_hu_aa256fc934e9a932.jpg</url><title>Linux｜林鼎淵 Dean Lin</title><link>https://deanlin.net/tags/linux/</link><width>144</width><height>144</height></image>
    <description>林鼎淵（Dean Lin）的部落格：生成式 AI 企業內訓講師、外商資安公司軟體專家、7 本書作者。400 多篇文章用白話分享生成式 AI 實戰、Claude Code 與 Vibe Coding 教學、團隊合作與職場成長。</description>
    <generator>Hugo</generator>
    <language>zh-tw</language>
    <copyright>© 2020–2026 林鼎淵 Dean Lin</copyright>
    <lastBuildDate>Wed, 17 Aug 2022 17:59:44 +0800</lastBuildDate>
    <atom:link href="https://deanlin.net/tags/linux/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>那些沒人教你又超實用的 Linux 指令</title>
      <link>https://deanlin.net/blog/202208/useful-linux-commands/</link>
      <pubDate>Wed, 17 Aug 2022 17:59:44 +0800</pubDate>
      <guid>https://deanlin.net/blog/202208/useful-linux-commands/</guid>
      <dc:creator>林鼎淵</dc:creator>
      <category>技術教學</category>
      <description>儘管在 Linux 用 cd、vi、mv、cp、rm、ls 這些基礎指令就能滿足大部分的需求，但遇到一些特殊狀況時，這些指令就不夠用了。這篇文章彙整了筆者常用的 Linux 指令，以「vim 編輯器、查詢 Server 各項資源、檢查網路環境」為主。</description>
      <media:content url="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-og_hu_eadb6778986bc128.jpg" medium="image" type="image/jpeg" width="1200" height="675" />
      <content:encoded><![CDATA[<p>儘管在 Linux 用 cd、vi、mv、cp、rm、ls 這些基礎指令就能滿足大部分的需求，但遇到一些特殊狀況時，這些指令就不夠用了。</p>
<p>這篇文章彙整了筆者常用的 Linux 指令，以「vim 編輯器、查詢 Server 各項資源、檢查網路環境」為主。</p>
<p><img src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-01.webp" width="1400" height="730" alt="那些沒人教你又超實用的 Linux 指令（圖 1）" fetchpriority="high" decoding="async"></p>
<pre tabindex="0"><code>大綱
▋ vim 編輯器常用指令
 ➤ 顯示行數（:set number）
 ➤ 前往指定行數（:行數）
 ➤ 刪除多行
 ➤ 大量註解/反註解
 ➤ 搜尋文字並取代
▋ 查詢 Server 各項資源
 ➤ Server 啟動多久了（uptime）
 ➤ 找出最耗費資源的程式（top）
 ➤ 以目錄形式呈現目前磁碟使用狀況（ncdu）
 ➤ 搜尋檔案（find）
 ➤ 搜尋歷史指令（Control+R）
▋ 檢查網路環境
 ➤ 網路診斷瑞士刀（nc）
 ➤ 設定好 DNS 後，確認回應是否正常（nslookup）
 ➤ 查詢佔用的 port（lsof）
</code></pre><h2 id="-vim-編輯器常用指令">vim 編輯器常用指令<a class="h-anchor" href="#-vim-%e7%b7%a8%e8%bc%af%e5%99%a8%e5%b8%b8%e7%94%a8%e6%8c%87%e4%bb%a4" aria-label="這個段落的連結"></a></h2>
<p>有時我們會需要進入 Server 做一些緊急調整（ex：Debug 時），搭配下面的操作可以增進你的效率。</p>
<h3 id="-顯示行數set-number">顯示行數（:set number）<a class="h-anchor" href="#-%e9%a1%af%e7%a4%ba%e8%a1%8c%e6%95%b8set-number" aria-label="這個段落的連結"></a></h3>
<p>方便與 Local 端程式做比對。</p>
<p><img src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-02.webp" width="1400" height="944" alt="顯示行數（:set number）（圖 2）" loading="lazy" decoding="async"></p>
<h3 id="-前往指定行數行數">前往指定行數（:行數）<a class="h-anchor" href="#-%e5%89%8d%e5%be%80%e6%8c%87%e5%ae%9a%e8%a1%8c%e6%95%b8%e8%a1%8c%e6%95%b8" aria-label="這個段落的連結"></a></h3>
<p>直接跳轉到你要修改的位置。</p>
<figure class="figure figure-video">
  <video class="gifv" muted loop playsinline controls preload="none" poster="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-03.webp" width="950" height="658" aria-label="前往指定行數（:行數）（圖 3）" data-gifv>
    <source src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-03.mp4" type="video/mp4">
  </video>
</figure>

<h3 id="-刪除多行">刪除多行<a class="h-anchor" href="#-%e5%88%aa%e9%99%a4%e5%a4%9a%e8%a1%8c" aria-label="這個段落的連結"></a></h3>
<p>刪除一行的指令是「<code>dd</code>」，但如果有幾十行甚至上百行需要刪除，那會 dd 到崩潰，而且很容易有誤刪的情形。</p>
<ul>
<li>刪除從游標開始的 N 行（ex：刪除 5 行就是 <code>5dd</code>）</li>
</ul>
<figure class="figure figure-video">
  <video class="gifv" muted loop playsinline controls preload="none" poster="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-04.webp" width="960" height="651" aria-label="可以觀察右下角，會看到我的輸入" data-gifv>
    <source src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-04.mp4" type="video/mp4">
  </video>
  <figcaption>可以觀察右下角，會看到我的輸入</figcaption>
</figure>

<ul>
<li>刪除指定開始、結束的行 <code>:[start],[end]d</code> （ex：刪除第 3~17 行 <code>3,17d</code>）</li>
</ul>
<figure class="figure figure-video">
  <video class="gifv" muted loop playsinline controls preload="none" poster="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-05.webp" width="960" height="651" aria-label="刪除多行（圖 5）" data-gifv>
    <source src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-05.mp4" type="video/mp4">
  </video>
</figure>

<h3 id="-大量註解反註解">大量註解/反註解<a class="h-anchor" href="#-%e5%a4%a7%e9%87%8f%e8%a8%bb%e8%a7%a3%e5%8f%8d%e8%a8%bb%e8%a7%a3" aria-label="這個段落的連結"></a></h3>
<p>有時我們不是要刪除程式，只是想要把一定範圍內的程式註解起來做一些測試或是 Debug，如果你依靠「<code>i</code>」來慢慢編輯，真的會心死。</p>
<ul>
<li><strong>大量註解</strong>：先將游標移至想開始註解的行，按「Control+v」進入 VISUAL BLOCK 模式，接著選擇想要註解的區塊；按下「Shift+i」進入編輯模式填寫註解符號（ex：<code>//</code>），按「Esc」離開。</li>
</ul>
<figure class="figure figure-video">
  <video class="gifv" muted loop playsinline controls preload="none" poster="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-06.webp" width="960" height="651" aria-label="大量註解/反註解（圖 6）" data-gifv>
    <source src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-06.mp4" type="video/mp4">
  </video>
</figure>

<ul>
<li><strong>反註解</strong>：先將游標移至想開始註解的行，按「Control+v」進入 VISUAL BLOCK 模式，接著選擇想要反註解的區塊（因為我的註解用<code>//</code>所以要按右鍵刪除兩列）；按下「d」刪除註解。</li>
</ul>
<figure class="figure figure-video">
  <video class="gifv" muted loop playsinline controls preload="none" poster="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-07.webp" width="960" height="651" aria-label="大量註解/反註解（圖 7）" data-gifv>
    <source src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-07.mp4" type="video/mp4">
  </video>
</figure>

<h3 id="-搜尋文字並取代">搜尋文字並取代<a class="h-anchor" href="#-%e6%90%9c%e5%b0%8b%e6%96%87%e5%ad%97%e4%b8%a6%e5%8f%96%e4%bb%a3" aria-label="這個段落的連結"></a></h3>
<pre tabindex="0"><code>:[範圍]s/[比對字串]/[取代字串]/g
</code></pre><ul>
<li>將全部的「test」取代為「try」： <code>:%s/test/try/g</code></li>
</ul>
<figure class="figure figure-video">
  <video class="gifv" muted loop playsinline controls preload="none" poster="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-08.webp" width="960" height="651" aria-label="搜尋文字並取代（圖 8）" data-gifv>
    <source src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-08.mp4" type="video/mp4">
  </video>
</figure>

<ul>
<li>將 1~10 行的「test」取代為「try」：<code>:1,10s/test/try/g</code></li>
</ul>
<figure class="figure figure-video">
  <video class="gifv" muted loop playsinline controls preload="none" poster="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-09.webp" width="960" height="651" aria-label="搜尋文字並取代（圖 9）" data-gifv>
    <source src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-09.mp4" type="video/mp4">
  </video>
</figure>

<hr>
<h2 id="-查詢-server-各項資源">查詢 Server 各項資源<a class="h-anchor" href="#-%e6%9f%a5%e8%a9%a2-server-%e5%90%84%e9%a0%85%e8%b3%87%e6%ba%90" aria-label="這個段落的連結"></a></h2>
<p>了解資源的使用狀況，可以幫你更快地找出問題點，與優化方向。</p>
<h3 id="-server-啟動多久了uptime">Server 啟動多久了（uptime）<a class="h-anchor" href="#-server-%e5%95%9f%e5%8b%95%e5%a4%9a%e4%b9%85%e4%ba%86uptime" aria-label="這個段落的連結"></a></h3>
<p><img src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-10.webp" width="1400" height="87" alt="Server 啟動多久了（uptime）（圖 10）" loading="lazy" decoding="async"></p>
<ul>
<li><strong>21:21:33</strong>：系統時間</li>
<li><strong>up 6 days, 7:40</strong>：Server 已執行時間，時間越大代表越穩定。</li>
<li><strong>1 user</strong>：使用者總連線數（注意不是使用者數！）。</li>
<li><strong>load average：</strong>系統平均負載狀況，顯示1 / 5 / 15 分鐘的系統平均負載（以單核 CPU 來說，建議數值不大於 3，超過 5 就需要注意了）。</li>
</ul>
<h3 id="-找出最耗費資源的程式top">找出最耗費資源的程式（top）<a class="h-anchor" href="#-%e6%89%be%e5%87%ba%e6%9c%80%e8%80%97%e8%b2%bb%e8%b3%87%e6%ba%90%e7%9a%84%e7%a8%8b%e5%bc%8ftop" aria-label="這個段落的連結"></a></h3>
<p><code>top</code> 這個指令會顯示 Server 即時的資源使用狀況。</p>
<p><img src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-11.webp" width="1400" height="979" alt="找出最耗費資源的程式（top）（圖 11）" loading="lazy" decoding="async"></p>
<p>top 也可以指定欄位做排序，以下面的指令（ <code>top -b -o +%MEM | head -n 17</code>）為例，他是指：</p>
<ul>
<li><strong>-b</strong>：以 batch 模式輸出報表。</li>
<li><strong>-o</strong>：我們使用「%MEM」這個欄位，以「正序」來排序。</li>
<li><strong>head -n 17</strong>：因為上面的資訊就佔了 7 行，所以你想要顯示前 10 名就要用 10+7=17。</li>
</ul>
<p><img src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-12.webp" width="1400" height="696" alt="找出最耗費資源的程式（top）（圖 12）" loading="lazy" decoding="async"></p>
<blockquote>
<p>在找出最耗費資源的程式後，有時會搭配 <code>kill [PID]</code> 的方式來終止程式；如果無法順利終止，可使用 <code>kill -9 [PID]</code> 來強行終止。</p>
</blockquote>
<h3 id="-以目錄形式呈現目前磁碟使用狀況ncdu">以目錄形式呈現目前磁碟使用狀況（ncdu）<a class="h-anchor" href="#-%e4%bb%a5%e7%9b%ae%e9%8c%84%e5%bd%a2%e5%bc%8f%e5%91%88%e7%8f%be%e7%9b%ae%e5%89%8d%e7%a3%81%e7%a2%9f%e4%bd%bf%e7%94%a8%e7%8b%80%e6%b3%81ncdu" aria-label="這個段落的連結"></a></h3>
<p>如果發現磁碟空間快滿了，又不確定哪個資料夾佔用了大量空間，<code>ncdu</code> 可以幫你高效率的檢查（指令會以你當前的目錄來做顯示，）。</p>
<p><img src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-13.webp" width="1400" height="302" alt="以目錄形式呈現目前磁碟使用狀況（ncdu）（圖 13）" loading="lazy" decoding="async"></p>
<p>如果你想要掃描根目錄硬碟使用狀況，請使用 <code>ncdu -x /</code>，若要退出請按 <code>q</code> 。</p>
<p><img src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-14.webp" width="1400" height="322" alt="以目錄形式呈現目前磁碟使用狀況（ncdu）（圖 14）" loading="lazy" decoding="async"></p>
<blockquote>
<p>此指令並非預設安裝，Ubuntu 環境下的安裝指令為： <code>apt-get -y install ncdu</code></p>
</blockquote>
<h3 id="-搜尋檔案find">搜尋檔案（find）<a class="h-anchor" href="#-%e6%90%9c%e5%b0%8b%e6%aa%94%e6%a1%88find" aria-label="這個段落的連結"></a></h3>
<p>假使我們想從根目錄開始搜尋「php.ini」在哪裡：</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>sudo find / -name php.ini
</span></span></code></pre></div><p><img src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-15.webp" width="1400" height="335" alt="搜尋檔案（find）（圖 15）" loading="lazy" decoding="async"></p>
<p>如果我們不確定檔名，也可以透過「?」來進行模糊搜尋：</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>sudo find / -name ph?.ini
</span></span></code></pre></div><p><img src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-16.webp" width="1400" height="326" alt="搜尋檔案（find）（圖 16）" loading="lazy" decoding="async"></p>
<h3 id="-搜尋歷史指令controlr">搜尋歷史指令（Control+R）<a class="h-anchor" href="#-%e6%90%9c%e5%b0%8b%e6%ad%b7%e5%8f%b2%e6%8c%87%e4%bb%a4controlr" aria-label="這個段落的連結"></a></h3>
<p>有時我們會用到帶有一長串噁心的參數的指令，這些指令只要下過一次後 history 就會記住。</p>
<p>因此我們下次想要再使用它時，只要按下「Control+R」輸入要搜尋的字串，bash 就會顯示最近一項匹配的指令；連續按「Control+R」則繼續搜尋更早的歷史指令，按「Enter」則會執行顯示的指令。</p>
<figure class="figure figure-video">
  <video class="gifv" muted loop playsinline controls preload="none" poster="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-17.webp" width="960" height="645" aria-label="搜尋歷史指令（Control&#43;R）（圖 17）" data-gifv>
    <source src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-17.mp4" type="video/mp4">
  </video>
</figure>

<hr>
<h2 id="-檢查網路環境">檢查網路環境<a class="h-anchor" href="#-%e6%aa%a2%e6%9f%a5%e7%b6%b2%e8%b7%af%e7%92%b0%e5%a2%83" aria-label="這個段落的連結"></a></h2>
<p>想要知道網路彼此環境的狀況，除了用最基礎的 <code>ping</code> 以外，下面這些指令可以幫你獲得更詳盡的資訊。</p>
<h3 id="-網路診斷瑞士刀nc">網路診斷瑞士刀（nc）<a class="h-anchor" href="#-%e7%b6%b2%e8%b7%af%e8%a8%ba%e6%96%b7%e7%91%9e%e5%a3%ab%e5%88%80nc" aria-label="這個段落的連結"></a></h3>
<p>Netcat （nc）能夠做到的事情很多，這邊跟大家分享常用的功能。</p>
<ul>
<li><strong>測試目標 Server 特定的 port 是否開啟： <code>nc -v [host] [port]</code></strong></li>
</ul>
<p><img src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-18.webp" width="1400" height="256" alt="網路診斷瑞士刀（nc）（圖 18）" loading="lazy" decoding="async"></p>
<ul>
<li><strong>在兩台 Server 間複製檔案</strong></li>
</ul>
<p>在接收檔案的 Server 上執行 <code>nc -l 5000 &gt; demo.js</code> ，在傳送檔案的 Server 上執行 <code>nc [host] 5000 &lt; demo.js</code></p>
<p><img src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-19.webp" width="1400" height="190" alt="網路診斷瑞士刀（nc）（圖 19）" loading="lazy" decoding="async"></p>
<p>這個操作跟 <code>scp</code> 很類似，但特別的地方在於不需要登入的動作（不需要帳號密碼），假使遇到兩台 Server 無法互相登入時， 他就是一個解決方案。</p>
<blockquote>
<p>Netcat 還有非常多的功能，有興趣的可以再自行鑽研。</p>
</blockquote>
<h3 id="-設定好-dns-後確認回應是否正常nslookup">設定好 DNS 後，確認回應是否正常（nslookup）<a class="h-anchor" href="#-%e8%a8%ad%e5%ae%9a%e5%a5%bd-dns-%e5%be%8c%e7%a2%ba%e8%aa%8d%e5%9b%9e%e6%87%89%e6%98%af%e5%90%a6%e6%ad%a3%e5%b8%b8nslookup" aria-label="這個段落的連結"></a></h3>
<p>下面以查詢 google.com 的 ip 為例： <code>nslookup google.com</code></p>
<p><img src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-20.webp" width="1156" height="324" alt="設定好 DNS 後，確認回應是否正常（nslookup）（圖 20）" loading="lazy" decoding="async"></p>
<p>在執行結果中「192.168.220.186」是查詢的 DNS Server，而「172.217.160.110」則是返回的 ip。</p>
<blockquote>
<p>在內網/外網執行時會有不同回應，可用來確認設定是否符合預期。</p>
</blockquote>
<h3 id="-查詢佔用的-portlsof">查詢佔用的 port（lsof）<a class="h-anchor" href="#-%e6%9f%a5%e8%a9%a2%e4%bd%94%e7%94%a8%e7%9a%84-portlsof" aria-label="這個段落的連結"></a></h3>
<p>lsof （List Open Files）可以列出所有被行程打開的檔案，筆者會透過這個指令來看 port 被哪個行程佔用了。</p>
<pre tabindex="0"><code>lsof -i -P | grep :443
</code></pre><p><code>-i</code> 是查找網路連線， <code>-P</code> 是列出實際的 port，最後透過 <code>grep</code> 將 port 為 443 的顯示出來。</p>
<p><img src="https://deanlin.net/blog/202208/useful-linux-commands/images/useful-linux-commands-21.webp" width="1400" height="127" alt="查詢佔用的 port（lsof）（圖 21）" loading="lazy" decoding="async"></p>
<blockquote class="pullquote"><p>以上是筆者常用的 Linux 指令，如果你平常有用到其他實用的指令，也歡迎留言分享喔！</p>
</blockquote>
]]></content:encoded>
    </item>
  </channel>
</rss>
