macOS: Useful Shell Commands

Activate App in New Instance | 应用双开

1
nohup /Applications/zoom.us.app/Contents/MacOS/zoom.us > /dev/null 2>&1 &
1
2
echo "nohup /Applications/WeChat.app/Contents/MacOS/WeChat >/dev/null 2>&1 &" > ~/Applications/WeChat.command
chmod +x ~/Applications/WeChat.command

> /dev/null redirects standard output (stdout) to /dev/null, which discards it. 2>&1 redirects standard error (2) to standard output (1), which then discards it as well since standard output has already been redirected.

Activate App in Specified Locale | 特定语言打开应用

1
open -a /System/Applications/Maps.app --args -AppleLanguages '(zh)'

Works even for apps that do not follow System Settings → General → Language & Region.

Change Screenshot File Format to JPG | 更改截屏图像格式为JPG

defaults write com.apple.screencapture type jpg; killall SystemUIServer

To revert: defaults write com.apple.screencapture type png; killall SystemUIServer

Hide File | 隐藏文件 / 显示隐藏文件

1
2
sudo chflags -R hidden ‹file path›
sudo chflags -R nohidden ‹filepath›

Kill Process | 终止进程

1
killall WeChat
1
kill -9 $(pgrep "WeChat")

Signal -9 means “non-catchable, non-ignorable kill”.

Reset Safari Extensions | 重置Safari浏览器扩展

1
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user

Reset Bluetooth | 重启蓝牙

1
sudo pkill bluetoothd

Reset Wi-Fi | 重启Wi-Fi

1
sudo ifconfig en0 down && sudo ifconfig en0 up
1
2
# 列出网络设备界面标识
networksetup -listnetworkserviceorder