[筆記] Command Line 超新手入門
前言
此篇筆記為參加程式導師實驗計畫中,整理在 Lidemy [CMD101] Command Line 超新手入門 的課程筆記。
簡介
What is Command-Line Interface?
命令列介面(英語:Command-Line Interface,縮寫:CLI)是在圖形使用者介面得到普及之前使用最為廣泛的使用者介面,它通常不支援滑鼠,用戶通過鍵盤輸入指令,電腦接收到指令後,予以執行。也有人稱之為文字使用者介面(character user interface, CUI)。by Wiki-命令列介面
簡言之,就是以文字的方式跟電腦溝通。
而以下常見的名稱都是指 CLI,只是在不同作業系統下,有不同稱呼:
- 類 Unix 系統:bash / sh / ksh / csh / zsh
- Windows 系統:cmd.exe / 命令提示字元 / Windows PowerShell
- MS-DOS系統:COMMAND.COM
關於更多 CLI 的介紹,可以參考:
Why is Command-Line Interface?
在 GUI 盛行的年代,為什麼要學 CLI 呢?
使用 CLI 有以下的好處:
- 較節約電腦系統的資源
- 有時可以更高效與精準地達到目的
舉簡單的例子來說,如果你只會 GUI,但今天要用別人的電腦工作,而對方沒有安裝相關的 GUI,或是他安裝的 GUI 跟你的不同,就得再去下載你熟悉的 GUI,或花時間去了解對方的 GUI 如何操作;又如果連線到只能用 terminal 工作的 server 時…,在這些情況下,還是會 CLI 會比較好。
而到底是 CLI 還是 GUI 比較好,其實兩者各有優點與缺點,端看使用情境。關於兩者的比較,可以參見以下文章:
環境建置
以 Mac OS 為例。參考以下文章建置環境:
其中,安裝字體的部分,操作指令為:
# 第一次使用 homebrew 安裝字型
# 現在已經不使用 brew tap caskroom/fonts
brew tap homebrew/cask-fonts
# 安裝字體指令
brew cask install font-hack-nerd-font
先關閉 iTerm2,才能在 preference 中選擇該字體。
最後的結果
- terminal:iTerm2
- shell:zsh
- 管理套件:oh-my-zsh
- 主題:powerlevel9k
- 字體:hack-nerd-font
- 外掛:zsh-autosuggestions
.zshrc
設定:
ZSH_THEME="powerlevel9k/powerlevel9k"
POWERLEVEL9K_MODE='nerdfont-complete'
# command line 左邊想顯示的內容
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(dir dir_writable vcs vi_mode)
# command line 右邊想顯示的內容
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status background_jobs time)
plugins=(git zsh-autosuggestions)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- VScode 設定檔(setting.json):
"terminal.integrated.fontFamily": "hack nerd font"
常用指令
基本指令
pwd 印出所在位置
pwd
:Print Working Directory,印出從根目錄到現在的所在位置路徑
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
ls 列出檔案清單
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
註:藍色的為資料夾,白色的是檔案
變化型
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
ls -a
: 顯示隱藏的檔案與目錄
隱藏的檔案最前面會用 .
來表示
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
ls -al
: 顯示隱藏檔案與目錄的詳細資訊
ls -l
與 ls -a
的組合
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
關於 ls
的詳細使用方法,可以參考:
cd 前往不同地方
可以使用相對路徑或絕對路徑
cd
:Change Directory,切換資料夾
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
變化型
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
註:起始目錄與根目錄並不相同
man 指令說明手冊
man
:MANual,列出使用指令的詳細資訊,包括可以帶什麼參數
- 按
q
離開
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
檔案操作相關指令
touch 建立檔案或更改時間
touch
:若檔案已存在則會更改檔案的修改時間,若檔案不存在則是新增該檔案
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
rm 刪除檔案
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
變化型
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
mkdir 建立資料夾
mkdir
:MaKe DIRectory,建立新資料夾

mv 移動檔案或是改名
mv
:MoVe,mv 檔案 資料夾
為移動檔案;mv 檔案 新檔案
為更改檔案名稱


cp 複製檔案

變化型

cat 印出檔案內容
cat
:ConcATenate,將檔案內容輸出在終端機上

Vim 文字編輯器
vim
:編輯檔案文字內容
- 按
i
為 insert mode
- 按
esc
為 normal mode
- 按
:q
為離開編輯器
- 按
:wq
為儲存後離開編輯器
- 按
:qa!
為不儲存就離開編輯器

關於 Vim 如何操作,可以參考以下文章:
其他好用指令
grep 抓取關鍵字

wget 下載檔案

若電腦內沒有 wget 指令的話,請先使用 homebrew 套件安裝
關於 wget 的詳細使用方法,可參考:
curl 送出 request
curl
:送出 request,可以用來測試 API
取得 Response Header 以及網頁內容

關於 curl 的詳細使用方法,可參考:
指令的組合技
Output > Input
:將前面指令的 Output 作為後面檔案的 Input
將 ls -l
的資訊輸出到 result 這個檔案

變化型
Output >> Input
:將前面指令的 Output 新增(append) 到後面指令或檔案
若原本只用 >
會覆蓋所有內容,使用 >>
可以把內容新增到後面

Output | Input
:將前面指令的 Output 作為後面指令的 Input
印出 text 的內容後,再抓出 3 這個字

後記
一開始使用 CLI 會很不習慣,但打久了,那些常用的指令就能記起來,對於電腦的 response 也比較能看懂。尤其在幫 iTerm2 安裝管理套件與主題後,畫面美觀許多,也更好上手。用起來心情變得不一樣。可謂工欲善其事必先利其器吧(笑)。