CHAPTER 3 / 14

檔案操作與權限

cp、mv、rm、mkdir、touch、chmod、ln — 安全管理 raw data、reference 與 results。

cp, mv, rm, mkdir, touch, chmod, ln — safely manage raw data, references and results.

檔案管理基本指令

mkdir results
新建資料夾
mkdir -p results/fastqc/sampleA
一次建立多層資料夾
touch sample.txt
建立空檔案
cp src.fq backup/
複製檔案
cp -r reference/ ../bk/
遞迴複製資料夾
cp -p file results/
保留時間戳與權限
mv old.fq new.fq
改檔名或搬移
rm tmp.txt
刪除檔案
rm -r results_old/
遞迴刪除資料夾
rm -i *.bam
刪除前先確認
ln -s /data/genome.fa .
建立 symbolic link
find . -name "*.bam"
找特定副檔名檔案

關於 rm — 沒有回收桶這件事

☠️

Linux 的 rm真的刪除,不會進垃圾桶、無法 Ctrl+Z、無法復原。再加上 rm -rf 與 wildcard 的組合,是無數 PhD 一夜回到解放前的元兇。

rm on Linux is a real delete — no recycle bin, no Ctrl+Z, no undo. Combined with rm -rf + wildcards, it has destroyed countless PhD theses overnight.

非常危險

rm -rf /

絕對不要輸入此指令——這會嘗試刪除整個系統。

Never run this — it tries to wipe the whole system.

rm -rf $undefined_var/

如果變數沒定義,這會展開成 rm -rf /。寫 script 時務必 set -u

If the variable is undefined, this expands to rm -rf /. Always set -u in scripts.

安全做法

① raw_data/ 設為唯讀:chmod -R a-w raw_data/
② 養成先 lsrm 的習慣
③ 互動使用建議 alias rm='rm -i'
④ 重要時刻用 mv 改名為 .trash/ 而不是直接 rm
⑤ 寫 script 時:set -euo pipefail

① Mark raw_data/ read-only: chmod -R a-w raw_data/
② Habit: ls then rm
③ Interactively: alias rm='rm -i'
④ Critical files: mv to .trash/ instead of rm
⑤ In scripts: set -euo pipefail

Wildcards — 用 *、?、{} 一次處理多個檔

*

匹配 0 個或多個任意字元。*.fastq.gz 匹配所有 fastq.gz 檔。

Match 0 or more characters. *.fastq.gz matches every fastq.gz file.

?

匹配恰好 1 個字元。sample?.fastq 匹配 sampleA、sampleB...

Match exactly 1 character. sample?.fastq matches sampleA, sampleB…

[ABC]

字元集合。sample[AB]_R1.fastq 只匹配 sampleA、sampleB。

Character class. sample[AB]_R1.fastq matches only sampleA, sampleB.

{a,b,c}

大括號展開。sample{A,B,C}_R1.fq.gz = sampleA_R1.fq.gz sampleB_R1.fq.gz sampleC_R1.fq.gz。

Brace expansion. sample{A,B,C}_R1.fq.gz = three filenames in one.

{1..10}

數字範圍。sample{1..10}.bam 展開為 sample1.bam ~ sample10.bam。

Numeric range. sample{1..10}.bam expands to sample1.bam … sample10.bam.

**

啟用 globstar 後(shopt -s globstar),**/*.bam 匹配所有子資料夾。

With globstar (shopt -s globstar), **/*.bam matches across all subdirectories.

💡

實務小技巧:下危險指令前,先用 echo 預覽 wildcard 會展開成什麼。
echo rm raw_data/sample*.fastq.gz 顯示「實際會刪哪些」,沒問題再把 echo 拿掉。

Pro tip: Before any dangerous command, preview wildcard expansion with echo:
echo rm raw_data/sample*.fastq.gz prints what it would delete — drop the echo only when satisfied.

Linux 權限系統 — 讀懂 -rwxr-xr--

ls -l 看到的左邊那串 10 個字元,是檔案的權限。前 1 個是檔案類型(-=檔案、d=目錄、l=symlink),後面 9 個分為三組:

The 10-character string on the left of ls -l encodes permissions. The first character is the type (-=file, d=directory, l=symlink); the remaining 9 split into three groups:

位置角色字元意義
2-4擁有者 (user)rwx可讀、可寫、可執行
5-7群組 (group)r-x可讀、可執行,但不可寫
8-10其他人r--只可讀

chmod 兩種寫法

符號式(直觀)

chmod u+x run.sh

給 user 加上執行權限(最常見的「讓 script 可執行」)。u/g/o/a = user/group/other/all;+/-/= = 加/減/設定。

Give the user execute permission (the canonical "make a script runnable"). u/g/o/a = user/group/other/all; +/-/= = add/remove/set.

chmod -R a-w raw_data/

遞迴讓所有人都不能寫入 raw_data — 保護原始資料的標準做法。

Recursively remove write for everyone in raw_data — the standard way to protect raw data.

數字式(精確)

chmod 755 run.sh

r=4, w=2, x=1 相加。
755 = user 7 (rwx), group 5 (r-x), other 5 (r-x)。
常見:
755 程式 / 公開資料夾
644 一般檔案
700 私人資料夾

r=4, w=2, x=1, summed.
755 = user 7 (rwx), group 5 (r-x), other 5 (r-x).
Common modes:
755 programs / public dirs
644 regular files
700 private dirs

Symbolic Links — 巨大檔案的最佳朋友

NGS reference genome 動輒 3 GB、index 還要 10 GB;多個專案如果都複製一份,硬碟很快爆掉。Symbolic link 像 Windows 的「捷徑」,只佔幾 bytes 就能讓所有專案共用一份檔案。

ln -s /data/reference/hg38/genome.fa  reference/genome.fa

現在 reference/genome.fa 看起來像本地檔案,但實際指向 /data/...。在 NGS 流程中是非常常用的技巧。

An NGS reference genome is easily 3 GB, with another 10 GB of index. Copying a fresh one for every project drains disk fast. Symbolic links are like Windows shortcuts: a few bytes that let many projects share one master file.

ln -s /data/reference/hg38/genome.fa  reference/genome.fa

reference/genome.fa now behaves like a local file but really points at /data/.... This is a very common NGS pattern.

⚠️

ls -l 會看到 reference/genome.fa -> /data/...。如果原始檔被刪除或移動,連結就失效(dangling link)。複製整個專案時要確認 cp -L(跟著連結走)或保留 symlink。

ls -l shows reference/genome.fa -> /data/.... If the target is moved or deleted the link dangles. When duplicating projects, decide between cp -L (follow the link) or preserving the symlink.

任務:建立 NGS 專案骨架

在下方 terminal 中,依序執行:

  1. mkdir -p project/{raw_data,metadata,reference,scripts,results,logs}
  2. cd project
  3. touch README.md
  4. ls -lh
  5. chmod -R a-w raw_data

In the simulator below, run in order:

  1. mkdir -p project/{raw_data,metadata,reference,scripts,results,logs}
  2. cd project
  3. touch README.md
  4. ls -lh
  5. chmod -R a-w raw_data

📝 自我檢測

1. 想一次建立 results/fastqc/sampleA 三層資料夾,最簡單的方法?

1. Easiest way to create the 3-level results/fastqc/sampleA at once?

mkdir results/fastqc/sampleA
mkdir -p results/fastqc/sampleA
mkdir -r results/fastqc/sampleA
cp -p results/fastqc/sampleA

2. 關於 rm,下列何者正確?

2. Which is true about rm?

A. 刪除後會放到回收桶可以還原A. Deletes go to a recycle bin you can restore
B. rm -r 會把整個資料夾保留,只刪檔案B. rm -r keeps the directory and deletes only files
C. 真的刪除,無法復原;建議先 ls 預覽 wildcard 結果C. Truly deletes; preview wildcard expansion with ls first
D. rm 預設不能刪除任何 fastq 檔D. rm by default refuses fastq files

3. chmod 755 run.sh 執行後,ls -l run.sh 看到的權限是?

3. After chmod 755 run.sh, ls -l run.sh shows?

-rw-rw-rw-
-rwxrwxrwx
-rwxrw-rw-
-rwxr-xr-x

4. 為什麼 NGS 專案常用 symbolic link 引用 reference genome?

4. Why do NGS projects often use symlinks for the reference genome?

A. 不複製檔案、節省 GB 級空間,多個專案共用A. No copy — saves gigabytes; many projects share one file
B. symlink 比真實檔案讀取快很多B. Symlinks read faster than real files
C. symlink 自動加密檔案C. Symlinks encrypt the target
D. samtools 只能讀 symlinkD. samtools only reads through symlinks