這份參考整理 Linux environment 中常用的 commands。 每個 command 可以獨立查閱;實際使用時,請依自己的 path、filename 與 environment 調整參數。

Overview

指令 主要用途 常用參數/選項 使用情境
定位與移動
pwd 顯示目前的 working directory — 確認目前所在的 project、home 或工作目錄
ls 列出 directory 中的 files 與 directories — 確認 input、script 或 output 是否存在
cd 切換 working directory `後面接檔案/目錄位置
~` home
.. 上一層
- 前一個位置 在不同工作目錄之間移動
檔案與目錄操作
mkdir 建立 directory -p 同時建立需要的上層 directories 建立 project、data 或 results directory
touch 建立空 file 或更新 timestamp — 快速建立新的 file
cp 複製 file 或 directory -r recursively 複製 directory 備份 input、script 或 results
mv 移動或重新命名 file / directory — 整理 files 或修改 filename
rm 刪除 file 或 directory -r recursively 刪除 directory 清除不再需要的 files 或 results
檢視與編輯內容
cat 直接顯示 text file 內容 — 快速查看短 output、config 或 script
less 分頁查看較長的 text file /word 搜尋
q 離開 閱讀較長的 log 或 output
nano 在 Terminal 中編輯 text file Ctrl+O 儲存
Ctrl+X 離開 修改 script、設定檔或簡單文字檔
權限
id 查看目前 user identity 與 groups — 確認目前的 user 與 group membership
ls -l 查看 permissions、owner、group 等 file information — 檢查 file 為什麼無法讀取、修改或執行
chmod 修改 file 或 directory permissions u+x owner 加 execute
g+w group 加 write 讓 script 可以執行或調整 shared file permissions
sudo 以另一個 identity 執行這一次 command 通常直接接要執行的 command 執行被授權的 system-level operation
程式執行與 Shell Script
./script.sh 執行目前 directory 中的 script 執行前通常搭配 chmod u+x 執行自己準備的 Shell Script
#!/bin/bash 指定 Shell Script 使用 Bash 執行 — 在 script 開頭指定 interpreter
套件管理(APT)
apt update 更新可取得的 package information 通常搭配 sudo 安裝 package 前更新 package index
apt install 安裝指定 package apt install <package> 在有管理權限的 machine 上安裝 software
apt upgrade 升級目前可更新的 installed packages 通常搭配 sudo 維護自己負責管理的 Linux system
程序監控
ps 查看目前的 processes — 確認 program 是否仍然正在執行
top 即時查看 processes 與 resource usage q 離開 觀察 CPU、memory 與 running processes
htop 以互動式介面查看 processes 與 resources F3 Search
F6 Sort
F9 Kill
F10 Quit 快速檢查 process 與 system resource usage
kill 向指定 Process 傳送 signal `kill <PID>
-9` 強制終止 結束不再需要或無法正常停止的 Process
終端機管理
tmux new -s <name> 建立新的 tmux session -s 指定 session name 建立不依賴目前 connection 的 Terminal session
tmux ls 列出現有 tmux sessions — 確認先前建立的 session 是否仍然存在
tmux attach -t <name> 重新進入既有 tmux session -t 指定 session connection 中斷後回到原本的工作環境

Where am I?

pwd

pwd 用來查看目前所在的 working directory。

例如:

pwd

可能看到:

/home/alice

這表示目前位在 /home/alice。

ls

ls 用來查看一個 directory 裡有哪些 files 與 directories。

例如:

ls

可能看到:

data
notes.txt
run.sh