目录

Mac 实用命令收集一

『千里之行,始于足下』,掌握高频使用的命令也不例外。积铢累寸,总有一天,你终会在命令行中游刃有余。

让终端说英语

1
2
3
4
$ say hello

// 让程序执行完了说一声
$ sleep 5 && say "this is a test"

标记文件或者文件夹,用 Spotlight 打开

选中一个文件或者文件夹,Command+I 打开简介,在添加标记中输入关键词,关闭简介窗口。打开 Spotlight 输入刚才的关键词,可以准确定位

使用 sips 命令批量处理图片

1
2
3
4
5
6
7
8
// 将图片宽度缩小为 800px,高度按比例缩放
$ sips -Z 800 ~/001.jpg

// 顺时针旋转 90$ sips -r 90 ~/001.jpg

// 垂直反转
$ sips -f vertical ~/001.jpg

把当前网页附加到待发送邮件中

Safari 浏览器中浏览网页时,通过 Command+I 可以直接打开邮件并把当前网页附加到待发送的邮件中

删除文件和清空纸篓

删除文件:Command+Delete 彻底删除:Command+Alt+Delete

安装 Go2Shell,快速在当前目录打开终端的工具

默认情况下是打开系统终端,这里配置成用 iTerm2 打开终端

1
$ open -a Go2Shell --args config

修改终端电脑名称

1
2
$ sudo scutil --set HostName your-host-name
$ hostname // 查看 `hostname`

快速复制和粘贴

1
2
3
4
5
6
7
8
// 将列出的主目录复制到剪贴板
$ ls ~ | pbcopy

// 文件的内容读入剪贴板
$ pbcopy < ~/.zshrc

// 将剪贴板的内容写入到文件中
$ pbpaste >> ~/demo.php

找回任何来源选项

1
2
// 找回安全与隐私中的信任 "任何来源" 选项
$ sudo spctl --master-disable

快速查看日历

1
2
3
$ cal
$ cal 2018

Xcode Command Line Tools

检查 Xcode 安装包是否已经被安装

1
$ xcode-select -p

安装

1
$ xcode-select --install

Mac 的启动组合键

这些组合键适用于基于 Intel 的 Mac 电脑。在 Mac 启动后,可以使用其他 Mac 键盘快捷键

ShortcutDescription
Shift (⇧)安全模式启动
Option (⌥)启动进入启动管理器
D从内建的 Apple Hardware TestApple Diagnostics 实用工具启动,具体取决于 Mac 机型
N从兼容的 NetBoot 服务器(若可用)启动。要使用 NetBoot 服务器上默认的引导映像,则请按住 Option-N
Command+R从内建的 macOS 恢复系统启动。或者可以使用 Option+Command+RShift+Option+Command+R 以通过互联网从 macOS 恢复功能启动
Option+Command+P+R重置 NVRAM(或 PRAM
Command+S单用户模式启动
T目标磁盘模式启动
Command+V详细模式启动
推出键 (⏏)、F12、鼠标键或触控板按钮推出可移动介质

终端中上传下载

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Mac 安装
$ brew install lrzsz

// Linux 安装
$ yum install -y lrzsz
$ apt install -y lrzsz

// Intel Chip
$ cd /usr/local/bin

// M1 Chip
$ cd /opt/homebrew/bin

// 创建 iterm2-recv-zmodem.sh 文件
$ vim iterm2-recv-zmodem.sh
#!/bin/bash
# Author: Matt Mastracci (matthew@mastracci.com)
# AppleScript from http://stackoverflow.com/questions/4309087/cancel-button-on-osascript-in-a-bash-script
# licensed under cc-wiki with attribution required
# Remainder of script public domain

osascript -e 'tell application "iTerm2" to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm
if [[ $NAME = "iTerm" ]]; then
  FILE=`osascript -e 'tell application "iTerm" to activate' -e 'tell application "iTerm" to set thefile to choose folder with prompt "Choose a folder to place received files in"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
else
  FILE=`osascript -e 'tell application "iTerm2" to activate' -e 'tell application "iTerm2" to set thefile to choose folder with prompt "Choose a folder to place received files in"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
fi

if [[ $FILE = "" ]]; then
  echo Cancelled.
  # Send ZModem cancel
  echo -e \\x18\\x18\\x18\\x18\\x18
  sleep 1
  echo
  echo \# Cancelled transfer
else
  cd "$FILE"

  # TODO: Here according to the actual command address
  /usr/local/bin/rz -E -e -b
  sleep 1
  echo
  echo
  echo \# Sent \-\> $FILE
fi

// 创建 iterm2-send-zmodem.sh 文件
$ vim iterm2-send-zmodem.sh
#!/bin/bash
# Author: Matt Mastracci (matthew@mastracci.com)
# AppleScript from http://stackoverflow.com/questions/4309087/cancel-button-on-osascript-in-a-bash-script
# licensed under cc-wiki with attribution required
# Remainder of script public domain

osascript -e 'tell application "iTerm2" to version' > /dev/null 2>&1 && NAME=iTerm2 || NAME=iTerm
if [[ $NAME = "iTerm" ]]; then
  FILE=`osascript -e 'tell application "iTerm" to activate' -e 'tell application "iTerm" to set thefile to choose file with prompt "Choose a file to send"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
else
  FILE=`osascript -e 'tell application "iTerm2" to activate' -e 'tell application "iTerm2" to set thefile to choose file with prompt "Choose a file to send"' -e "do shell script (\"echo \"&(quoted form of POSIX path of thefile as Unicode text)&\"\")"`
fi
if [[ $FILE = "" ]]; then
  echo Cancelled.
  # Send ZModem cancel
  echo -e \\x18\\x18\\x18\\x18\\x18
  sleep 1
  echo
  echo \# Cancelled transfer
else
  # TODO: Here according to the actual command address
  /usr/local/bin/sz "$FILE" -e -b
  sleep 1
  echo
  echo \# Received $FILE
fi

// 添加执行权限
$ chmod +x /path/to/iterm2-*

打开终端的偏好,Profiles > Profile Name (选中你自定义设置) > Advanced > Triggers,点击编辑,进行如下设置:

Regular ExpressionActionParametersInstantEnabled
rz waiting to receive.\*\*B0100Run Silent Coprocess.../opt/homebrew/bin/iterm2-send-zmodem.shFalseTrue
\*\*B00000000000000Run Silent Coprocess.../opt/homebrew/bin/iterm2-recv-zmodem.shFalseTrue

终端下批量替换目录下文件中的内容

1
2
3
4
5
6
7
8
# 查看当前目录下所有文件中的 foo 内容
grep -nr 'foo' .

# 预览将要替换的文件内容
grep -rl 'foo' . | xargs sed 's/foo/bar/g'

# 批量替换
grep -rl 'foo' . | xargs sed -i '' 's/foo/bar/g'