Linux 是伺服器領域的霸主,也是現代 DevOps 工程師的必備技能。本文整理 150 個最常用的 Linux 指令,從基礎操作到進階技巧,幫助你快速上手 CLI。
1. 文件與目錄操作 (File & Directory)
這些指令用於日常的文件與目錄管理:
| 指令 | 說明 | 常用用法 | |
|---|---|---|---|
ls |
列出目錄內容 | ls -la、ls -lh、ls --color |
|
cd |
切換工作目錄 | cd /var/log、cd ~、cd .. |
|
pwd |
顯示當前目錄絕對路徑 | pwd -P (解析符號連結) |
|
cp |
複製檔案或目錄 | cp a b、cp -r dir1 dir2、cp -a src/ dest/ |
|
mv |
移動/重新命名檔案 | mv old new、mv file ~/dir/ |
|
rm |
刪除檔案或目錄 | rm file、rm -rf dir、rm -i file |
|
touch |
建立空檔案或更新時間戳 | touch new.txt、touch -t 202401011200 file |
|
mkdir |
建立目錄 | mkdir project、mkdir -p a/b/c |
|
rmdir |
刪除空目錄 | rmdir empty_dir |
|
find |
搜尋檔案 | find . -name "*.txt"、find / -mtime -1 |
|
ln |
建立連結 | ln -s target link (軟連結)、ln target link (硬連結) |
|
tree |
以樹狀圖顯示目錄結構 | tree -L 2、tree -a |
2. 文件內容查看 (Viewing Content)
| 指令 | 說明 | 常用用法 |
|---|---|---|
cat |
顯示/串接檔案內容 | cat file、cat -n file、cat a b > c |
head |
顯示檔案開頭 | head -n 10、head -f access.log |
tail |
顯示檔案結尾 | tail -n 10、tail -f access.log |
less |
分頁式檢視器 | less file、less -N (顯示行號) |
more |
簡易分頁檢視器 | more file |
nl |
顯示行號 | nl file、nl -ba file |
wc |
計算行數/字數/位元組 | wc -l file、wc -w file、wc -c file |
od |
以二進位格式顯示檔案 | od -c file、od -x file |
xxd |
十六進位檔案查看器 | xxd file、xxd -r (反向轉換) |
file |
偵測檔案類型 | file script.sh、file --mime-type f |
3. 文本處理 (Text Processing)
| 指令 | 說明 | 常用用法 |
|---|---|---|
grep |
全域搜尋正則式 | grep pattern file、grep -r "text"、grep -i pattern、grep -v pattern |
egrep |
延伸正則搜尋 | egrep "pattern1|pattern2" |
fgrep |
固定字串搜尋 | fgrep "literal string" |
rg |
ripgrep (快速搜尋) | rg "pattern" -t py、rg --files | head |
sed |
流式編輯器 | sed 's/old/new/g' file、sed -i '1d' file、sed -n '5,10p' |
awk |
列式文字處理 | awk '{print $1}'、awk -F: '/root/ {print $7}' /etc/passwd |
cut |
切分字串 | cut -d: -f1 /etc/passwd、cut -c1-5 file |
sort |
排序 | sort file、sort -r file、sort -n file、sort -k2 file |
uniq |
去重排序列 | uniq file、sort file | uniq -c、uniq -d file |
tr |
字符轉換 | echo "HELLO" | tr 'A-Z' 'a-z'、tr -d ' ' |
diff |
比較檔案差異 | diff a b、diff -u a b、diff -r dir1 dir2 |
tee |
同時輸出到螢幕和檔案 | command | tee output.txt、tee -a file (追加) |
fold |
折行文字 | fold -w 80 file、fold -s (以空格折行) |
column |
格式化顯示表格 | column -t file、column -s: -t /etc/passwd |
paste |
合併行為表格 | paste file1 file2、paste -s file |
fmt |
格式化段落 | fmt -w 72 file、fmt -s file |
rev |
反轉每行字元 | echo "hello" | rev |
cut |
切分字串 | cut -d' ' -f1 file |
basename |
取得路徑最後部分 | basename /path/to/file.txt |
dirname |
取得路徑目錄部分 | dirname /path/to/file.txt |
4. 壓縮與解壓 (Compression)
| 指令 | 說明 | 常用用法 |
|---|---|---|
tar |
打包與解壓縮 | tar -czf file.tar.gz dir、tar -xzf file.tar.gz、tar -cjf file.tar.bz2 dir |
gzip |
gzip 壓縮 | gzip file、gzip -d file.gz、gzip -k file (保留原檔) |
gunzip |
解壓 gzip | gunzip file.gz |
zcat |
查看 gzip 檔不解壓 | zcat file.gz |
bzip2 |
BZip2 壓縮 | bzip2 file、bzip2 -d file.bz2、bzip2 -k file |
zip |
建立 zip 壓縮 | zip -r file.zip dir、zip -e file.zip file (加密) |
unzip |
解壓 zip | unzip file.zip、unzip -l file.zip (僅列出) |
xz |
XZ 壓縮 | xz file、xz -d file.xz、xz -k file |
7z |
7-Zip 壓縮 | 7z a file.7z dir、7z x file.7z |
zstd |
Zstandard 壓縮 | zstd file、zstd -d file.zst、zstd -19 file (最高壓縮率) |
5. 系統資訊 (System Info)
| 指令 | 說明 | 常用用法 |
|---|---|---|
uname |
顯示系統資訊 | uname -a、uname -r (核心版本) |
hostname |
顯示/設定主機名 | hostname、hostname -I (IP 位址) |
uptime |
顯示系統運行時間 | uptime、uptime -p (易讀格式) |
date |
顯示/設定日期時間 | date、date +%Y%m%d、date -u (UTC) |
uptime |
系統運行時間 | uptime、uptime -p |
free |
顯示記憶體使用情況 | free -h (易讀格式)、free -m (MB) |
df |
顯示磁碟空間 | df -h、df -i (inode 使用) |
du |
顯示目錄檔案大小 | du -sh dir、du -sh *、du --max-depth=1 |
top |
動態顯示進程資訊 | top、top -p PID、top -n 1 |
htop |
互動式進程查看器 | htop、htop PID |
ps |
顯示進程狀態 | ps aux、ps -ef、ps -ef | grep nginx |
who |
顯示已登入用戶 | who、whoami、who am i |
w |
顯示用戶與進程資訊 | w |
id |
顯示用戶與群組 ID | id、id username |
nproc |
顯示可用 CPU 核心數 | nproc |
lscpu |
顯示 CPU 詳細資訊 | lscpu |
lsblk |
列出磁碟設備 | lsblk、lsblk -f (顯示檔案系統) |
lspci |
列出 PCI 裝置 | lspci、lspci -v |
lsusb |
列出 USB 裝置 | lsusb、lsusb -v |
dmesg |
顯示核心日誌 | dmesg、dmesg | tail、dmesg | grep -i usb |
6. 進程管理 (Process Management)
| 指令 | 說明 | 常用用法 |
|---|---|---|
ps |
顯示進程資訊 | ps aux、ps -ef、ps -e -o pid,comm |
kill |
發送信號給進程 | kill PID、kill -9 PID、kill -15 PID |
pkill |
依名稱殺進程 | pkill nginx、pkill -f pattern、pkill -u username |
killall |
殺所有同名進程 | killall nginx、killall -9 nginx |
nohup |
跳離終端執行 | nohup command &、nohup command > log.txt 2>&1 & |
bg |
將進程放到背景 | bg、bg %1 |
fg |
將進程 bring 到前台 | fg、fg %1 |
jobs |
列出工作 | jobs -l |
top |
動態進程監視器 | top、top -p PID、top -n 1 -b (批次模式) |
htop |
互動式進程查看器 | htop、htop -p PID |
pgrep |
搜尋進程 ID | pgrep nginx、pgrep -u root、pgrep -f "pattern" |
pstree |
樹狀顯示進程 | pstree、pstree PID、pstree -p |
nice |
設定執行優先級 | nice -n 19 command、nice -n -5 command |
renice |
改變已執行進程優先級 | renice +5 PID、renice -10 PID |
strace |
追踪系統呼叫 | strace command、strace -p PID、strace -e trace=open |
lsof |
列出開啟檔案的進程 | lsof -i :80、lsof -u user、lsof /path/to/file |
fuser |
找出使用檔案/目錄的進程 | fuser file、fuser -k file (殺掉進程) |
pidof |
找出程序名稱的 PID | pidof nginx、pidof -s nginx (單一 PID) |
7. 磁碟與文件系統 (Disk & Filesystem)
| 指令 | 說明 | 常用用法 |
|---|---|---|
df |
磁碟空間使用情況 | df -h、df -i、df -hT |
du |
目錄/檔案大小 | du -sh dir、du -sh *、du --max-depth=1 |
mount |
掛載文件系統 | mount /dev/sda1 /mnt、mount | column、mount -a |
umount |
卸載文件系統 | umount /mnt、umount /dev/sda1、umount -l /mnt (懶卸載) |
fdisk |
分割硬碟 | fdisk -l、fdisk /dev/sda |
cfdisk |
互動式磁碟分割 | cfdisk /dev/sda |
mkfs |
建立文件系統 | mkfs.ext4 /dev/sda1、mkfs.vfat /dev/sdb1 |
fsck |
檢查/修復文件系統 | fsck /dev/sda1、fsck -y /dev/sda1 (自動修復) |
blkid |
顯示磁碟設備 UUID | blkid、blkid /dev/sda1 |
lsblk |
列出磁碟設備 | lsblk、lsblk -f、lsblk -t |
fdisk |
磁碟分割工具 | fdisk -l、fdisk /dev/sda |
partprobe |
重新載入分割表 | partprobe /dev/sda |
sync |
同步緩衝資料到磁碟 | sync |
mountpoint |
檢查掛載點 | mountpoint /mnt/disk |
findmnt |
查找/列出掛載資訊 | findmnt、findmnt /dev/sda1 |
8. 網路 (Network)
| 指令 | 說明 | 常用用法 |
|---|---|---|
ip |
新一代網路配置工具 | ip addr、ip link、ip route、ip neigh |
ifconfig |
舊版網路配置 (已棄用) | ifconfig、ifconfig eth0 |
ping |
測試網路連通性 | ping google.com、ping -c 4 google.com、ping -i 0.5 |
curl |
資料傳輸 (HTTP/FTP/等) | curl url、curl -O url、curl -X POST -d "data" url、curl -H "Header: value" |
wget |
下載網路檔案 | wget url、wget -O file url、wget -c url (斷點續傳) |
netstat |
顯示網路連線 | netstat -tlnp、netstat -an | grep :80 |
ss |
新版網路連線查看器 | ss -tlnp、ss -tulnp、ss -t state established |
dig |
DNS 查詢 | dig example.com、dig -x 8.8.8.8、dig MX example.com |
nslookup |
DNS 查詢工具 | nslookup example.com、nslookup -type=MX example.com |
nslookup |
DNS 反向查詢 | nslookup -type=mx example.com、nslookup -type=any example.com |
traceroute |
追踪路由 | traceroute google.com、traceroute -n |
tracepath |
追踪路由 (無需 root) | tracepath google.com |
ssh |
安全 shell 遠端連線 | ssh user@host、ssh -p 2222 host、ssh -i key.pem host |
scp |
安全複製遠端檔案 | scp local remote:、scp remote:/file local、scp -r dir remote: |
rsync |
遠端同步 | rsync -av src/ dest/、rsync -av --delete src/ dest/、rsync -avz --progress |
nc |
Netcat 網路工具 | nc -l 8080、echo "hi" | nc host 80、nc -zv host 80 (端口掃描) |
telnet |
Telnet 測試連線 | telnet host port |
iptables |
防火牆設定 | iptables -L、iptables -A INPUT -p tcp --dport 80 -j ACCEPT |
hostname |
顯示/設定主機名 | hostname、hostname -I |
route |
顯示/設定路由表 | route -n、route add default gw 192.168.1.1 |
9. 包管理 (Package Management)
| 指令 | 說明 | 常用用法 |
|---|---|---|
apt |
Debian/Ubuntu 包管理 | apt update、apt upgrade、apt install pkg、apt remove pkg、apt search pkg、apt list --installed |
apt-get |
apt 的舊版 CLI | apt-get install pkg、apt-get update |
yum |
RHEL/CentOS 包管理 | yum install pkg、yum update、yum remove pkg、yum list installed、yum search pkg |
dnf |
yum 的下一代 (Fedora) | dnf install pkg、dnf update、dnf remove pkg |
pacman |
Arch Linux 包管理 | pacman -Syu、pacman -S pkg、pacman -R pkg、pacman -Qs pkg |
zypper |
SUSE/openSUSE 包管理 | zypper install pkg、zypper update、zypper search pkg |
npm |
Node.js 包管理 | npm install、npm install pkg -g、npm update、npm uninstall pkg、npm list -g |
pip |
Python 包管理 | pip install pkg、pip uninstall pkg、pip list、pip freeze > requirements.txt、pip install -r requirements.txt |
snap |
Ubuntu Snap 包管理 | snap install pkg、snap list、snap remove pkg、snap refresh |
brew |
macOS Homebrew | brew install pkg、brew upgrade、brew list、brew uninstall pkg、brew search pkg |
rpm |
RHEL 套件管理 (底層) | rpm -qa、rpm -ivh pkg.rpm、rpm -e pkg |
dpkg |
Debian 套件管理 (底層) | dpkg -l、dpkg -i pkg.deb、dpkg -r pkg、dpkg -L pkg |
10. 用戶與群組 (Users & Groups)
| 指令 | 說明 | 常用用法 |
|---|---|---|
useradd |
建立新用戶 | useradd username、useradd -m username (建立家目錄)、useradd -M username (不建立家目錄) |
usermod |
修改用戶設定 | usermod -aG group user、usermod -s /bin/bash user (更改 shell)、usermod -L user (鎖定) |
userdel |
刪除用戶 | userdel username、userdel -r username (刪除家目錄) |
passwd |
修改密碼 | passwd (修改自己的)、passwd username (root 修改他人)、passwd -d username (清空) |
adduser |
新增用戶 (Debian 友好版) | adduser username (互動式) |
groupadd |
建立群組 | groupadd groupname、groupadd -g 1000 groupname |
groupdel |
刪除群組 | groupdel groupname |
su |
切換用戶 | su - username、su - (切換到 root)、su -c "command" user |
sudo |
以其他用戶權限執行命令 | sudo command、sudo -i (切換到 root)、sudo -u user command、sudo !! (重新以 sudo 執行上一個命令) |
whoami |
顯示當前用戶 | whoami、whoami -a (顯示所有資訊) |
id |
顯示用戶/群組 ID | id、id username、groups username |
chmod |
修改檔案權限 | chmod 755 file、chmod +x file、chmod -R 755 dir、chmod u+x,g-w,o=r file |
chown |
修改檔案擁有者 | chown user file、chown user:group file、chown -R user:group dir、chown :group file |
chgrp |
修改檔案群組 | chgrp group file、chgrp -R group dir |
umask |
顯示/設定預設權限 | umask、umask 022、umask 077 |
useradd |
建立用戶 (RHEL) | useradd -m -s /bin/bash user、useradd -r -s /sbin/nologin user (系統用戶) |
11. 系統服務 (Systemd & Services)
| 指令 | 說明 | 常用用法 |
|---|---|---|
systemctl |
管理 systemd 服務 | systemctl start nginx、systemctl stop nginx、systemctl restart nginx、systemctl enable nginx、systemctl status nginx、systemctl list-units --type=service、systemctl daemon-reload |
journalctl |
查看 systemd 日誌 | journalctl -u nginx、journalctl -f、journalctl --no-pager、journalctl -b (本次開機日誌) |
service |
舊版服務管理 | service nginx start、service nginx status |
crontab |
設定排程工作 | crontab -e、crontab -l、crontab -r、crontab file |
at |
一次性排程工作 | echo "command" | at 10:00、atq、atrm jobid |
shutdown |
關機 | shutdown -h now、shutdown -r +5、shutdown -c (取消) |
reboot |
重新啟動 | reboot、reboot -p (關機) |
halt |
停止系統 | halt、halt -p (關機) |
poweroff |
關閉電源 | poweroff、poweroff -h now |
init |
設定 runlevel | init 0 (關機)、init 3 (多用戶)、init 5 (圖形界面)、init 6 (重啟) |
cal |
顯示日曆 | cal、cal 2024、cal 6 2024 |
timedatectl |
時間設定 | timedatectl、timedatectl list-timezones、timedatectl set-timezone Asia/Taipei |
hwclock |
硬體時鐘 | hwclock --show、hwclock --systohc (系統時鐘 → 硬體) |
12. 文件壓縮與打包進階
| 指令 | 說明 | 常用用法 |
|---|---|---|
tar |
打包與壓縮 | tar -czf file.tar.gz dir、tar -xzf file.tar.gz、tar -cjf file.tar.bz2 dir、tar -xvf file.tar.gz -C /path、tar --exclude="*.git" -czf file.tar.gz dir |
gzip |
Gzip 壓縮 | gzip file、gzip -d file.gz、gzip -c file > file.gz、gzip -k file (保留原檔) |
gunzip |
解壓 Gzip | gunzip file.gz、gunzip -c file.gz > file |
bzip2 |
Bzip2 壓縮 | bzip2 file、bzip2 -d file.bz2、bzip2 -k file |
zip |
Zip 壓縮 | zip -r file.zip dir、zip -e file.zip file (加密)、zip -j file.zip file1 file2 (去除路徑) |
unzip |
解壓 Zip | unzip file.zip、unzip -l file.zip、unzip -t file.zip (檢驗) |
xz |
XZ 壓縮 | xz file、xz -d file.xz、xz -k file、xz -9 file (最高壓縮率) |
zcat |
查看 Gzip 檔案 | zcat file.gz、zcat file.gz | grep pattern |
bzcat |
查看 Bzip2 檔案 | bzcat file.bz2 |
xzcat |
查看 XZ 檔案 | xzcat file.xz |
13. 其他實用指令 (Utilities)
| 指令 | 說明 | 常用用法 |
|---|---|---|
date |
日期時間 | date、date +%Y-%m-%d、date -d "2 days ago"、date +%s (Unix timestamp) |
cal |
顯示日曆 | cal、cal 2024、cal 6 2024 |
bc |
命令列計算器 | echo "5+3" | bc、bc -l (浮點數)、echo "scale=2; 10/3" | bc |
jq |
JSON 處理 | cat data.json | jq、jq '.key' data.json、jq -r '.key' data.json、jq '.items[] | .name' |
xxd |
十六進位查看器 | xxd file、xxd -r -p hex.txt > file、xxd -l 100 file (僅前 100 位元組) |
base64 |
Base64 編碼/解碼 | echo "hello" | base64、echo "aGVsbG8=" | base64 -d |
md5sum |
MD5 校驗和 | md5sum file、md5sum -c checksum.md5 |
sha256sum |
SHA256 校驗和 | sha256sum file、sha256sum -c checksum.txt |
diff |
比較檔案差異 | diff a b、diff -u a b、diff -r dir1 dir2、diff -q a b (僅顯示差異) |
wc |
字詞計數 | wc -l file、wc -w file、wc -c file、wc -l /path/* |
seq |
產生數列 | seq 1 10、seq 0 2 10、seq -w 1 5 (補零) |
yes |
重複輸出字串 | yes | rm -i file、yes "text" | head -n 5 |
env |
顯示/設定環境變數 | env、env | grep PATH、env VAR=value command |
printenv |
顯示環境變數 | printenv PATH、printenv | sort |
export |
匯出環境變數 | export VAR=value、export -p (列出)、export PATH=$PATH:/new/bin |
alias |
設定別名 | alias ll='ls -la'、alias (列出)、unalias ll (移除) |
type |
顯示指令類型 | type ls、type -a python、type cd |
which |
查找指令位置 | which ls、which -a python |
whereis |
查找指令/源碼/手冊 | whereis python、whereis -b python |
man |
查看手冊頁 | man ls、man -k keyword、man 2 open (查看 section 2) |
info |
查看 info 文檔 | info bash、info --subnodes 'bash' |
help |
內建指令說明 | help cd、help echo、help -d (列表) |
history |
歷史指令 | history、history -c (清空)、history | grep ssh、!42 (執行第 42 條) |
!! |
重複執行上一個指令 | sudo !!、!! | grep、cd !:1 (上一個指令的第一個參數) |
echo |
輸出文字 | echo "hello"、echo -e "\n" (解譯脫逸字元)、echo $VAR、echo {a,b,c} (生成序列) |
printf |
格式化輸出 | printf "Hello %s\n" world、printf "%05d\n" 42 |
cat |
顯示/串接檔案 | cat file、cat -n file、cat a b > c、cat < |
head |
顯示開頭 | head -n 5、head -c 100 (前 100 位元組)、head -f file (持續追蹤) |
tail |
顯示結尾 | tail -n 5、tail -f file (持續追蹤)、tail -n +10 file (從第 10 行開始) |
touch |
建立/更新時間戳 | touch file、touch -t 202401011200 file、touch -r ref target |
stat |
顯示檔案詳細資訊 | stat file、stat -c "%n %s bytes" file |
rename |
批量重命名 | rename 's/old/new/' files、rename 'y/a-z/A-Z/' * |
file |
檢查文件類型 | file script.sh、file --mime-type f、file -i f |
dd |
低階拷貝工具 | dd if=/dev/sda of=backup.img、dd if=/dev/zero of=file bs=1M count=100、dd if=file of=/dev/null |
tee |
同時輸出到螢幕和檔案 | command | tee output.txt、tee -a file (追加)、tee file1 file2 |
fold |
折行文字 | fold -w 80 file、fold -s file (以空格折行) |
yes |
重複輸出 | yes "text" | head -n 100、yes | command |
watch |
定期執行命令 | watch -n 1 'df -h'、watch -n 5 'ps aux | head' |
screen |
終端多重視窗 | screen -S session、screen -r session、screen -ls |
tmux |
終端多重視窗 (現代版) | tmux new -s session、tmux a -t session、tmux ls、tmux kill-session -t session |
crontab |
排程工作 | crontab -e、crontab -l、crontab -r |
rsync |
同步文件 | rsync -av src/ dest/、rsync -av --delete src/ dest/、rsync -avz --progress |
locate |
快速搜尋檔案 | locate file、locate -i PATTERN、updatedb (更新資料庫) |
🎯 學習技巧: 背記不是目的,理解語義與參數才是。建議每天練習 5~10 個指令,熟悉它們的組合用法。使用man或tldr查詢速查即可。
快速上手:常用指令速查
# 文件操作
ls -la # 列出所有文件 (含隱藏)
touch file # 建立空檔
cp -r src/ dest/ # 遞迴複製
rm -rf file # 強制刪除 (危險!)
# 進程管理
ps aux | grep nginx # 查找進程
kill PID # 終止進程
kill -9 PID # 強制終止
top # 動態監視
killall nginx # 依名稱殺進程
# 磁碟與系統
df -h # 磁碟空間
du -sh * # 目錄大小
free -h # 記憶體使用
uname -a # 系統資訊
lscpu # CPU 資訊
# 網路
ip addr # 網路介面
ping google.com # 測試連通
ss -tlnp # 監聽埠
curl -O url # 下載檔案
wget url # 下載 (可恢復)
# 包管理
apt update # 更新索引 (Ubuntu)
apt install pkg # 安裝套件
yum install pkg # 安裝套件 (CentOS)
pip install pkg # Python 套件
npm install pkg # Node.js 套件
💡 Tip: 在終端機中按 TAB 可以自動補完檔案名與指令,按 ↑/↓ 翻找歷史指令。使用 Ctrl+R 可以反向搜尋歷史命令。
總結
Linux 命令列是經驗豐富工程師的利器。掌握基礎指令(ls、cd、grep、find、ps、kill、tar)後,就能用管道組合出強大的工作流程:
ps aux | grep nginx | awk '{print $2}' | xargs kill -9
🚀 記住: 命令列的威力在於「組合」 — 單個指令可能簡單,但透過管道 (|) 將它們串起來,就能完成復雜的任務。建議從tldr開始學習,然後逐漸掌握man頁的細節。