--- tags: 109-1 --- # Linux Command Line Tutorial ## 一些有用的按鍵 | 按鍵 | 功能 | | ----------------- | ----------------------------------- | | Tab | 自動補齊指令/檔案名稱 | | Arrow Up | 叫出history裡的上一個指令 | | Shift + pgup/pgdn | 向上/向下捲動terminal | | Ctrl + C | 中斷程式執行 | | Ctrl + D | Exit | | Ctrl + Z | 把程式送進背景並暫停執行 | | Ctrl + U | 從游標處向前刪除指令 | | Ctrl + A | 讓游標移動到指令的最前面 | | Ctrl + E | 讓游標移動到指令的最後面 | | Ctrl + S | lock console | | Ctrl + Q | unlock console | | Ctrl + L | clear console (效果等同於clear指令) | | Ctrl + R | 搜尋指令history | ## 基礎指令 | 指令 | 功能 | | ------- | ------------------------------------------- | | pwd | **p**rint current **w**orking **d**irectory | | cd | **c**hange **d**irectory | | ls | **l**i**s**t files | | cp | **c**o**p**y files | | mv | **m**o**v**e/rename files | | mkdir | **m**a**k**e a new **dir**ectory | | rmdir | **r**e**m**ove an empty **dir**ectory | | rm | **r**e**m**ove files or directories | | touch | create a new empty file | | ln | create **l**i**n**ks | | cat | con**cat**enate files to standard output | | head | output the first part of files | | tail | output the last part of files | | diff | output the **diff**erences of two files | | clear | clear console | | echo | echo text | | sudo | execute commands as root user | | history | show command history | | man | read **man**uals | | tar | create/extract tarball | | find | search for filename | | ssh | remote login to workstation | ## 進階觀念 ### IO Redirection IO redirection可以使輸入/輸出不從standard input(鍵盤輸入)和standard output(輸出到螢幕上)來,而是導到特定檔案。 #### Output redirection (>) 試試以下指令吧! 沒有redirection時,輸出會跑到console上: ```sh echo hi ``` Redirect output to `redirection_test.txt`: ```sh echo hi > redirection_test.txt ``` 此時console不會有output,讓我們把 `redirection_test.txt` 的內容print出來看看: ```sh cat redirection_test.txt ``` #### Input redirection (<) `cat` 指令單獨使用時 (不加任何參數),會把鍵盤輸入的東西 (standard input) 輸出到螢幕上 (standard output)。 ```sh cat ``` 試著打幾個字再按enter看看,測試完畢後按 Ctrl+D 離開。 現在把剛才的 `redirection_test.txt` 的內容重新導向到 `cat` 指令看看。 ```sh cat < redirection_test.txt ``` :::info :bulb: 下面這兩個指令有什麼不同呢? ```sh cat redirection_test.txt ``` ```sh cat < redirection_test.txt ``` A:雖然結果一樣,但概念不一樣,第一個是把 `redirection_test.txt` 當成參數,所以 `cat` 指令接收到1個參數,而 `cat` 指令接收到一個參數的動作就是把檔案裡面的東西輸出到standard output。而第二個則是 `cat` 指令接收到0個參數,`cat` 指令接收到0個參數的動作就是把鍵盤輸入 (standard input) 的東西輸出到standard output,而我們用"<"把鍵盤輸入 (standard input) 用 `redirection_test.txt` 的內容重導向,所以現在鍵盤輸入變成檔案裡面的內容, `cat` 指令就把它輸出到螢幕。 ::: ### Pipe (|) Pipe可以把前一個指令的輸出當作是下一個指令的輸入傳下去。 ---- ## #### more / less command 這兩個command都可以讓輸出不會直接一口氣跑到standard output,而可以翻頁或捲動檢視,搭配pipe使用。 試試下面兩種指令吧! ```sh ls | more ``` ```sh ls | less ``` 在 `more` 模式下,用 Enter 鍵可以往下,按 q 可以離開。 在 `less` 模式下,用方向鍵或j/k可以上下,按 q 可以離開。 #### grep 這個指令是"Get by REgular exPression"的縮寫,搭配pipe使用可以用Regular expression(正則表示法)過濾出特定的字。 我們不教Regular expression,有興趣的可以自己去查,在沒有夾帶符號的時候regular expression和一般字母是一樣的,試試看下面的指令: ```sh ls | grep test ``` 最後,把那個練習的檔案刪掉吧! ``` rm redirection_test.txt ``` ## Want To Learn More? - [鳥哥的Linux私房菜](http://linux.vbird.org/)