Mac 里的不少配置可以通过特定的命令行来设定,以下是我比较常用的、感兴趣的命令。我一般是在 zsh 里对这些常用的命令设定 alias,非常方便。
更多的命令请参考:
Finder
Show All File Extensions 显示所有的文件扩展名
1
| defaults write -g AppleShowAllExtensions -bool true
|
Hide Folder in Finder 隐藏 Finder 里的指定文件夹
1
| chflags hidden /path/to/folder/
|
Show Hidden Files 显示隐藏文件
隐藏文件是指以.
开头的系统文件等,在 Finder 里默认隐藏;开启显示后,数目会很多,建议平时不用时关闭
1 2 3 4 5
| # Show All #要重启才行? defaults write com.apple.finder AppleShowAllFiles true
# Restore Default File Visibility defaults write com.apple.finder AppleShowAllFiles false
|
Unhide User Library Folder 不隐藏用户的库文件夹
1
| chflags nohidden ~/Library
|
Increase Number of Recent Places 增加 Finder 中的“最近使用文件夹”数量
1 2
| defaults write -g NSNavRecentPlacesLimit -int 10 && \ killall Finder
|
Makes possible to see Finder menu item “Quit Finder” with default shortcut Cmd + Q
这个好用,这样就能使用 Cmd + Q
一次性关闭所有的 Finder 窗口了
1 2 3 4 5 6 7
| # Enable defaults write com.apple.finder QuitMenuItem -bool true && \ killall Finder
# Disable (Default) defaults write com.apple.finder QuitMenuItem -bool false && \ killall Finder
|
Path Bar 显示路径栏
开启后会在 Finder 底部显示当前路径信息
1 2 3 4 5
| # Show defaults write com.apple.finder ShowPathbar -bool true
# Hide (Default) defaults write com.apple.finder ShowPathbar -bool false
|
Status Bar 状态栏
在 Finder 最底部,显示当前文件夹项目数量、光标选中数量、硬盘可用容量
1 2 3 4 5
| # Show defaults write com.apple.finder ShowStatusBar -bool true
# Hide (Default) defaults write com.apple.finder ShowStatusBar -bool false
|
Set Current Folder as Default Search Scope 设置当前文件夹为默认搜索域
整理有序的话,大部分情况下只是搜索当前目录,这样就不用每次都要多点击一次来选择了
1
| defaults write com.apple.finder FXDefaultSearchScope -string "SCcf"
|
Layout
Desktop Icon Visibility 显示/隐藏桌面图标
OS X 桌面的右键功能里没有 Windows 的“隐藏桌面图标”选项,可以通过下面的命令实现
1 2 3 4 5 6 7
| # Hide Icons defaults write com.apple.finder CreateDesktop -bool false && \ killall Finder
# Show Icons (Default) defaults write com.apple.finder CreateDesktop -bool true && \ killall Finder
|
Battery 电池
哈哈,这个纯粹是列在这儿,平时并没多大用
Remaining Battery Percentage 剩余电量
1
| pmset -g batt | egrep "([0-9]+\%).*" -o --colour=auto | cut -f1 -d';'
|
Remaining Battery Time 电池剩余使用时间
1
| pmset -g batt | egrep "([0-9]+\%).*" -o --colour=auto | cut -f3 -d';'
|
Power Management 电源管理
Prevent System Sleep 防止系统进入睡眠一段时间
参数单位是秒
Prevent sleep for 1 hour:
Put Display to Sleep after 15 Minutes of Inactivity 一段时间不活动后关闭显示器
参数单位是分钟
1
| sudo pmset displaysleep 15
|
Put Computer to Sleep after 30 Minutes of Inactivity 一段时间不活动后使 Mac 睡眠
参数单位是分钟
Check System Sleep Idle Time 查看系统进入休眠的间隔
目前我的是 1 分钟
1 2
| $ sudo systemsetup -getcomputersleep Computer Sleep: after 1 minutes
|
Set System Sleep Idle Time to 60 Minutes 设置系统进入休眠的间隔
1
| sudo systemsetup -setcomputersleep 60
|
Audio
Set Audio Volume
1
| osascript -e 'set volume 4'
|
Play Audio File
You can play all audio formats that are natively supported by QuickTime.
1
| afplay -q 1 filename.mp3
|
TTS
吐槽下,综合各方面考虑,Mac 上可用的 TTS 大概最好的就是系统自带的了。然而——还是那么地垃圾……
Speak Text with System Default Voice 用 TTS 发音朗读文本
1
| say 'All your base are belong to us!'
|
Create Audiobook From Text
Uses “Alex” voice, a plain UTF-8 encoded text file for input and AAC output.
1
| say -v Alex -f file.txt -o "output.m4a"
|
Ping a Host to See Whether It’s Available
这个大概是天朝特色了……
Troubleshoot Routing Problems
SSH
Remote Login 远程登录
1 2 3 4 5
| # Enable remote login sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist
# Disable remote login sudo launchctl unload -w /System/Library/LaunchDaemons/ssh.plist
|
TCP/IP
Show Application Using a Certain Port 显示当前占用特定端口的应用列表 (强烈推荐
)
这个用来检查端口占用很不错
This outputs all applications currently using port 80.
Show External IP Address
1
| dig +short myip.opendns.com @resolver1.opendns.com
|
Wi-Fi
Show Local IP Address
Show Wi-Fi Connection History
1
| defaults read /Library/Preferences/SystemConfiguration/com.apple.airport.preferences | grep LastConnected -A 7
|
Show Wi-Fi Network Passwords
Exchange SSID with the SSID of the access point you wish to query the password from.
1
| security find-generic-password -D "AirPort network password" -a "SSID" -gw
|
Spotlight
这个要怎么用?听说这个很久了,还没去具体了解使用
Spotlight Indexing
1 2 3 4 5
| # Disable mdutil -i off -d /path/to/volume
# Enable (Default) mdutil -i on /path/to/volume
|
Erase Spotlight Index and Rebuild 重建索引
1
| mdutil -E /path/to/volume
|
Search via Spotlight 搜索
1
| mdfind -name 'searchterm'
|
Screenshots 屏幕截图
目前在用软件Snip
,满足基本所需了
Take Delayed Screenshot
Takes a screenshot as JPEG after 3 seconds and displays in Preview.
1
| screencapture -T 3 -t jpg -P delayedpic.jpg
|
Save Screenshots to Given Location
Sets location to ~/Desktop
.
1 2
| defaults write com.apple.screencapture location ~/Desktop && \ killall SystemUIServer
|
Sets format to png
. Other options are bmp
, gif
, jpg
, jpeg
, pdf
, tiff
.
1
| defaults write com.apple.screencapture type -string "png"
|
Disable Shadow in Screenshots 隐藏截图里的阴影
有阴影好看很多
1 2
| defaults write com.apple.screencapture disable-shadow -bool true && \ killall SystemUIServer
|
Set Default Screenshot Name
Date and time remain unchanged.
1 2
| defaults write com.apple.screencapture name "Example name" && \ killall SystemUIServer
|
Login Window
Set Login Window Text
1
| sudo defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText "Your text"
|
Security
Application Firewall
这个是不是可以阻止应用联网??(之前试了下,好像没什么效果,😓)
Firewall Service
1 2 3 4 5 6 7 8
| # Show Status sudo /usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate
# Enable sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate on
# Disable (Default) sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate off
|
Add Application to Firewall
1
| sudo /usr/libexec/ApplicationFirewall/socketfilterfw --add /path/to/file
|
Passwords
Generate Secure Password and Copy to Clipboard
1
| LC_ALL=C tr -dc "[:alpha:][:alnum:]" < /dev/urandom | head -c 20 | pbcopy
|
Physical Access
Launch Screen Saver 启动屏幕保护程序
1
| open /System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ScreenSaverEngine.app
|
Lock Screen 锁屏
1
| /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend
|
1 2 3 4 5 6 7 8
| # Status defaults read com.apple.screensaver askForPasswordDelay
# Enable (Default) defaults write com.apple.screensaver askForPasswordDelay -int 0
# Disable (Integer = lock delay in seconds) defaults write com.apple.screensaver askForPasswordDelay -int 10
|
Screensaver Password
1 2 3 4 5 6 7 8
| # Status defaults read com.apple.screensaver askForPassword
# Enable defaults write com.apple.screensaver askForPassword -int 1
# Disable (Default) defaults write com.apple.screensaver askForPassword -int 0
|
Shutdown 关机
Uptime 已开机时间
How long since your last restart.
1 2
| $ uptime 15:38 up 19 days, 14:48, 6 users, load averages: 1.85 1.55 1.47
|