PlistBuddy
PlistBuddy is a tool for reading and writing plist files on the command line in a Mac system. Can be used to read or modify the contents of a plist file.
PlistBuddy tool path
/usr/libexec/PlistBuddy
You can create a soft connection in the /usr/local/bin/ directory and you can use the PlistBuddy command directly.
# Cannot use the PlistBuddy command directly
Ln -s /usr/libexec/PlistBuddy /usr/local/bin/PlistBuddy
View help
/usr/libexec/PlistBuddy --help
Manipulating the plist file
Print the info.plist file
/usr/libexec/PlistBuddy -c "Print" info.plist
Print the corresponding value of the field
# print the field name value in info.plist
/usr/libexec/PlistBuddy -c 'Print :name' info.plist
#Script to get the field name value in the plist file and assign it to the variable
Name=$($PlistBuddy -c "print :name" info.plist)
# print array field testArr item 0
/usr/libexec/PlistBuddy -c 'Print :testArr:0' info.plist
Add field
Field Type
String
Array
Dict
Bool
Real
Int
Date
Data
# string Type: Add a field to the test.plist file Version value is 1.0.0
/usr/libexec/PlistBuddy -c 'Add :Version string 1.0.0' test.plist
# Array Type: Add an array field to the test.plist file AppArr
# 1. Add a key value
/usr/libexec/PlistBuddy -c 'Add :AppArr array' test.plist
# Note: The keys are separated by : and there can be no spaces:
# 2. Add value value app1 , app2
/usr/libexec/PlistBuddy -c 'Add :AppArr: string app1' test.plist
/usr/libexec/PlistBuddy -c 'Add :AppArr: string app2' test.plist
# Dictionary Type: Add an array field to the test.plist file AppDic
# 1. Add a key value
/usr/libexec/PlistBuddy -c 'Add :AppDic dict' test.plist
# 2. Add value value name , age
/usr/libexec/PlistBuddy -c 'Add :AppDic:name string Tom' test.plist
/usr/libexec/PlistBuddy -c 'Add :AppDic:age string 100' test.plist
Delete field
# Delete the fields in test.plist Version
/usr/libexec/PlistBuddy -c 'Delete :Version' test.plist
Modify field values
# Modify string type
/usr/libexec/PlistBuddy -c 'Set :version "1.1.1"' test.plist
# Modify array type. Modify the 0th value of the array in the AppArr field.
/usr/libexec/PlistBuddy -c 'Set :AppArr:0 "this is app1"' test.plist
# Modify the dict type. Modify the value of name in the AppDic field
/usr/libexec/PlistBuddy -c 'Set :AppDic:name "Jim"' test.plist
Merge two plists
# Merge A.plist into B.plist. With the same field, an overwrite will occur.
/usr/libexec/PlistBuddy -c 'Merge A.plist' B.plist