# Ubuntu/Linux 常用指令入門 By 蓉爸 RungBa Created: 2019-09-23 Revised: 2024-12-31 --- ![](https://i.imgur.com/MvWpVu5.png) (圖片來源:https://blog.techbridge.cc/2017/12/23/linux-commnd-line-tutorial/) --- **Ref:** - **[100+ Essential Linux Commands for Every Linux User (2023-02-23)](https://www.tecmint.com/essential-linux-commands/)** - **[Ubuntu 常用指令 (2011-05-12)](https://cometlc.pixnet.net/blog/post/2547363-ubuntu-%E5%B8%B8%E7%94%A8%E6%8C%87%E4%BB%A4)** - **[GNU/Linux 常用指令](https://wiki.ubuntu-tw.org/index.php?title=GNU/Linux_%E5%B8%B8%E7%94%A8%E6%8C%87%E4%BB%A4)** - **[簡易terminal指令](https://gist.github.com/BeMg/2cec68a98b088dc74daf)** - **[tar 指令的常用語法](http://www.vixual.net/blog/archives/127)** - **[簡單學 makefile:makefile 介紹與範例程式](https://mropengate.blogspot.com/2018/01/makefile.html)** - 拯救資工系學生的基本素養—Linux 平台程式開發環境(上) {%youtube _DXgCXlZvwQ %} - 拯救資工系學生的基本素養-Linux 平台程式開發環境(下) {%youtube djkpt0TBnWI %} --- :::danger > 1. 通常也適用於 Debian, Raspbian 等 Linux-Like OS > 2. 好用的 **tab 鍵** > 3. ctrl+c, ctrl+z > 4. man [指令] > 5. 一般指令格式如下: > ```bash= > $ sudo 指令 [選項] [選項值] > $ 指令 [選項] [選項值] > ``` > 6. Linux 的指令、檔名、選項(或參數)**有區分「大小寫」**,千萬要注意! ::: --- ## 1. 網際網路相關操作 ```bash= # ping:網路檢測工具,透過發送 ICMP ECHO_REQUEST 的封包,檢查自己與特定設備之間的網路是否暢通,速度是否正常 $ ping 192.168.1.1 # 可輸入 hostname 或是 IP $ ping -c 4 192.168.1.1 # $ ping -c 4 google.com # # traceroutes:檢查從你的電腦到網路另一端的主機是走的什麼路徑 $ traceroute google.com # nslookup:查詢 DNS 回應是否正常 $ nslookup google.com # 查詢網卡與 IP $ ifconfig # Ubuntu 18.04 以上版本用以下指令 $ ip -c a ## 查詢路徑 ip route ``` ![](https://i.imgur.com/G4mqkQW.png) --- ## 2. 開關機 ```bash= $ sudo reboot # 重開機 $ sudo poweroff # 關機 ``` --- ## 3. 系統管理 ```bash= $ su root # 切換成 "root" 這個使用者 $ sudo vi [filename] # 用 "root" 權限使用 vi 編輯器 $ top # 顯示系統CPU, RAM, Swap, 執行中程序等資訊 $ uname -a # 顯示 Linux kernel 版本資訊 $ ps -ef | grep [程式名稱] # 搜尋執行中的程序 $ kill -9 [pid] # 強制關閉執行中的程序 ``` ### htop - **top 的加強版,外觀、功能也更強** ```bash= 行中程序等資訊 $ htop # 顯示系統CPU, RAM, Swap, 執行中程序等資訊 ``` ![](https://i.imgur.com/Xe5t1ZG.png) --- ## 4. 使用者管理 ```bash= # $ passwd [user] # 變更 user 的密碼 $ passwd -d [user] # 讓 user 無密碼也能登入 $ passwd -S [user] # 查詢 user 的密碼 $ useradd # 建立一個新的使用者 $ groupadd # 建立一個新的群組 $ usermod -l [new name] [old name] # 更改使用者名稱 $ userdel -r [user] # 刪除使用者及其所擁有的 home dir ``` --- ## 5. 硬碟管理 ```bash= $ df -h # 顯示硬碟掛載狀況 $ fdisk -l # 顯示硬碟 partition 等資訊 $ fdisk /dev/sdb # 可對 /dev/sdb 這個硬碟進行分割 ``` --- ## 6. Linux 檔案系統基本概念 * [Ubuntu/Linux 檔案系統(File System)基本觀念](https://hackmd.io/FyqGYvSuSGqPsKEjNq1PgA) ![](https://i.imgur.com/hikpDuH.png) (圖片來源:https://blog.techbridge.cc/2017/12/23/linux-commnd-line-tutorial/) ![](https://i.imgur.com/LSNW30X.png) (圖片來源:https://blog.techbridge.cc/2017/12/23/linux-commnd-line-tutorial/) Ref: * Chmod Command in Linux (File Permissions) (2019-09-16) https://linuxize.com/post/chmod-command-in-linux/ --- ## 7. 檔案與資料夾管理 ### 7-1. 檔案一般指令 ```bash= # list files $ pwd # 顯示目前路徑 $ ls $ ls -a # 顯示隱藏檔,也就是「.」開頭的檔案 $ ls -al $ cat # 顯示文字檔案內容 $ more # 用分頁方式顯示文字檔案內容 $ less # 與 more 類似 $ grep [pattern] # 過濾出 [pattern類型] 字串 ``` ---- ### 7-2. 檔案拷貝、移除、移動 ```bash= $ cp file1 file2 # copy 檔案 $ rm [file or files] # 刪除一個或多個文件 $ mv file1 file2 # 更改檔名 $ mv file1 ../file2 # 移動檔案,並更名 $ mv file1 path-to-dir ``` ---- ### 7-3. 資料夾指令 * **資料夾建立與移除** ```bash= # 建資料夾 $ mkdir # 刪資料夾 $ rmdir # 移除非空目錄內的一切 $ rm -rf [not empty dir] ``` * **資料夾切換** ```bash= # 切換資料夾 $ cd [path] $ cd .. $ cd - # 回上次所在資料夾 $ cd ~ # 回到 login user home dir $ cd # 同 cd ~ ``` ---- ### 7-4. 檔案、資料夾權限指令 ```bash= $ chmod 755 [file] # 變更 file 的開放權限為 755 $ chown [user] [file] # 變更 file 的擁有者 $ chgrp [group] [file] # 變更 file 的擁有群組 # 遞迴更改群組與擁有者 $ chown -R [grp]:[user] ./t_dir ``` ---- ### 7-5. 檔案壓縮、解壓縮指令 #### 7-5-1. tar (todo) #### 7-5-2. zip ```shell= # 將 data 目錄下所有檔案壓儲到 file.zip zip file data/* # 將 data 目錄下所有檔案及副目錄壓儲到 file.zip zip -r file data/* ``` #### 7-5-3. unzip ```shell= # 列出壓縮檔所有內容 unzip -l file.zip # 只想解壓壓縮檔內其中一個檔案,例如 test.pdf unzip file.zip test.pdf # 單純解壓縮 unzip file.zip # 解壓縮到指定資料夾 unzip file.zip -d [DIR] ``` Ref: * Linux 壓縮及解壓 ZIP 檔 (2015-07-21) https://www.opencli.com/linux/linux-zip-unzip ---- ### 7-6. 檔案搜尋 **locate 指令** ```shell= # 安裝 locate 套件 sudo apt-get install locate # 搜尋檔案名稱有 sunny locate sunny # 搜尋檔案名 sunny 有幾個 locate -c sunny ``` **find 指令** ```shell= sudo find . -name "*poster*" sudo find . -iname "*poster*" find ~/ -size -5M find / -size -5M -and -size +2M find / -amin -3 find / -time -2 ``` Ref: * How to search files from the Terminal on Linux https://www.howtoforge.com/tutorial/linux-search-files-from-the-terminal/ --- ## 8. 套件管理 * apt: Ubuntu 18.04 之後 apt-get 已經簡化為 apt ### 8-1. 更新套件庫 ```bash= # 若有新版套件,「會詢問」是否要更新 $ sudo apt-get update # 更新套件資料庫狀態 $ sudo apt-get upgrade # 更新已安裝的套件 $ sudo apt-get dist-upgrade # 系統升級更新 # 若有新版套件,「不詢問」,直接更新 $ sudo apt-get update -y $ sudo apt-get upgrade -y $ sudo apt-get dist-upgrade -y ``` ### 8-2. 安裝套件 ```bash= # 安裝套件 $ sudo apt-get install [-y] [package] $ sudo apt-get install [package] --reinstall # 重新安裝 $ sudo apt-get install -f [package] # 修復安裝 -f = --fix-missing ``` ### 8-3. 移除套件 ```bash= # 移除套件 $ sudo apt-get remove package $ sudo apt-get remove package --purge # 移除套件,包括相關設定檔與文件 $ sudo apt-get autoremove # 移除完全不再被依賴的套件 $ sudo apt-get autoclean # 清除在 cache 中的套件 $ sudo apt-get check # 檢查是否有損壞的依賴 ``` ### 8-4. 套件的搜尋與顯示 ```bash= # 搜尋與顯示 $ apt-cache search [package] # 搜索套件包 $ apt-cache show [package] # 獲取包的相關資訊,如說明、大小、版本等 ``` ### 8-5. 瞭解套件相依狀況 ```bash= $ apt-cache depends [package] # 查本套件依賴哪些套件 $ apt-cache rdepends [package] # 查本套件被哪些套件所依賴 ``` --- ## 9. Console 切換 Linux/Ubuntu 皆提供多個 console 可供切換,指令如下: ``` Ubuntu 主要有兩個模式: 文字模式(或稱 console ) 與 X-視窗模式 (桌面是 X-視窗模式 的一個子集合) Ctrl+Alt+F1~F6 是讓你進入 tty1~tty6 文字模式終端 共有六個 ctrl+Alt+F7~ F12 是讓你進入 tty7~tty12 X-視窗模式終端 也有六個 我們比較熟悉常用的文字模式終端是 tty1 我們比較熟悉常用的X-視窗模式終端是 tty7 他們是獨立存在的,你進了一個,其它的 11 個終端還在你的電腦裏。 唯一不同的是 你要進 X-視窗模式終端 該終端一定要安裝有 x-windows ``` Ref: https://www.ubuntu-tw.org/modules/newbb/viewtopic.php?post_id=227790#forumpost227790 --- ## 10. Shell Script 簡介 * Shell script 入門 https://hackmd.io/cBB8GdOYSqeZ71BqkMxjjg --- ## 11. make 指令 (略)