Mac 使用笔记(二)

1. iPhone每次连接Mac都会弹出iPhoto

解决办法:

  1. Plug in your iPad/iPhone
  2. Open Image Capture
  3. Select your device (“devMikePad”)
  4. Press the triangle in square symbol in the lower left corner.
  5. Select “No application” in the menu.

iPhoto: Preferences > General > “Connecting Camera Opens …” set it to “No Application”

2. Add environment variable

1
2
// ~/.bash_profile
export var=value

3.SSH ask passpharse every time I use SSH key.

A:

1
$echo -e "AddKeysToAgent yes\nUseKeychain yes" >> ~/.ssh/config

4.Fix the Enable and Disable install software from anywhere in macOS Sierra

A:

1
2
3
4
5
// Enable
$ sudo spctl --master-disable

// Disable
$sudo spctl --master-enable

Reference:Fix the Enable and Disable install software from anywhere in macOS Sierra problem

5.Mac 上批量替换文件中的字符串

A:

1
$ grep -rl discription source/_posts | xargs sed -i ''  "s/discription/description/g"

Reference:linux 批量替换文件内容及查找某目录下所有包含某字符串的文件(批量修改文件内容)
论mac使用sed修改文件的正确姿势

6.如何查看占用指定端口的进程?

A:

1
$ lsof -i :[port]

结果输出中有进程的 pid, 之后我们可以用这个 pid 来杀掉该进程:

1
$ kill -9 [pid]

这些进程很可能是系统启动时就启动了,可以通过:

1
$ launchctl list | grep [pid]

来确认。如果找到有,而我们又不想它自启动了,可以在以下目录中找到配置文件:

1
2
3
~/Library/LaunchAgents         Per-user agents provided by the user.
/Library/LaunchAgents          Per-user agents provided by the administrator.
/Library/LaunchDaemons         System wide daemons provided by the administrator.

找到之后,可以用命令:

1
2
3
$ launchctl unload -w paths
// eg:
$ launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist

查看启动进程的命令:

1
ps -p <pid> -wwf

7.如何使用命令行安装 dmg?

A:

First, mount the dmg image : sudo hdiutil attach <image>.dmg

The image will be mounted to /Volumes/. Then we can install with: sudo installer -package /Volumes/<image>/<image>.pkg -target /

Finally unmount the image: sudo hdiutil detach /Volumes/<image>.

这个需求是源于最近一次安装 mysql 时,图形界面安装无响应,之后尝试命令行成功安装,还蛮奇怪。

Reference:Is there a command to install a dmg

8.How to start Mac’s built-in SMTP server?

A:Mac include a built-in SMTP server, but its configuration on Mojave seems not property for daemon. Because KeepAlive not set to true, so we have to correct it. However its configuration file locate at /System/Library/LaunchDaemons/org.postfix.master.plist, we can’t edit it even as root, SIP only can diable on recovery OS on mojave. Eventually I think a workaround, detail as follow:

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
$ sudo launchctl unload -w /System/Library/LaunchDaemons/org.postfix.master.plist
$ sudo mv /System/Library/LaunchDaemons/org.postfix.master.plist /Library/LaunchDaemons/org.postfix.master.plist  

// Edit plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>org.postfix.master</string>
  <key>Program</key>
  <string>/usr/libexec/postfix/master</string>
  <key>ProgramArguments</key>
  <array>
      <string>master</string>
      <string>-e</string>
      <string>60</string>
  </array>
  <key>QueueDirectories</key>
  <array>
      <string>/var/spool/postfix/maildrop</string>
  </array>
  <key>AbandonProcessGroup</key>
  <true/>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
</dict>
</plist>

// Check plist syntax 
$ plutil -lint /Library/LaunchDaemons/org.postfix.master.plist

// Load service
$ sudo launchctl load -w /Library/LaunchDaemons/org.postfix.master.plist

// Check service
$ sudo lsof -i :25

// Or 
$ telnet localhost 25

9.从“时间机器”备份恢复文件

A: Apple 介绍了如何从 “时间机器”备份恢复文件,你可以用备份创建一个新的用户,它拥有备份时状态。但我的情况是备份之后还在继续使用,产生了新的文件,想恢复那些删除的文件,但不覆盖新的文件,而且由于电脑的硬盘较小,我直接用备份创建一个新的用户的恢复方法空间不够。于是我只能手动从“时间机器”的备份里恢复文件。

  1. Enable Full Disk Access for your terminal, I use iTerm;

    • System Preferences > Security & Privacy > Privacy > Full Disk Access > Add iTerm
  2. Use rsync restore;

    • eg $rsync -avuzb Documents ~/

Reference:

10.升级到 macOS Catalina 后,根目录下的非标准目录丢失了

A:

While creating the two separate volumes during the upgrade process, files and data that couldn’t be moved to their new location are placed in a Relocated Items folder. The Relocated Items folder is in the Shared folder within the User folder (/Users/Shared/Relocated Items) and available though a shortcut on the Desktop.

我之前是将私有 git 仓库目录放在了 /git, 所以现在需要使用 rsync 恢复到新的目录: rsync -avuzb /Users/Shared/Relocated\ Items/Security/git/ repo/

Reference: