10 款你不知道的 Linux 環境下的替代工具!

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作者:JackTian","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"來源:公衆號「傑哥的IT之旅」","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ID:Jake_Internet","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"轉載請聯繫授權(微信ID:Hc220088)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原文地址:","attrs":{}},{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s/p9WlxEBfFj0h7IQIOa9Xqw","title":"","type":null},"content":[{"type":"text","text":"https://mp.weixin.qq.com/s/p9WlxEBfFj0h7IQIOa9Xqw","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大家好,我是傑哥。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 Linux 操作系統下,我們經常使用 cat 命令去連接多個文件並打印到標準輸出,合成幾個文件爲一個目標文件,追加幾個文件到目標文件中。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最近我在 GitHub 上發現了一個具有相似作用的命令叫做 “","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Bat","attrs":{}}],"attrs":{}},{"type":"text","text":"”,它是 cat 命令的替代品,具有一些例如","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"語法高亮","attrs":{}}],"attrs":{}},{"type":"text","text":"、 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Git 集成","attrs":{}}],"attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"自動分頁","attrs":{}}],"attrs":{}},{"type":"text","text":"等非常酷的特性。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在這篇文章中,就來給大家介紹下如何在 Linux 中安裝和使用 bat 命令。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"一、bat","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"安裝 bat","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"Arch Linux","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"bat 可以在 Arch Linux 操作系統的默認軟件源中進行獲取,可直接使用 pacman 命令在任何基於  Arch 的系統上來安裝它。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"pacman -S bat","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"Fedora","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"dnf install bat","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"macOS","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"brew install bat","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"Ubuntu 21.10","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我是通過 Ubuntu 環境下安裝的 bat,從 bat 的發佈頁面下載","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":".deb","attrs":{}}],"attrs":{}},{"type":"text","text":"文件,可執行如下命令進行下載和安裝。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"sudo apt-get install wget\nwget https://github.com/sharkdp/bat/releases/tag/v0.18.3/bat_0.18.3_amd64.deb\nsudo dpkg -i bat_0.18.3_amd64.deb","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於其他系統,也許需要從軟件源編譯並安裝,或參考 GitHub 上 bat 的其他操作系統安裝方式。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"bat 命令的使用","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"bat 命令的使用與 cat 命令的使用非常相似。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 bat 命令創建一個新文件:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"bat > jiaoben1.sh","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 bat 命令查看文件內容:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"bat jiaoben1.sh","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 bat 命令同時查看多個文件:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"bat jiaoben1.sh jiaoben2.sh","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 bat 命令將多個文件的內容合併到一個單獨的文件中:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"bat jiaoben1.sh jiaoben2.sh > test.txt","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上述介紹了 bat 創建新文件、查看文件內容、將多個文件的內容合併到一個單獨的文件中的方式。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來,我們就來看一下 bat 命令一些非常酷的特性。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"bat 命令支持大多數編程和標記語言的語法高亮,那麼我們就來使用 cat 和 bat 命令展示下 jiaoben1.sh 裏面的內容做個鮮明的對比。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c0/c09b2479b8cb4e22a8049644aed2e885.gif","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以看出,cat 命令以純文本格式顯示文件的內容,而 bat 命令顯示了語法高亮和整齊的文本對齊格式。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從上面 bat 命令的輸出,可以看出是有表格線的,如果你只想顯示行號(而沒有表格)可以使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"-n","attrs":{}}],"attrs":{}},{"type":"text","text":"參數來實現,雖然 cat 執行也會顯示行號,但還是沒有 bat 執行輸出後的效果美觀。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"bat -n jiaoben2.sh","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/0f/0f87a007ded1c2b15457e73b720ab31b.gif","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"bat 命令支持 Git 集成,可以輕鬆查看和編輯 Git 存儲庫中的文件,bat 與 Git 連接可顯示關於索引的修改。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a0/a0b3679df51ebf393d1073c839392d5d.webp","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"bat 支持自動分頁,這點特性也是需要值得注意的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"支持分頁意思也就是說,當文件的輸出內容超出屏幕顯示的時候,bat 命令會自動將自己輸出的內容傳到 less 命令中,你將可以使用回車鍵一頁一頁的查看輸出內容,使用空格鍵可直接跳轉至文末最後一行。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當你使用 bat 命令時這些都是不必要的,bat 命令將自動傳輸跨越多個頁面的文件的輸出。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"bat jiaoben3.sh","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c7/c797db5fc451b18158096e2268610a63.gif","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對比下 cat 命令,查看 jiaoben3.sh 跨多個頁面文件內容時,提示符會直接跳到文件的最後一頁,將無法看到跨多個頁面文件開頭及中間部分的內容。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"cat jiaoben3.sh","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/dc/dc57b7c0be968bf78f057a19ac5545c2.gif","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你想要通過 cat 來實現 bat 命令的效果,則需要去使用 cat 的輸出傳輸到 less 命令中去,這樣就可以從開頭部分一頁一頁的查看內容了。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"cat jiaoben3.sh | less","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/0d/0d2e110d8921443c0a1869f092eb5cca.gif","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同樣,這種方式也是可以使用回車鍵一頁一頁的查看輸出內容以及使用空格鍵可直接跳轉至文末最後一行。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"bat 定製主題","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"目前 bat 定製主題有 24 種,包括:Coldark-Cold、GitHub、Monokai Extended Light、OneHalfDark、Solarized (light)、TwoDark、Visual Studio Dark+、base16、zenburn 等;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你不喜歡默認主題,也是可以根據自己的風格進行修改,只需執行:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"bat --list-themes","attrs":{}}],"attrs":{}},{"type":"text","text":"命令,即可顯示可用的主題。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"# bat --list-themes\nTheme: 1337\n\n // Output the square of a number.\n fn print_square(num: f64) {\n let result = f64::powf(num, 2.0);\n println!(\"The square of {:.2} is {:.2}.\", num, result);\n }\n\n......","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如你要選擇","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Dracula","attrs":{}}],"attrs":{}},{"type":"text","text":"主題,可直接執行:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"bat --theme=Dracula 文件名","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/40/4060de17c2f2ecd8ffa2b8989d85988f.webp","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你想永久改變某一個主題,可使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"export BAT_THEME=\"Dracula\"","attrs":{}}],"attrs":{}},{"type":"text","text":"在你的 shell 啓動文件,以使更改爲永久。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"您可以使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"--style","attrs":{}}],"attrs":{}},{"type":"text","text":"參數選項來控制","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"bat","attrs":{}}],"attrs":{}},{"type":"text","text":"輸出的外觀,配置元素包括:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"行號、文件頭、網格邊框、Git修改","attrs":{}}],"attrs":{}},{"type":"text","text":"來顯示除文件內容之外的東西,參數是一個逗號分隔的列表。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"--style=numbers,changes","attrs":{}}],"attrs":{}},{"type":"text","text":"可以使用僅顯示 Git修改和行號,而不顯示網格和文件標題,設置","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"BAT_STYLE","attrs":{}}],"attrs":{}},{"type":"text","text":"環境變量以使這些更改爲永久。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"關於更多 bat 的使用方法可通過執行命令","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"man bat","attrs":{}}],"attrs":{}},{"type":"text","text":"或","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"bat --help","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除此之外,也可以通過 bat 項目的 GitHub 庫查看更多詳細信息,我用過 bat 後就不會再想用回 cat 了!","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"二、duf","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 Linux 環境下,我們知道 df 命令是用於顯示磁盤分區上可使用的磁盤空間,默認顯示單位爲 KB,你還可以利用該命令來獲取硬盤被佔用了多少空間,目前還剩下多少空間等信息。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# df\nFilesystem     1K-blocks    Used Available Use% Mounted on\n/dev/sda3       18439940 4943264  12559972  29% /\ntmpfs             506176      76    506100   1% /dev/shm\n/dev/sda1          99150   30003     64027  32% /boot\n/dev/sr0         4363088 4363088         0 100% /media/CentOS_6.5_Final","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除此之外,du 命令也是查看使用空間的,但與 df 命令不同的是 Linux du 命令是對文件和目錄磁盤使用的空間的查看,和 df 命令還是有一些區別的。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# du\n4    ./.abrt\n8    ./.dbus/session-bus\n12    ./.dbus\n4    ./.nautilus\n40    ./.local/share/gvfs-metadata\n24    ./.local/share/applications\n68    ./.local/share\n72    ./.local","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來就跟大家介紹一款終端工具,你可以理解爲傳統的 Linux 命令 df 和 du 整合版 —— duf。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"duf 是一個用 Golang 編寫的跨平臺磁盤使用情況工具,可以讓你輕鬆地檢查可用磁盤空間,對輸出進行分類,並以用戶友好的方式進行呈現,適用於 Linux、Windows、macOS、Android、FreeBSD;","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"Ubuntu 安裝方式:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ sudo apt install gdebi\n#如果報錯,Failed to fetch http://security.ubuntu.com......\n# sudo apt-get update\n\n$ sudo gdebi duf_0.5.0_linux_amd64.deb\n#要先在下載duf_0.5.0_linux_amd64.deb,下載地址爲:https://github.com/muesli/duf/releases/download/v0.5.0/duf_0.5.0_linux_amd64.deb","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"MacOS 安裝方式:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"#先安裝Homebrew,按照提示安裝即可。\n/bin/zsh -c \"$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)\"\n\n#安裝duf\nbrew install duf\n\n#或者\nsudo port selfupdate && sudo port install duf","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"Windows 安裝方式:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"choco install duf\n\n#或者\n\nscoop install duf","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"安裝完畢後,可直接使用 duf,方法也非常簡單。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不添加任何參數,會輸出所有本地設備、已掛載的任何雲存儲設備以及任何其他特殊設備(包括臨時存儲位置等)的詳細信息;","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"$ duf","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f1/f1b19791db4754f887014fc00d44b6fa.webp","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你只想查看本地連接設備的詳細信息,而不想看其他的,可執行:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"$ duf --only local","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/4d/4db04c8543c5a55f804ae04a1d43a5bb.webp","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你只想根據大小按特定順序對輸出信息進行排序,可執行:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"$ duf --sort size","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/be/beb67ee9bd32771874e1bb979b3fb2a4.webp","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"更多參數使用方式:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"duf /home /some/file 根據參數,則 duf 將僅列出特定的設備和安裝點\nduf --all 列出所有內容\nduf --hide-network 隱藏網絡文件系統,與之對應的 --hide-fuse --hide-special --hide-loops --hide-binds\nduf --inodes 列出inodes\nduf --output mountpoint,size,usage 指定輸出的格式 對應的還有(mountpoint, size, used, avail, usage, inodes, inodes_used, inodes_avail, inodes_usage, type, filesystem)\nduf --json 以json格式輸出\nduf --theme light 如果 duf 無法正確檢測終端的顏色,可以設置一個主題\nduf --help 查看所有 duf 的可用命令","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"三、tldr","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於初學者學習 Linux 來說,遇到最多的問題就是在 Linux 操作系統下有記不完的命令以及包括各種參數的使用方法,其實你根本不用死記硬背它,當你用到時去查找相關資料或通過","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"man","attrs":{}}],"attrs":{}},{"type":"text","text":"命令來查看相關文檔,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"man","attrs":{}}],"attrs":{}},{"type":"text","text":"手冊輸出的信息也比較多,而對於參數較多的命令來說,翻看","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"man","attrs":{}}],"attrs":{}},{"type":"text","text":"手冊也非常喫力。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e6/e6813d76f82d12ef95d9ba4af4dfdf3f.gif","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這次就給大家介紹一款工具:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"tldr","attrs":{}}],"attrs":{}},{"type":"text","text":",它的全稱是:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Too Long, Don't Read","attrs":{}}],"attrs":{}},{"type":"text","text":",中文意思:太長不看的意思。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其實跟","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"man","attrs":{}}],"attrs":{}},{"type":"text","text":"命令的結果基本上就是太長不看,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"tldr","attrs":{}}],"attrs":{}},{"type":"text","text":" 這個工具你可以理解爲簡化了 man 命令的幫助文檔,把 Linux 命令最常用的一些命令用法列舉出來,非常容易看懂。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同時,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"tldr","attrs":{}}],"attrs":{}},{"type":"text","text":"包括非常多語言編寫的客戶端,比如:C、Go、iOS、Python、nodejs 等等,你可以使用不同的客戶端來安裝;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我這裏的環境是 Ubuntu,需要先安裝 Node.js,然後根據下述安裝方式操作:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"sudo apt-get install nodejs\n\nsudo apt-get install npm\n\nsudo npm install -g tldr","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"執行完上述三條命令後,還需要再執行","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"tldr --update","attrs":{}}],"attrs":{}},{"type":"text","text":",更新","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"tldr","attrs":{}}],"attrs":{}},{"type":"text","text":"至最新的狀態,然後就可以直接使用了;","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"tldr --update","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"tldr 的使用:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"tldr 命令的使用也非常簡單,只需 tldr + 你所要查詢的命令即可;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/59/595d09e91029090399e4b22d09fb52f5.gif","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"四、htop","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 Linux 操作系統上顯示進程運行狀態信息最常用工具是我們熟悉的 top,它是每位系統管理員的好幫手。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"htop 可以說是 top 的絕佳替代品,它是用 C 寫的,是一個跨平臺的交互式的進程監控工具,具有更好的視覺效果,一目瞭然更容易理解當前系統的狀況,允許垂直和水平滾動進程列表以查看它們的完整命令行和相關信息,如內存和 CPU 消耗。還顯示了系統範圍的信息,例如平均負載或交換使用情況。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"顯示的信息可通過圖形設置進行配置,並且可以交互排序和過濾,與進程相關的任務(例如終止和重新處理)可以在不輸入其 PID 的情況下進行完成。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"安裝也很簡單,只需執行命令:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"apt install htop","attrs":{}}],"attrs":{}},{"type":"text","text":"即可完成。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/05/055389ed1fcf0803b707a0f941ab710f.gif","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"htop 比起 top,優勢很明顯,除了功能更加完善以外,它的色彩搭配也很豐富,整體上使用起來更加友好。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"五、glances","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Glances 是用 Python 寫的一個跨平臺的監控工具,旨在通過 curses 或基於 Web 的界面呈現大量系統監控信息,該信息根據用戶界面的大小動態調整,是 GNU/Linux、BSD、Mac OS 和 Windows 操作系統的 top/htop 替代品。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"它可以在客戶端/服務器模式下工作,遠程監控可以通過終端、Web 界面或 API(XML-RPC 和 RESTful)完成,統計數據也可以導出到文件或外部時間/值數據庫。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除了列出所有進程及其 CPU 和內存使用情況之外,它還可以顯示有關係統的其他信息,比如:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"網絡及磁盤使用情況","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"文件系統已使用的空間和總空間","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"來自不同傳感器(例如電池)的數據","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以及最近消耗過多資源的進程列表","attrs":{}}]}]}],"attrs":{}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6e/6e494d0d619602db6740982711f741ad.gif","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"六、exa","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"提到 ls 命令,大家都不陌生,在 Linux 環境下,其主要作用:列出當前目錄下所包含的文件及子目錄,如果當前目錄下文件過多,則使用命令 ls 不是很好,因爲這輸出出來的結果跟你所要查找的文件未能達成一致,第一:需要進行二次過濾查找;第二:文件過多時,終端輸出結果較慢;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"EXA 是 Unix 和 Linux 操作系統附帶的命令行程序的 ls 現代替代品,賦予它更多功能和更好的默認值。它使用顏色來區分文件類型和元數據。它瞭解符號鏈接、擴展屬性和 Git。它","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"體積小","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"速度快","attrs":{}}],"attrs":{}},{"type":"text","text":",而且只有一個","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"二進制文件","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"EXA 與 LS 有什麼不同之處?","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"終端屏幕不再是瓶頸:exa 功能強大,比 ls 要友好,輸出結果會更快;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"顏色:exa 大量使用顏色;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不與 ls 命令衝突","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"EXA 的特徵","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"色彩:文件類型列表以及如何區分文件;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"網格視圖:在與終端窗口大小相同的網格中顯示文件;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"長遠:在表中顯示文件及其元數據;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"樹視圖:在樹中顯示文件及其子代;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Git 集成:在存儲庫中時,查看文件的 Git 狀態;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"篩選:隱藏列表中的文件,顯示不可見的文件,並對輸出進行排序;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Xattrs:列出文件的擴展屬性;","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Ubuntu 21.10 安裝只需執行命令:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"apt install exa","attrs":{}}],"attrs":{}},{"type":"text","text":"即可。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"顯示選項","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-1,--oneline:每行顯示一個條目","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-G,--grid:將條目顯示爲網格(默認)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-l,--long:顯示擴展細節和屬性","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-R,--recurse:遞歸到目錄","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-T,--tree:作爲樹遞歸到目錄中","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-x,--across:對網格進行排序,而不是向下排序","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-F,--classify:按文件名顯示類型指示器","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"--colo[u]r:何時使用終端顏色","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"--colo[u]r-scale : 清楚地突出顯示文件大小的級別","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"--icons : 顯示圖標","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"--no-icons : 不顯示圖標(總是覆蓋 --icons)","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"過濾選項","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-a,--all:顯示隱藏和“點”文件","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-d,--list-dirs:像普通文件一樣列出目錄","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-L,--level=(depth):限制遞歸深度","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-r,--reverse:反轉排序順序","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-s,--sort=(field):按哪個字段排序","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"--group-directories-first:在其他文件之前列出目錄","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-D,--only-dirs:只列出目錄","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"--git-ignore:忽略中提到的文件.gitignore","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-I,--ignore-glob=(globs):要忽略的文件的全局模式(管道分隔)","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"傳遞該","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"--all","attrs":{}}],"attrs":{}},{"type":"text","text":"選項兩次以同時顯示","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":".","attrs":{}}],"attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"..","attrs":{}}],"attrs":{}},{"type":"text","text":"目錄。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/4b/4bd01bd6ef8c2edd57f3fee25a0d7c79.webp","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"關於 EXA 更多可參考:","attrs":{}},{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s?__biz=MzAwMjg1NjY3Nw==&mid=2247488292&idx=1&sn=5bec2375a46edd32f3f78aae796005ea&scene=21#wechat_redirect","title":"","type":null},"content":[{"type":"text","text":"https://mp.weixin.qq.com/s?__biz=MzAwMjg1NjY3Nw==&mid=2247488292&idx=1&sn=5bec2375a46edd32f3f78aae796005ea&scene=21#wechat_redirect","attrs":{}}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"七、fd","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"fd 是一個在文件系統中查找條目的程序,它是 find 命令的一個簡單、快速且用戶友好的替代品,fd 目的不是取代 find 命令所提供的全部功能,而是在多數用例中提供了合理的默認值,在某些情況下非常有用。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"fd 輸出是彩色的,類似於某些 ls 模式,它是遞歸的,在包含 Git 存儲庫的目錄中搜索源代碼文件時,fd 會自動排除隱藏的文件和目錄,包括","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":".","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":".git","attrs":{}}],"attrs":{}},{"type":"text","text":"目錄,並忽略","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":".gitignore","attrs":{}}],"attrs":{}},{"type":"text","text":"文件中的模式。一般來說,搜索速度更快,而且第一次搜索時會提供了更多相關結果。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"默認情況下,fd 在當前目錄中執行不區分大小寫的模式搜索,但是,如果你的搜索模式包含一個大寫字母,fd 將以區分大小寫的模式進行執行。當然,也可以重寫默認值,但在許多情況下還是有利的。同樣使用 find 進行相同的搜索需要你提供額外的命令行參數。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"fd 可用於許多 Linux 發行版,執行以下命令,可以使用標準存儲庫在 Ubuntu 中安裝:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"sudo apt install fd-find","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 Ubuntu 操作系統中,命令是 fdfind,避免與其他現有的實用程序名稱發生衝突,如果希望它是 fd,可以設置一個別名:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"alias fd=fdfind","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要使別名保持不變,在重啓後仍然可用,請將其放入“","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":".bashrc","attrs":{}}],"attrs":{}},{"type":"text","text":"”或“","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":".bash_aliases","attrs":{}}],"attrs":{}},{"type":"text","text":"”文件中。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用不帶參數選項的 fd,則類似於 ls,不同之處在於它默認情況下也會將子目錄中的文件進行列出。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"fd\nbat_0.18.3_amd64.deb\nfd_8.3.0_amd64.deb\njiaoben1.sh\njiaoben2.sh\njiaoben3.sh\nsnap\nsnap/firefox\nsnap/firefox/631\nsnap/firefox/731\nsnap/firefox/common\nsnap/firefox/current\nsnap/snap-store\nsnap/snap-store/557\nsnap/snap-store/558\nsnap/snap-store/common\nsnap/snap-store/current\ntest.md","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在當前目錄中搜索所有的 markdown 文件(.md 或.MD ),find 命令如下:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"find . -iname \"*.md\"\n./test.md","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 fd 進行同樣的搜索:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"fd *.md\ntest.md","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在某些情況下,fd 需要額外的選項;例如,如果你想包含隱藏文件和目錄,則必須使用選項 -H,而這在 find 中是不需要的,關於 fd 更多命令行參數選項的使用方法,可查閱 fd GitHub 存儲庫。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"八、ag","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"ack","attrs":{}}],"attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ag","attrs":{}}],"attrs":{}},{"type":"text","text":"是兩個文本搜索工具,比自帶的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"grep","attrs":{}}],"attrs":{}},{"type":"text","text":"要好用得多。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在指定目錄下搜索文本時,不需要像 grep 那樣指定各種命令行選項,輸出結果也會包含文件名和行號,並且會高亮顯示搜索關鍵字。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"ag","attrs":{}}],"attrs":{}},{"type":"text","text":"是可以比","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"grep","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ack","attrs":{}}],"attrs":{}},{"type":"text","text":"更快的遞歸搜索文件內容。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Ubuntu 21.10 安裝 ag 和 ack","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"ack和ag是兩個文本搜索工具,比自帶的grep要好用得多。\n\n在指定目錄下搜索文本時,不需要像 grep 那樣指定各種命令行選項,輸出結果也會包含文件名和行號,並且會高亮顯示搜索關鍵字。\n\nag是可以比grep、ack更快的遞歸搜索文件內容。\n\nUbuntu 21.10 安裝 ag 和 ack","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"ag 命令常用參數","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ag -g (文件名):類似於 find . -name (文件名)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ag -i sed:忽略大小寫搜索含 sed 文本","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ag -A sed:搜索含 sed 文本,並顯示匹配內容之後的 n 行文本","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ag -B sed:搜索含 sed 文本,並顯示匹配內容之前的 n 行文本","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ag -C sed:搜索含 sed 文本,並同時顯示匹配內容以及它前後各 n 行文本的內容","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ag -w sed:全匹配搜索,只搜索與所搜內容完全匹配的文本","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ag --java sed:在 java 文件中搜索含 sed 的文本","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ag --xml sed:在 XML 文件中搜索含 sed 的文本","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"ag 命令的使用","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"根據文件名進行搜索對應的文件,可使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"-g","attrs":{}}],"attrs":{}},{"type":"text","text":"參數選項。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"ag -g jiaoben\njiaoben2.sh\njiaoben1.sh\njiaoben3.sh","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"搜索當前目錄下有關 sed 關鍵字的文本。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"ag -w sed\njiaoben1.sh\n10:                sed -i '2d' \"$new_filename\"","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/12/12b9855dd1e17edc5d21615474e46e1c.webp","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"更多參數選項,大家可根據自身實際場景進行運用。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"九、axel","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"axel 是命令行多線程下載工具,下載文件時可以替代 curl、wget。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Ubuntu 21.10 安裝 axel","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"apt-get install axel","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"axel 參數選項","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-n:指定線程數","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-o:指定另存爲目錄","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-s:指定每秒的最大比特數","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"-q:靜默模式","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"案例","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# axel -n 10 -o /tmp/ http://mirrors.163.com/ubuntu/ls-lR.gz\n# exa /tmp/ls-lR.gz\n/tmp/ls-lR.gz","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以看出,我們通過 axel 也是可以把 ls-lR.gz 這個文件正常下載到 /tmp 目錄下的。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"十、pydf","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 Linux 系統下,我們可以使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"df","attrs":{}}],"attrs":{}},{"type":"text","text":"命令來顯示磁盤的相關信息。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# df\n文件系統          1K-塊     已用    可用 已用% 掛載點\ntmpfs            199180     1660  197520    1% /run\n/dev/sda3      19946096 10317864 8589692   55% /\ntmpfs            995884        0  995884    0% /dev/shm\ntmpfs              5120        4    5116    1% /run/lock\n/dev/sda2        524252     5344  518908    2% /boot/efi\ntmpfs            199176      100  199076    1% /run/user/1000\n/dev/sr0        3043440  3043440       0  100% /media/jacktian/Ubuntu 21.10 amd64\ntmpfs            199176       60  199116    1% /run/user/0","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Ubuntu 21.10 安裝 pydf","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"apt install pydf","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"pydf 可以說是 df 的替代品,它以更簡潔的方式顯示磁盤使用狀態。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b6/b60ea56575498f3aef43f3fe463eed67.webp","alt":"圖片","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"參考","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"bat:https://github.com/sharkdp/bat","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"duf:https://github.com/muesli/duf","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"tldr:https://github.com/tldr-pages/tldr","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"htop:https://github.com/htop-dev/htop","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"glances:https://github.com/nicolargo/glances","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"exa:https://github.com/ogham/exa","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"fd:https://github.com/sharkdp/fd/","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"最後","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"雖然我們在日常工作中,大多數還都是用的默認基本命令工具,當如果你替換成上述所介紹的 10 款替代品工具時,相信在許多場景中還是非常有用的。當然了,可能還有更多的 Linux 命令行替代工具,也歡迎大家在下面的留言區補充。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以上就是今天所要分享的全部內容了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你覺得這篇文章對你有點用的話,爲本文","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"點個贊","attrs":{}},{"type":"text","text":" or ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"在看","attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"留個言","attrs":{}},{"type":"text","text":"或者","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"轉發","attrs":{}},{"type":"text","text":"一下,讓更多的朋友看到,因爲這將是我持續輸出更多優質文章的最強動力!","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​ ","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/01/019b65e5ec6661e8bbcaec704494797d.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章