Mac mini 擁有 macOS 強大的底層系統,但許多人只用到圖形界面。終端機 (Terminal) 是 Mac mini 極其強大的一面 — 它基於 BSD Unix,支援大多數標準 Unix 指令,同時還有許多 macOS 特殊指令。

本文整理 150 個 Mac mini / macOS 常用指令,助你擊破終端機關門。

macOS 與 Ubuntu 的差異

macOS 基於 Darwin (BSD Unix) + Apple 圖形層。指令集上大致相同,但有以下差異:

功能 macOS Ubuntu (Linux)
預設 shell zsh (2019+) bash
包管理器 Homebrew (brew) apt
剪貼簿 pbcopy / pbpaste xsel / xclip
開啟文件 open xdg-open
查看 .app open -a / open -e 無對應
系統偏好設定 open -a "System Settings" 無對應

1. 文件與目錄操作 (macOS 版)

指令 說明 常用用法
ls 列出目錄內容 ls -lals -lhls -la@ (顯示延伸屬性)
cd 切換工作目錄 cd ~/Documentscd /Systemcd ..cd - (上一個目錄)
pwd 顯示當前目錄 pwdpwd -P (解析符號連結)
cp 複製檔案 cp a bcp -r src/ dest/cp -a src destcp -p file new (保留屬性)
mv 移動/重新命名 mv old newmv file ~/dir/mv -n file /Volumes/SSD/
rm 刪除檔案 rm filerm -rf dirrm -i file (互動式)
touch 建立/更新時間戳 touch filetouch -t 202401011200 filetouch -r ref file
mkdir 建立目錄 mkdir projectmkdir -p a/b/cmkdir -m 755 dir
rmdir 刪除空目錄 rmdir empty_dirrmdir -p a/b/c
find 搜尋檔案 find . -name "*.txt"find . -type ffind . -mtime -1find . -size +10M
ln 建立連結 ln -s target linkln -s ~/Documents/ /tmp/docs
open 用預設 App 開啟 open file.txtopen -e file.txt (用 TextEdit 開啟)、open -a "Google Chrome"open . (打開 Finder)
open -a 用指定 App 開啟 open -a "Visual Studio Code" .open -a iTunes ~/Music/song.mp3
open -e 用TextEdit 開啟 open -e README.mdopen -e file.txt
mdfind Spotlight 搜尋 CLI mdfind "filename"mdfind -name "*.pdf"
mdls 顯示檔案 metadata mdls file.txtmdls -name kMDItemKind file
md5 計算 MD5 (macOS) md5 fileecho "text" | md5
shasum SHA 校驗和 (macOS) shasum -a 256 fileshasum -a 1 file
base64 Base64 編碼/解碼 base64 filebase64 -D <<< "data==" (解碼)、echo "aGVsbG8=" | base64 -D
xxd 十六進位查看器 xxd filexxd -r -p hex file (僅 macOS 10.15+)

2. 剪貼簿操作 (Clipboard)

pbcopypbpaste 是 macOS 獨有的剪貼簿指令。
指令 說明 常用用法
pbcopy 複製到剪貼簿 echo "text" | pbcopycat file | pbcopypbcopy < file
pbpaste 從剪貼簿貼上 pbpastepbpaste > filepbpaste | grep pattern
pbcopy / pbpaste 進階用法 pbpaste | sed 's/old/new/g' | pbcopypbpaste | wc -c
pbnc 剪貼簿同步 (需安裝) pbnc --sync (在多台 Mac 間同步剪貼簿)

3. 文本處理 (Text Processing)

指令 說明 常用用法
grep 搜尋 grep pattern filegrep -ri "text" .grep -v patterngrep -n patterngrep -E "a|b"
sed 流式編輯 sed 's/old/new/g' filesed -i '' '1d' file (macOS 需要 '' 參數), sed -n '5,10p' file
awk 列式處理 awk '{print $1}'awk -F: '{print $1}' /etc/passwdawk '/pattern/ {print}'
cut 切分字串 cut -d: -f1 /etc/passwdcut -c1-5 filecut -d' ' -f1,3 file
sort 排序 sort filesort -r filesort -n filesort -k2 file
uniq 去重 sort file | uniq -cuniq -d fileuniq -u file
tr 字符轉換 echo "HELLO" | tr 'A-Z' 'a-z'tr -d ' 'tr '\n' ' '
diff 比較差異 diff a bdiff -u a bdiff -r dir1 dir2diff -q a b
column 格式化表格 column -t fileecho "a:b:c" | column -s: -t
fold 折行 fold -w 80 filefold -s file
fmt 格式化段落 fmt -w 72 filefmt -s file
rev 反轉字串 echo "hello" | revrev file
strings 顯示二進位中的文字 strings /bin/lsstrings -n 5 binary (最少 5 字元)
wc 字數統計 wc -l filewc -w filewc -c filecat file | wc -l
nl 顯示行號 nl filenl -ba file (所有行)、nl -v 5 file (從 5 開始)
paste 合併欄位 paste file1 file2paste -s filepaste -d',' file1 file2
uniq 去重排序列 sort file | uniq -cuniq -d file
tee 同時輸出到檔案 command | tee output.txttee -a filetee file1 file2
expand 將 tab 轉換為空格 expand fileexpand -t 4 file
unexpand 將空格轉為 tab unexpand -t 4 fileunexpand -a file

4. Homebrew 包管理 (Package Management)

macOS 的 Homebrew 是最流行的套件管理工具。安裝指令:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
指令 說明 常用用法
brew install 安裝套件 brew install wgetbrew install --cask google-chrome (安裝 macOS App)
brew uninstall 移除套件 brew uninstall wgetbrew uninstall --cask google-chrome
brew update 更新 Homebrew brew updatebrew upgrade (升級已安裝)、brew upgrade wget
brew list 列出已安裝 brew listbrew list --caskbrew list --formulabrew list -1 (單列)
brew search 搜尋套件 brew search pythonbrew search --cask chrome
brew info 套件資訊 brew info wgetbrew info --json python3
brew doctor 診斷問題 brew doctorbrew config (顯示配置)
brew clean 清理快取 brew cleanupbrew cleanup -s (保留舊版)、brew cleanup -n (預覽)
brew deps 查看相依性 brew depsbrew deps pythonbrew deps --tree python
brew bundle 批次安裝 brew bundle dump (匯出)、brew bundle (從 Brewfile 安裝)

5. 系統資訊 (System Information)

指令 說明 常用用法
uname 系統資訊 uname -auname -m (架構)、uname -r (核心版本)
sw_vers macOS 版本 sw_verssw_vers -productVersionsw_vers -buildVersion
system_profiler 系統詳細資訊 system_profiler SPHardwareDataTypesystem_profiler SPSoftwareDataTypesystem_profiler SPNetworkDataType
uptime 系統運行時間 uptimeuptime -p (易讀格式)
date 日期時間 datedate +%Y-%m-%ddate -u (UTC)
calendar 顯示日曆 calendar 1.1 (顯示 1月1日生日)
whoami 當前用戶 whoamiwhoami -a
id 用戶群組資訊 idid usernamegroups (顯示群組)
who 登入用戶 whowhoami
w 顯示用戶與負載 wtop -l 1 | head -10
nproc CPU 核心數 nprocsysctl hw.ncpusystem_profiler SPHardwareDataType | grep "Core"
free 顯示記憶體 (需 install) brew install freefree -hvm_stat (macOS 原生)
vm_stat 虛擬記憶體統計 (macOS) vm_statvm_stat 1 (每秒更新)
top 動態進程監控 top -l 1 (單次)、top -o cpu (依 CPU 排序)、top -o rsize (依記憶體排序)
htop 互動式監控 (需安裝) brew install htophtopsudo htop -u $USER
ps 顯示進程 ps auxps -efps -axps -Aps aux | grep nginx
lsof 列出開啟檔案的進程 lsof -i :80lsof -u $USERlsof /path/to/file
lscpu CPU 資訊 (部分支援) sysctl -a | grep machdep.cpusystem_profiler SPHardwareDataType
iostat I/O 統計 iostatiostat -w 1 (每秒更新)
sysctl 核心參數 sysctl -asysctl hwsysctl net.inet.ip.forwarding

6. 磁碟與存儲 (Disk & Storage)

指令 說明 常用用法
df 磁碟空間 df -hdf -Hdf -i (inode)
du 目錄大小 du -sh dirdu -sh *du --max-depth=1
diskutil 磁碟工具 CLI (macOS) diskutil listdiskutil info /dev/disk1diskutil eraseDisk APFS /dev/disk2diskutil mount /dev/disk2s1diskutil unmount /dev/disk2s1
mount 掛載文件系統 mount (顯示已掛載)、mount -vmount -t ntfs /dev/disk2s1 /Volumes/NTFS
umount 卸載 umount /Volumes/UNTITLEDdiskutil unmount /dev/disk2s1
lsblk 磁碟列表 (僅 Linux) macOS: diskutil list 對應
iostat 磁碟 I/O 統計 iostat -w 1iostat -c 5
fsck 檢查文件系統 fsck /dev/rdisk2s1 (macOS 用原始裝置)、fsck_hfs -f /dev/rdisk2s1fsck_apfs -f /dev/rdisk2s1
dd 低階拷貝 dd if=/dev/zero of=file bs=1m count=100dd if=/dev/rdisk0 of=backup.img bs=1m (從 SD 卡)、dd if=usb.img of=/dev/rdisk2 bs=1m (寫入 USB)
md5 MD5 校驗 md5 filemd5 /dev/rdisk2 (驗證 ISO)
shasum SHA 校驗 shasum -a 256 fileshasum -a 1 /dev/rdisk2
diskutil cs CoreStorage 管理 diskutil cs listdiskutil cs unlockVolume UUID
diskutil apfs APFS 管理 diskutil apfs listdiskutil apfs resizeContainer /dev/disk1 0
volumes 掛載磁卷 diskutil mount /dev/disk2s1diskutil unmount /dev/disk2s1
afdist APFS 診斷 afdist repair /dev/disk1s1
hdiutil 磁盤映像工具 hdiutil create -size 100m -fs HFS+ -volname "Backup" backup.dmghdiutil attach backup.dmghdiutil detach /dev/disk2

7. 網路 (Network)

指令 說明 常用用法
ipconfig IP 配置 (macOS 版) ipconfig getifaddr en0ipconfig getoption en0 bootps
ifconfig 網路介面 (舊版) ifconfig en0ifconfig | grep inetifconfig en0 | grep ether
ping 測試連通性 ping google.comping -c 4 baidu.comping -i 0.5 google.com
curl 資料傳輸 curl urlcurl -O urlcurl -X POST -d "data" urlcurl -H "Header: value"curl -L -o file.zip url
wget 下載 (需安裝) brew install wgetwget urlwget -c url (斷點續傳)、wget -r url (遞迴下載)
nslookup DNS 查詢 nslookup google.comnslookup -type=MX google.com
dig DNS 查詢 dig google.comdig -x 8.8.8.8dig MX google.com (需安裝)
host DNS 查詢 host google.comhost -t MX google.com (部分需安裝)
traceroute 路由追踪 traceroute google.comtraceroute -n google.com (需安裝: brew install traceroute)
tracepath 路由追踤 (Linux) macOS 對應 traceroutemtr
mtr 網路品質監測 brew install mtrsudo mtr google.commtr -c 10 google.com
netstat 網路連線 netstat -an | grep LISTENnetstat -i (介面)、netstat -rn (路由)
ss 新版網路 (Linux) macOS 對應 lsof -inetstat
lsof -i 列出網路連線 lsof -i :80lsof -i -P | grep LISTENlsof -iTCP -sTCP:LISTEN -P
arp ARP 表格 arp -aarp -d hostname (刪除)、arp -s hostname ip (添加)
route 路由表 route get google.comnetstat -rn (等效)
networksetup 網路設定 networksetup -listallhardwareportsnetworksetup -getairportnetwork en0sudo networksetup -setairportpower en0 off
/usr/libexec/airport Wi-Fi 管理 /usr/libexec/airport en0 scan/usr/libexec/airport en0 sniff 1
scutil 系統配置 scutil --dnsscutil --nc listscutil --proxy
dns-sd DNS Service Discovery dns-sd -G v4 en0dns-sd -B _http._tcp (瀏覧服務)

8. 進程管理 (Process Management)

macOS 的 pstop 有些與 Linux 不同,但核心概念相同。
指令 說明 常用用法
ps 顯示進程 ps auxps -efps -axps aux | grep nginxps -p PID (查看特定 PID)
top 動態監控 top -l 1 (單次)、top -o cpu (依 CPU 排序)、top -o rsize (依記憶體)、top -n 10 -l 1 (10 秒)、top -pid PID
kill 發送信號 kill PIDkill -9 PIDkill -15 PIDkill -SIGSTOP PID
pkill 依名稱殺進程 pkill nginxpkill -f "pattern"pkill -u usernamepkill -HUP nginx
killall 殺同名進程 killall Safarikillall -9 nginxkillall -u root
pgrep 搜尋進程 ID pgrep nginxpgrep -u $USERpgrep -f "pattern"pgrep -l nginx (顯示名稱)
nohup 跳離終端執行 nohup command &nohup command > log.txt 2>&1 &
bg / fg 背景/前臺 bg %1fg %1jobsjobs -l
disown 從工作移除 disown %1disown -h %1 (保留但不回應 SIGHUP)
renice 改變優先級 renice +5 PIDrenice -10 PIDrenice -n 10 PID
nice 設定優先級啟動 nice -n 19 commandnice -n -5 command
pidof 查找 PID pidof nginxpgrep -x nginx (更精確)
pstree 樹狀進程 pstree PIDpstree -p PID
launchctl macOS 服務管理 launchctl listlaunchctl load ~/Library/LaunchAgents/com.user.agent.plistlaunchctl unload ~/Library/LaunchAgents/com.user.agent.plistsudo launchctl bootstrap system /Library/LaunchDaemons/com.service.plist
brew services Brew 服務管理 brew services listbrew services start mysqlbrew services restart nginxbrew services stop redis
lsof 列出開啟文件 lsof -i :80lsof +D /pathlsof -c processlsof -p PID
strace 追踪系統呼叫 (Linux) macOS 對應 dtraceopensnoop
dtrace DTrace 動態追蹤 (macOS) sudo dtrace -n "syscall::open:entry { printf("%s opened %s", execname, copyinstr(arg0)); }"
opensnoop DTrace 開啟檔案查看 sudo opensnoop -n Safari

9. 系统服务與偏好設定 (System Preferences)

指令 說明 常用用法
defaults 修改偏好設定 defaults write com.apple.finder AppleShowAllFiles YESdefaults readdefaults delete com.apple.finderdefaults write NSGlobalDomain AppleShowScrollBars -string "Always"
open -a 開啟 App open -a "System Settings"open -a Safariopen -a "Activity Monitor"open -a "App Store"
open -b 用 bundle ID 開啟 open -b com.apple.Safari https://apple.com
open -R 在 Finder 中顯示 open -R /path/to/file
osascript 執行 AppleScript osascript -e 'display notification "Hello"'osascript hello.scptosascript -e 'tell app "Safari" to activate'
say 語音合成 say "Hello"say -v Alex "Hello" (指定聲音)、say -f file.txt (從檔案朗讀)
afplay 播放音效 afplay /System/Library/Sounds/Glass.aiffafplay sound.mp3
pmset 電源管理 pmset -g batt (電池資訊)、sudo pmset displaysleepnow (立即螢幕睡眠)
caffeinate 防止休眠 caffeinate -i (防止睡眠)、caffeinate -u -t 3600 (1 小時後喚醒)
screencapture 螢幕截圖 screencapture file.pngscreencapture -x file.png (無聲)、screencapture -i file.png (互動模式)、screencapture -T 5 file.png (5 秒延遲)
sips 圖像處理 sips -s format png input.jpg --out output.pngsips -Z 500 image.png (調整大小)
mdls 檔案 metadata mdls filemdls -name kMDItemContentType file
mdutil Spotlight 索引管理 mdutil -s / (顯示狀態)、sudo mdutil -i on / (開啟)、sudo mdutil -i off / (關閉)
tccutil 隱私權控制 tccutil reset All com.apple.Terminaltccutil reset Camera
spctl Gatekeeper 檢查 spctl --assess --verbose /Applications/App.appsudo spctl --master-disable (開啟任何來源)
xattr 延伸屬性 xattr -l file (列出)、xattr -d attr file (移除)、xattr -w attr val file (設定)
codesign 代碼簽名 codesign --verify App.appcodesign -s "Developer ID" App.app
plutil plist 轉換 plutil -convert json file.plistplutil -convert xml1 file.plistplutil -p file.plist
security 密鑰圈管理 security find-generic-password -s "service"security set-keychain-settings
brew services 套件服務管理 brew services listbrew services start mysqldbrew services stop redisbrew services restart nginx

10. 開發者指令 (Developer Tools)

指令 說明 常用用法
git 版本控制 git statusgit clone urlgit commit -m "msg"git push
python3 Python 直譯器 python3 script.pypython3 -m http.server 8000python3 -c "import os; print(os.name)"
node Node.js 執行環境 node app.jsnode -e "console.log('hi')"npm installnpm start
pip3 Python 包管理 pip3 install pkgpip3 listpip3 freeze > requirements.txtpip3 install -r requirements.txt
ruby Ruby 直譯器 ruby script.rbgem install pkgbundle installrbenv install 3.2.0
php PHP CLI php -S localhost:8000php -acomposer install
make 自動化建構 makemake cleanmake test
gcc / clang C/C++ 編譯器 gcc -o program main.cclang -framework Carbon -o app app.m
xcodebuild Xcode 建構 xcodebuild -project App.xcodeprojxcodebuild -listxcodebuild -scheme App -destination 'platform=iOS Simulator,name=iPhone 15' build
simctl iOS 模擬器控制 xcrun simctl listxcrun simctl boot iPhone 15xcrun simctl install booted App.appxcrun simctl openurl booted "https://apple.com"
swift Swift 直譯器 swift script.swiftswift -I /usr/lib
pod CocoaPods pod installpod updatepod search podname
gem RubyGems gem install bundlergem listgem update --system
brew Homebrew brew install wgetbrew listbrew updatebrew upgradebrew doctor
pipx Python App 隔離 (可選) pipx install pkgpipx listpipx run --spec pkg
conda Anaconda 環境 conda create -n env python=3.11conda activate envconda install pkgconda env list
code VS Code CLI code . (打開資料夾)、code -r file (打開檔案)、code --diff file1 file2code --install-extension extension-id
subl Sublime Text CLI subl .subl filesubl -n (新視窗)
atom Atom CLI (已棄用) atom .atom file
vim / nano 命令列編輯器 vim filenano filevim +/pattern file

11. macOS 快捷鍵 (Keyboard Shortcuts)

在終端機中,快捷鍵可以大幅提高效率。
Cmd + K
快捷鍵 功能
Ctrl + A 游標移到行首
Ctrl + E 游標移到行尾
Ctrl + B 游標左移一個字元
Ctrl + F 游標右移一個字元
Ctrl + K 刪除游標到行尾
Ctrl + U 剪下游標到行首
Ctrl + W 刪除游標前一個單詞
Ctrl + L 清空螢幕
Ctrl + R 反向搜尋歷史指令
清空終端機螢幕
Cmd + N 新建 Terminal 視窗
Cmd + T 新增分頁
Cmd + W 關閉分頁
Cmd + Shift + T 重新開啟關閉的分頁
Cmd + 1 ~ Cmd + 9 切換分頁
Cmd + Enter 全螢幕切換

12. 快速速查 (Cheat Sheet)

日常開發

# 打開 Finder 到當前目錄
open .

# 打開 VS Code 到當前目錄
code .

# 查看當前目錄
pwd

# 列出所有文件
ls -la

# 快速切換到常用目錄
cd ~/Documents

# 查看最近指令
history | tail -10

# 搜尋指令歷史
history | grep "git"

# 終止進程
kill PID

系統管理

# 查看 macOS 版本
sw_vers

# 查看系統資訊
system_profiler SPHardwareDataType | grep "Model Identifier"

# 清理 DNS 快取
sudo dscache -flushcache; sudo killall -HUP mDNSResponder

# 重啟核心服務
sudo killall coreservicesd

# 查看電池資訊
pmset -g batt

# 關閉螢幕 (快速鎖定)
pmset displaysleepnow

開發工具

# 啟動本地 Python 伺服器
python3 -m http.server 8000

# 啟動 Node.js 開發伺服器
npm start

# 安裝 npm 套件
npm install package-name

# 執行 npm 指令
npx create-react-app my-app

# 更新 Homebrew 與套件
brew update && brew upgrade

# 清理 Homebrew 快取
brew cleanup
⚠️ macOS 2026 版本注意事項: macOS 2026 (Sonoma 2) 加強了隱私權與安全性,部分指令(如 dtracecsrutil)需要關閉 SIP 或取得特許權限。

總結

macOS 終端機結合了 BSD Unix 的強大與 Apple 圖形界面的便利。熟記以下幾類指令即可應付大多數場景:

  • 文件操作ls, cd, cp, mv, rm, find — 基本操作
  • 剪貼簿pbcopy, pbpaste — macOS 獨有
  • 包管理brew, brew cask — 開發必備
  • 系統管理launchctl, defaults, pmset — macOS 特有
  • 網路ping, curl, ssh, arp — 與 Linux 類似
🎯 學習路線: 先掌握標準 Unix 指令 (ls, cd, grep, find等),再學習 macOS 特有指令 (brew, open, defaults),最後配上剪貼簿與快捷鍵,效率立即翻倍。