作業系統原理:Linux Commands Extension
- DevOps 資訊 | 是時候升級你的命令行了
- bat > cat
- prettyping > ping
- fzf > ctrl+r
- htop > top
- diff-so-fancy > diff
- fd > find
- ncdu > du
- tldr > man
- ack || ag > grep
- jq > grep et al
DevOps 資訊 | 是時候升級你的命令行了
子貢問為仁。
子曰:“工欲善其事,必先利其器。居是邦也,事其大夫之賢者,友其士之仁者。”
——《論語·衛靈公》
bat > cat
cat 被用於列印檔案內容。ccat 工具還提供像語法高亮顯示這樣的功能。在此基礎之上,bat 還支援 分頁, 行號和 git 整合。同時允許在輸出中搜索 (當輸出長於螢幕高度時) 。更多資訊: ofollow,noindex">https://github.com/sharkdp/bat
# Linux wget https://github.com/sharkdp/bat/releases/download/v0.6.1/bat-v0.6.1-x86_64-unknown-linux-gnu.tar.gz make install # Mac brew install bat # alias cat='bat'
prettyping > ping
ping 是一種非常有用的網路工具。原理是向目標主機傳出一個ICMP echo@要求資料包,並等待接收 echo 迴應資料包。程式會按時間和成功響應的次數估算丟失資料包率(丟包率)和資料包往返時間(網路時延,Round-trip delay time)。不過預設的 ping 命令輸出比較乏味。prettyping 則提供了更友好、更美觀的輸出,包括彩色點圖表示網路連通性。prettyping 基於 bash 和 awk 編寫能,夠相容大部分作業系統 (例如 Linux, BSD, Mac OS X, …)。更多資訊: http://denilson.sa.nom.br/prettyping/
curl -O https://raw.githubusercontent.com/denilsonsa/prettyping/master/prettyping chmod +x prettyping ./prettyping baiud.com alias ping='prettyping --nolegend'
fzf > ctrl+r
在終端中使用 ctrl + r 組合鍵可以向後搜尋歷史操作記錄( 儘管有點繁瑣 )。fzf 工具是對 ctrl + r 的增強。支援對終端操作歷史的模糊搜尋, 預覽可能的匹配結果。除了歷史搜尋之外, fzf 還可以預覽和開啟檔案。
更多資訊: https://github.com/junegunn/fzf
# Linux git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf ~/.fzf/install # Mac brew install fzf # alias alias preview="fzf --preview 'bat --color \"always\" {}'" # add support for ctrl+o to open selected file in VS Code export FZF_DEFAULT_OPTS="--bind='ctrl-o:execute(code {})+abor
htop > top
top 是一個快速診斷系統性能的工具。值得一提的是 top for Mac 與 Linux 上的輸出不太一樣。htop 優化了頂部輸出格式,並支援大量的顏色, 鍵盤快鍵鍵和檢視, 幫助我們洞察程序行為。更多資訊: http://hisham.hm/htop/
htop 提供的鍵盤快捷鍵:
- P - 按 CPU 利用率排序
- M - 按記憶體利用率排序
- F4 - 按字串過濾程序
- space - 高亮顯示某一程序,便於持續跟蹤
alias top="sudo htop" # alias top
diff-so-fancy > diff
GIT 版本控制系統中使用 git diff 來顯示兩個版本之間差別(包括檔案、元資料和改動等)。diff-so-fancy 是一個用 node.js 實現的命令列工具,提供更友好的輸出樣式和定製能力。更多資訊: https://github.com/so-fancy/diff-so-fancy
# download wget https://raw.githubusercontent.com/so-fancy/diff-so-fancy/master/third_party/build_fatpack/diff-so-fancy chmod +x diff-so-fancy # npm npm install -g diff-so-fancy # 直接呼叫 git diff --color | diff-so-fancy
在 git diff 和 git show 中啟用 diff-so-fancy ,需要修改 gitconfig :
[pager] diff = diff-so-fancy | less --tabs=1,5 -RFX show = diff-so-fancy | less --tabs=1,5 -RFX
除了預設樣式優化,diff-so-fancy 還支援顏色和顯示項定製,例如:
git config --global color.ui true git config --global color.diff-highlight.oldNormal"red bold" git config --global color.diff-highlight.oldHighlight "red bold 52" git config --global color.diff-highlight.newNormal"green bold" git config --global color.diff-highlight.newHighlight "green bold 22" git config --global color.diff.meta"yellow" git config --global color.diff.frag"magenta bold" git config --global color.diff.commit"yellow bold" git config --global color.diff.old"red bold" git config --global color.diff.new"green bold" git config --global color.diff.whitespace "red reverse" git config --bool --global diff-so-fancy.markEmptyLines false
fd > find
fd 非常快。有意思的是 fd 與 bat 的作者是同一個人(David Peter)。更多資訊: https://github.com/sharkdp/fd/
常用的命令列:
## SourceCode git clone https://github.com/sharkdp/fd cd fd cargo build cargo test cargo install # Mac brew install fd ## Usage fd cli # 查詢所有包含"cli"的檔名 fd -e md # 查詢所有 .md 檔案 fd cli -x wc -w # 查詢 "cli" 並執行 `wc -w`
ncdu > du
瞭解磁碟空間佔用情況是一項非常重要的工作。常用的命令是 du -sh (-sh 表示摘要、便於人工閱讀), 但我們經常需要挖掘目錄的空間佔用。ncdu 是一個替代選擇(完全基於 C 語言編寫,MIT 許可證)。ncdu 提供了一個互動式介面, 支援快速掃描哪些資料夾或檔案佔用空間, 並且導航非常方便。更多資訊: https://dev.yorhel.nl/ncdu
git clone git://g.blicky.net/ncdu.git/ # release wget https://dev.yorhel.nl/download/ncdu-1.13.tar.gz tar -xvf ncdu-1.13.tar.gz ./configure make make install # Usage ncdu path
alias du="ncdu --color dark -rr -x --exclude .git --exclude node_modules" # 擴充套件選項 --color dark - use a colour scheme -rr - read-only mode (prevents delete and spawn shell) --exclude ignore directories I won't do anything about
tldr > man
幾乎每一個命令列工具都可以通過手工輸入 man 命令獲得幫助資訊。TL;DR 專案(“too long; didn’t read”)是一個由社群驅動的命令列文件系統,以非常簡潔的方式提供命令列引數列表、使用說明和示例。更多資訊: https://tldr.sh/
# Install npm install -g tldr alias help='tldr' # Usage Options: -V, --versionoutput the version number -l, --listList all commands for the chosen platform in the cache -a, --list-allList all commands in the cache -1, --single-columnList single command per line (use with options -l or -a) -r, --randomShow a random command -e, --random-exampleShow a random example -f, --render [file]Render a specific markdown [file] -m, --markdownOutput in markdown format -o, --os [type]Override the operating system [linux, osx, sunos] --linuxOverride the operating system with Linux --osxOverride the operating system with OSX --sunosOverride the operating system with SunOS -t, --theme [theme]Color theme (simple, base16, ocean) -s, --search [keywords]Search pages using keywords -u, --updateUpdate the local cache -c, --clear-cacheClear the local cache -h, --helpoutput usage information # Example bash-3.2$ tldr tar ✔ Page not found. Updating cache ✔ Creating index tar Archiving utility. Often combined with a compression method, such as gzip or bzip. - Create an archive from files: tar cf target.tar file1 file2 file3 - Create a gzipped archive: tar czf target.tar.gz file1 file2 file3 - Extract an archive in a target folder: tar xf source.tar -C folder
ack || ag > grep
grep 無疑是一個強大的命令列工具, 但多年來它已被許多工具所取代,包括 ack 和 ag 。更多資訊:[ https://beyondgrep.com/ ]
curl https://beyondgrep.com/ack-2.24-single-file > ~/bin/ack && chmod 0755 ~/bin/ack
ack 和 ag 預設情況下使用正則表示式進行搜尋,可以指定檔案型別 —— 使用像 –js 或 –html 標誌搜尋。ack 和 ag 工具都支援 grep 選項, 如 -B (表示輸出匹配行和其之後(after)的N行)。
ack 預設沒有支援 markdown 格式,可以在 .ackrc 檔案定製:
--type-set=md=.md,.mkd,.markdown --pager=less -FRX
jq > grep et al
jq is like sed for JSON data
jq 可以作為 JSON 資料轉換工具。示例:更新節點依賴項 (分為多行以便於可讀性)。
更多資訊: https://stedolan.github.io/jq/
npm i $(echo $(\ npm outdated --json | \ jq -r 'to_entries | .[] | "\(.key)@\(.value.latest)"' \ ))
{ "node-jq": { "current": "0.7.0", "wanted": "0.7.0", "latest": "1.2.0", "location": "node_modules/node-jq" }, "uuid": { "current": "3.1.0", "wanted": "3.2.1", "latest": "3.2.1", "location": "node_modules/uuid" } }
[email protected] [email protected]