你的第一個 Linux 指令

Linux 命令列的思維方式

在開始輸入指令前,先建立正確的心態。Linux 命令列建立在幾個核心哲學上:

  1. 每個程式只做一件事,但做好它ls 只負責列出目錄,grep 只負責搜尋文字
  2. 程式可以串聯使用:用管線(|)把多個小工具組合成強大的功能
  3. 文字是通用介面:幾乎所有程式的輸入輸出都是純文字,方便處理
  4. 沉默是金:指令成功時通常沒有輸出,只有失敗才顯示錯誤訊息
💡 剛從圖形介面(GUI)切換過來的人,最不習慣的就是「指令成功時什麼都不顯示」。在 Linux 命令列,沒有輸出 = 成功,這是正常行為。

解剖一個 Linux 指令

每個 Linux 指令的格式通常是:

指令名稱  [選項]  [引數]
   ls      -la    /etc/
部分說明例子
指令名稱 要執行的程式名稱,必填 lscdgrep
選項(Options) --- 開頭,修改指令的行為,可省略 -l(單字母)、--all(完整單字)
引數(Arguments) 指令操作的目標(檔案、目錄、文字等),可省略 /etc/file.txthello

多個單字母選項通常可以合併:ls -l -a 等同於 ls -la

認識路徑:絕對路徑 vs 相對路徑

在 Linux 中,所有東西都是檔案,而所有檔案都在一個以 /(根目錄)為起點的目錄樹中。理解路徑是操作 Linux 的基礎。

/                          ← 根目錄(所有東西從這裡開始)
├── etc/                   ← 系統設定檔
│   ├── nginx/
│   │   └── nginx.conf     ← 絕對路徑:/etc/nginx/nginx.conf
│   └── hosts
├── home/                  ← 使用者的家目錄
│   └── alice/             ← alice 的家目錄(也寫作 ~ )
│       ├── Documents/
│       └── Downloads/
├── var/                   ← 變動資料(日誌、資料庫等)
└── usr/                   ← 使用者安裝的程式

絕對路徑(Absolute Path)

/ 開頭,從根目錄開始描述完整路徑,不論你目前在哪個目錄,路徑永遠正確。

$ ls /etc/nginx/

相對路徑(Relative Path)

以目前所在目錄為起點,不加 / 開頭。有兩個特殊符號:

alice@server:~$ ls Documents/

如果你現在在 /home/alice/,上面的指令等同於 ls /home/alice/Documents/

這三個指令是 Linux 最基本的「導航三劍客」,每天都會用到上百次。

pwd — 我在哪裡?

隨時不知道自己在哪個目錄,就輸入 pwd

$ pwd
/home/alice

ls — 這個目錄裡有什麼?

$ ls
Desktop  Documents  Downloads  Music  Pictures  Videos
$ ls -la
total 56
drwxr-xr-x 9 alice alice 4096 May 20 10:00 .
drwxr-xr-x 3 root  root  4096 Mar  5 08:00 ..
-rw-r--r-- 1 alice alice  220 Mar  5 08:00 .bash_logout
-rw-r--r-- 1 alice alice 3526 Mar  5 08:00 .bashrc
drwxr-xr-x 2 alice alice 4096 May 20 09:15 Desktop
drwxr-xr-x 5 alice alice 4096 May 18 14:30 Documents

-l 是詳細格式,-a 是顯示隱藏檔(以 . 開頭)。

cd — 移動到另一個目錄

$ cd Documents   # 進入 Documents(相對路徑)
$ cd /etc/nginx   # 直接跳到絕對路徑
$ cd ..           # 回到上一層
$ cd              # 不加引數:回到家目錄
$ cd -            # 切換到上一個工作目錄(類似瀏覽器上一頁)

基本檔案操作

指令功能例子
mkdir建立目錄mkdir projects
mkdir -p建立多層目錄mkdir -p a/b/c
touch建立空白檔案touch hello.txt
cp複製檔案cp file1 file2
mv移動或重新命名mv old.txt new.txt
rm刪除檔案(不可復原!)rm file.txt
rm -r刪除目錄(遞迴)rm -r mydir/

實際操作練習

$ mkdir ~/linux-practice
$ cd ~/linux-practice
$ touch hello.txt
$ ls
hello.txt

查看檔案內容

先用 echo 寫一些內容進去,再用不同的方式查看:

$ echo "Hello, Linux!" > hello.txt
$ cat hello.txt
Hello, Linux!
指令適合場景
cat 檔案顯示短小的檔案(全部輸出)
less 檔案查看大型檔案(可上下捲動,q 離開)
head 檔案只看前 10 行(加 -n 20 看前 20 行)
tail 檔案只看後 10 行;tail -f 即時追蹤
grep 關鍵字 檔案搜尋含有關鍵字的行

如何求助:man 與 --help

不知道指令怎麼用?Linux 有完整的內建說明系統:

man — 詳細手冊(最完整)

$ man ls

進入 man 後,用 Space 翻頁,/ 搜尋,q 離開。

--help — 快速提示(常用)

$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs...

  -a, --all      do not ignore entries starting with .
  -l             use a long listing format
  -h, --human-readable  with -l, print sizes in human readable format
  ...

type — 確認指令的類型

$ type ls
ls is /usr/bin/ls
$ type cd
cd is a shell builtin

上手練習

🎯 試著自己完成以下任務,不要只是讀過去。動手操作才能真正學會。
  1. 在你的家目錄建立一個名為 practice 的目錄
  2. 進入這個目錄,確認 pwd 顯示正確的路徑
  3. 建立三個空白檔案:a.txtb.txtc.txt
  4. ls -l 查看這三個檔案
  5. a.txt 複製為 a_backup.txt
  6. b.txt 重新命名為 notes.txt
  7. 刪除 c.txt
  8. 回到家目錄,確認 practice/ 目錄存在
  9. ls practice/(相對路徑)確認目錄內的檔案