校验、查看、转换和修改 macOS 属性列表(plist)文件。
defaults,它会经过 cfprefsd 缓存。影响范围:
-convert默认直接覆盖原文件,-replace、-insert、-remove也会原地写入;改错系统或应用 plist 可能导致服务无法启动、应用崩溃或设置丢失。 备份:先复制一份带时间戳的备份,cp file.plist file.plist.bak,并在副本上验证命令;系统保护目录中的 plist 即使 sudo 也可能无法修改。 恢复:用备份覆盖原文件,重新plutil -lint,必要时重启应用或系统;偏好文件被 cfprefsd 覆盖时退出应用、注销或重启后再改,别直接强写。
# 1. 校验语法;-lint 是默认命令,可省略
% plutil -lint ~/Library/LaunchAgents/com.example.agent.plist
# 2. 以人类可读格式查看,输出格式不稳定,不适合脚本解析
% plutil -p info.plist
% plutil -p info.plist | head -30
# 3. 转换格式并输出到 stdout,不覆盖原文件
% plutil -convert xml1 -o - info.plist
% plutil -convert json -o - info.plist
% plutil -convert json -r -o /tmp/info.json info.plist
# 4. 原地转换为二进制或 XML;先备份
% cp info.plist info.plist.bak
% plutil -convert binary1 info.plist
# 5. 读取单个键和类型;raw 适合脚本,-expect 会校验类型
% plutil -extract CFBundleIdentifier raw -o - info.plist
% plutil -extract CFBundleIdentifier raw -expect string -o - info.plist
% plutil -type CFBundleIdentifier info.plist
# 6. 修改已有键、插入新键、删除键;操作前先确认键是否存在
% cp info.plist info.plist.bak
% plutil -replace CFBundleIdentifier -string com.example.app info.plist
% plutil -insert LSUIElement -bool true info.plist
% plutil -remove LSUIElement info.plist
# 7. 创建空 plist,或向数组追加元素
% plutil -create xml1 New.plist
% plutil -insert Items -array New.plist
% plutil -insert Items -string first -append New.plist
plutil -convert 不加 -o 会直接重写输入文件;只想查看或比对时用 -o - 输出到 stdout,或 -o 指定临时文件。date 和 data 类型;包含日期或二进制数据的 plist 转换 JSON 会失败。改用 XML,或只 -extract 需要的字段。-p 的输出格式明确“不稳定”,不要用 awk/grep 解析。脚本用 -extract ... raw、-convert json 或 -type。-replace 只能覆盖已存在的键;键不存在会失败,新键要用 -insert。-insert 遇到已存在键也可能失败,先用 -extract 或 -type 检查。defaults 处理。~/Library/Preferences/*.plist 时,应用或 cfprefsd 可能仍持有缓存并覆盖文件;先退出应用,优先用 defaults write,必要时注销或重启。plutil -lint 只检查语法,不检查语义;launchd 的 Label 与文件名不一致、键名拼错都能通过 lint,仍需 launchctl print 或实际加载验证。-convert objc/swift 是生成代码字面量,不是运行时配置格式;-convert objc -header 还会额外创建头文件。-extract、-type、-expect、-append 和 raw 格式从 macOS 12 才提供;更老的系统要改用 Python 或 defaults。plutil -p 确认路径,再检查应用是否从容器或 /Library/Preferences 读取。defaults — 读写偏好域,正确处理 cfprefsd 缓存。launchctl — 校验并重新加载 launchd plist。pkgutil — 查看安装包中的 plist 和收据。jq — JSON 查询和格式化对照。合上页面,先回答,再展开对照。
plutil -convert 不加 -o 会发生什么?只想查看转换结果应怎么写?defaults 而不是直接 plutil -replace?-o - 输出到 stdout,或用 -o /tmp/out 指定临时文件。defaults 通过系统接口读写,能保持缓存一致。
小程序版
微信扫码,随时查命令