CHAPTER 2 / 14

Terminal、Shell 與檔案系統

理解 terminal 是什麼、shell 在做什麼、Linux 檔案系統如何組織,以及如何用 pwd / cd / ls 在 NGS 專案中走動。

Understand what a terminal is, what the shell does, how the Linux filesystem is organised, and how to walk an NGS project tree with pwd / cd / ls.

Terminal、Shell、Bash — 它們各自是什麼?

🖥️

Terminal

那個「黑色視窗」程式(Terminal.app、iTerm、GNOME Terminal、Windows Terminal、PuTTY),負責顯示文字與接收鍵盤輸入。它是 外殼,不是真正在執行指令的人。

The "black window" program (Terminal.app, iTerm, GNOME Terminal, Windows Terminal, PuTTY). It just displays text and receives keystrokes — it's the shell of the shell, not the executor.

🐚

Shell

真正讀懂並執行指令的程式:把你輸入的 ls -lh 解析成「呼叫 /usr/bin/ls,帶 -lh 參數」。常見有 bashzshfish

The actual program that parses and runs your commands: it turns ls -lh into "call /usr/bin/ls with -lh". Common shells: bash, zsh, fish.

💬

Bash

最普及的 shell,幾乎所有 Linux 教程預設都用 bash。本課程的所有腳本都用 bash 撰寫。

The most widespread shell — every Linux tutorial defaults to bash. All scripts in this course are written in bash.

💡

想知道你現在用哪個 shell?輸入 echo $SHELLps -p $$。如果輸出是 /bin/bash/usr/bin/bash,你就在 bash 裡。

Wondering which shell you're in? Run echo $SHELL or ps -p $$. If you see /bin/bash or /usr/bin/bash, you're in bash.

Linux 檔案系統的全貌

Linux 的所有東西都掛在一棵「單一根目錄樹」上,最頂端是 /(不是 C: D: 那種磁碟代號)。常見路徑如下:

Everything in Linux hangs from a single root tree starting at / (no C:, D: drive letters). Common paths:

/                       # 根目錄
├── home/                # 一般使用者家目錄都在這
│   └── charlene/        # 你的家目錄 = ~ = $HOME
│       └── project/     # 工作中的 NGS 專案
├── usr/bin/             # 安裝的指令會在這 (ls, samtools …)
├── etc/                 # 系統設定檔
├── tmp/                 # 臨時檔,重開機會清空
├── opt/ data/            # 自行掛載的大型儲存常放這
└── mnt/                 # 暫掛 USB / 網路硬碟
⚠️

在 HPC 或實驗室伺服器上,重要 NGS 資料常放在 /data/scratch/work/home/share 等不同位置。配額(quota)、I/O 速度、備份政策都不一樣,記得先問你的系統管理員。

On HPC or lab servers, NGS data may live in /data, /scratch, /work, /home, /share — each with different quotas, I/O speed and backup policies. Ask your sysadmin first.

絕對路徑 vs 相對路徑

路徑類型特徵範例
絕對路徑 一定以 / 開頭 /home/charlene/project/raw_data/sampleA_R1.fastq.gz
相對路徑 不以 / 開頭 raw_data/sampleA_R1.fastq.gz
~ 自己的 home 目錄 ~/project/raw_data
.目前資料夾./run_fastqc.sh
..上一層資料夾cd ../reference
💡

實務原則:寫 script、log、README 時建議用絕對路徑,避免「在不同資料夾下執行就會壞掉」;互動操作時則用相對路徑比較快。

Rule of thumb: use absolute paths in scripts, logs and README files — they survive being run from any directory. Use relative paths for fast interactive work.

導航三劍客:pwd / cd / ls

pwd
顯示目前所在資料夾的絕對路徑
cd /path/to/dir
切換到指定資料夾
cd ~
回到家目錄($HOME)
cd -
回到上一個切換前的資料夾
cd ..
上一層
ls
列出目前資料夾的內容
ls -l
長格式
ls -lh
長格式 + 人類可讀大小
ls -a
顯示隱藏檔
ls -ltr
按時間排序,最新在最後
ls raw_data/*.fastq.gz
只列出 fastq.gz 檔
tree -L 2
樹狀顯示資料夾

一個 Linux 必學的鍵:Tab

Tab 自動補全是 Linux 上最重要的省時技巧。輸入指令或檔名前幾個字,按一下 Tab,shell 會幫你補完到唯一可能的選項;按兩下會列出所有可能的選項。

  • 輸入 cd raw_d → 按 Tab → 自動變成 cd raw_data/
  • 輸入 ls samp → 按 Tab Tab → 列出 sampleA、sampleB...
  • 檔名超長(如 SRR12345678_1.fastq.gz),Tab 補全可避免打錯字

Tab completion is the single most important time-saver in Linux. Type a few characters and press Tab — the shell completes the unique match; press it twice to list candidates.

  • Type cd raw_dTab → becomes cd raw_data/
  • Type ls sampTab Tab → lists sampleA, sampleB…
  • For long filenames like SRR12345678_1.fastq.gz, Tab eliminates typos.

↑ ↓ 歷史紀錄

按 ↑/↓ 翻閱之前輸入過的指令;按 Ctrl+R 反向搜尋(輸入幾個字就能找到一週前打過的命令)。

Press ↑/↓ to scroll through earlier commands; Ctrl+R reverse-search lets you find a command from last week with a few keystrokes.

Ctrl + C / D / L / U

Ctrl+C=中斷指令,Ctrl+D=結束輸入/登出,Ctrl+L=清螢幕(同 clear),Ctrl+U=清掉整行輸入。

Ctrl+C=interrupt, Ctrl+D=EOF/logout, Ctrl+L=clear screen (= clear), Ctrl+U=clear current input line.

動手練習:在 NGS 專案中走動

下方終端機已預先載入一個 NGS 專案。試著完成以下任務:

  1. 輸入 pwd 看看目前在哪
  2. 輸入 ls 看看現在資料夾有什麼
  3. 輸入 cd raw_data 進入 raw_data
  4. ls -lh 看詳細列表
  5. cd .. 回上層;cd ~ 回家目錄

The simulator below has a pre-loaded NGS project. Try:

  1. pwd to see where you are
  2. ls to see what's around
  3. cd raw_data to enter raw_data
  4. ls -lh for the detailed listing
  5. cd .. to go up; cd ~ to go home

📝 自我檢測

1. 下列哪一個是絕對路徑?

1. Which is an absolute path?

raw_data/sampleA.fastq.gz
../reference/genome.fa
/home/charlene/project/raw_data/sampleA.fastq.gz
~/project/raw_data

2. 想從 ~/project/raw_data 跳到 ~/project,應該下哪個指令?

2. From ~/project/raw_data, which command moves you to ~/project?

cd /
cd ..
cd ~
cd ../..

3. 想看 raw_data 裡每個 fastq.gz 的檔案大小(顯示為 KB/MB/GB),最直接的指令是?

3. To see each fastq.gz size in raw_data (in KB/MB/GB), the most direct command is?

ls raw_data
ls -a raw_data
du raw_data
ls -lh raw_data

4. 為什麼老手都狂按 Tab?

4. Why do experienced users hammer Tab?

A. 自動補全檔名與指令,避免打錯字A. Autocompletes commands/filenames, prevents typos
B. 取消正在執行的指令B. Cancels a running command
C. 翻閱歷史紀錄C. Scrolls history
D. 縮排程式碼D. Indents code