Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Shell 程式設計

40 個實用的 Mac OS X 終端命令與 Shell 技巧

以下整理了一批 Mac OS X 的終端命令並做了分類。其中多數在 Linux/Unix 上同樣通用,Windows users 也能透過類似的工具找到對應作法。

系統

shutdown -r now    # 重新開機
shutdown now       # 關機

電源管理

pmset -g                       # 檢視目前電源設定
sudo pmset displaysleep 15     # 螢幕閒置 15 分鐘後關閉
sudo pmset sleep 30            # 系統閒置 30 分鐘後休眠

OS X 外觀

defaults write com.apple.dashboard mcx-disabled -boolean YES && killall Dock   # 關閉儀表板
defaults write com.apple.dashboard mcx-disabled -boolean NO  && killall Dock   # 開啟儀表板
defaults write com.apple.finder AppleShowAllFiles TRUE    # 顯示隱藏檔
defaults write com.apple.finder AppleShowAllFiles FALSE   # 隱藏檔案

網路

ping -o linuxidc.net                          # ping 主機(-o 收到回應即結束)
traceroute linuxidc.net                       # 追蹤路由節點
curl -I www.linuxidc.net | head -n 1          # 檢查網站 HTTP 是否可用
man net                                       # Windows 網路管理(類 NET 指令)
dig www.linuxidc.net A                        # 查 A 記錄
dig www.linuxidc.net MX                       # 查 MX 記錄
w                                             # 誰登入了這台 Mac
netstat -r                                    # 路由表
netstat -an                                   # 活動連線
netstat -s                                    # 網路統計

故障排除

lsof                                                    # 列出所有開啟的檔案
sudo launchctl unload /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
sudo launchctl load /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist   # 重啟 Bonjour
diskutil eject disk1                                    # 退出 CD(編號未必是 disk1)

文字處理(搭配剪貼板超好用)

pbpaste | wc -l                  # 行數
pbpaste | wc -w                  # 單字數
pbpaste | sort | pbcopy          # 排序後寫回
pbpaste | rev | pbcopy           # 每行倒序
pbpaste | sort | uniq | pbcopy   # 去重複
pbpaste | sort | uniq -d | pbcopy   # 只留重複出現的行
pbpaste | sort | uniq -u | pbcopy   # 只留未重複的行
pbpaste | tidy | pbcopy          # 清理 HTML
pbpaste | head -n 5              # 前 5 行
pbpaste | tail -n 5              # 後 5 行
pbpaste | expand | pbcopy        # Tab 轉空白

其他好用的

htpasswd -nb username password         # 產生 Basic Auth 帳密
history                               # 檢視先前輸入的命令
textutil -convert html file.extension # 轉 HTML(支援 txt/rtf/doc)
nano [file]                           # 簡易編輯器;Ctrl+O 存、Ctrl+X 離開
clear                                 # 清螢幕

Apache 目錄加 Basic Auth 的設定片段:

AuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/your/site/.htpasswd
require valid-user

iTunes

defaults write com.apple.iTunes invertStoreLinks -bool YES   # 點連結走本機庫
defaults write com.apple.iTunes invertStoreLinks -bool NO    # 點連結走 iTunes Store

補充兩點:上面用 pbpaste 取剪貼板內容,若來源是檔案,換成 cat [/path/to/filename] 即可;想把結果存到桌面檔案,把 pbcopy 換成 > ~/Desktop/filename.txt。

延伸資源:Cameron Hayne's Bash Scripts、Mac OS X Hints、Apple Forums。

Copyright © Linux教程網 All Rights Reserved