系统学习 学习路径总入口:先选领域,再建立模型,最后用故障演练验证

macOS 学习路径:从 BSD 文件命令到系统、网络与开发维护

使用方式

这条路径按依赖顺序分四阶段,每阶段建议留 2–3 小时。先读、再做、后测:按阅读顺序看页面,动手任务在测试账户或一次性 macOS 虚拟机里完成,过关问题答不上来就回到对应页面。

不要从背快捷键开始。macOS 的命令行是 BSD 用户态加 launchd、SIP、TCC 和 APFS 的组合;先建立“命令 → 系统对象 → 权限边界”的对应关系,再进入开发工具链。

阶段一:BSD 文件命令

% mkdir -p ~/macos-lab; printf 'alpha\nbeta\n' > ~/macos-lab/access.log
% find ~/macos-lab -type f -print0 | xargs -0 stat -f '%N %z bytes'
% xattr -l ~/macos-lab/access.log; chmod 640 ~/macos-lab/access.log; ls -le ~/macos-lab/access.log
% mdfind -onlyin ~/macos-lab access; open -R ~/macos-lab/access.log

阶段二:系统与服务

% sw_vers; uname -m; sysctl -n hw.ncpu hw.memsize
% ps -Ao pid,ppid,%cpu,%mem,comm -r | head
% launchctl print gui/$(id -u) | head -30; launchctl list | head
% pmset -g; tmutil listlocalsnapshots /; sudo launchctl print system | head -20; csrutil status

阶段三:网络与安全

% ifconfig en0 | grep -E 'inet |status'; netstat -anv | grep LISTEN | head
% lsof -nP -iTCP -sTCP:LISTEN | head; dscacheutil -q host -a name example.com
% dig example.com +short; curl -I --max-time 5 https://example.com
% sudo tcpdump -ni en0 -c 5 'tcp port 443'; ssh -V; rsync -av --dry-run ~/macos-lab/ /tmp/macos-lab-backup/

阶段四:开发与维护

% xcode-select -p; xcrun --find clang; brew --prefix; brew doctor
% diskutil list; diskutil info / | grep -E 'File System|APFS'
% hdiutil create -size 64m -fs APFS -volname Lab /tmp/lab.dmg; hdiutil attach /tmp/lab.dmg
% hdiutil detach /Volumes/Lab; tmutil listbackups; defaults export com.apple.dock /tmp/dock-backup.plist

参考链接

读完自测

  1. BSD find 没有 -printf,怎样输出文件名和大小?
  2. 一个后台任务需要开机即运行、没有登录用户界面,应该选 LaunchAgent 还是 LaunchDaemon?
  3. Apple Silicon 上旧教程里的 /usr/local/bin/brew 找不到,如何判断并修正?
答案
  1. 用 find ... -exec stat -f '%N %z' {} +,或 -print0 | xargs -0 stat -f '%N %z';BSD stat 的格式符和 GNU 不同。
  2. 选 LaunchDaemon,放在 /Library/LaunchDaemons,由 system 域以 root 启动;它没有 GUI 会话,不能依赖用户登录或弹窗。
  3. 用 uname -m 和 brew --prefix 确认;Apple Silicon 原生 brew 在 /opt/homebrew,把 /opt/homebrew/bin 放到 PATH 前面,避免混用 x86 brew。

相关条目

微信小程序码:扫码打开命令速查小程序

小程序版

微信扫码,随时查命令