How to use the command 'plutil' (with examples)

How to use the command 'plutil' (with examples)

  • Osx
  • December 17, 2024

The plutil command is a powerful utility in macOS that allows users to interact with property list files, commonly referred to as plist files. These files are integral to macOS applications as they are often used for storing configuration data in a structured, hierarchical format. Plutil provides capabilities to view, convert, validate, and edit these files. It supports both XML and binary plist formats and can also handle conversions to and from JSON, Swift, and Objective-C representations.

Use case 1: Display the contents of one or more plist files in human-readable format

Code:

plutil -p file1.plist file2.plist ...

Motivation:

Viewing the contents of plist files in a human-readable format is crucial for debugging and configuration purposes. Developers and system administrators often need to verify the contents of plist files to ensure that they contain the desired settings or to troubleshoot issues with application configurations. The -p option in plutil formats the plist content into a readable structure without altering the file itself.

Explanation:

  • plutil: This is the command name, invoking the property list utility.
  • -p: This option specifies that the files should be printed in a human-readable format.
  • file1.plist file2.plist ...: These are the plist files you want to display. You can list multiple files, separated by spaces, to view them all at once.

Example output:

{
  "Version" => "1.0"
  "Name" => "SampleApp"
  "Settings" => {
    "Theme" => "dark"
    "Notifications" => true
  }
}

Use case 2: Convert one or more plist files to XML format, overwriting the original files in-place

Code:

plutil -convert xml1 file1.plist file2.plist ...

Motivation:

Some applications or development scenarios require plist files to be in XML format for compatibility or readability reasons. Converting binary plist files to XML format can also aid in version control systems where changes need to be tracked. The -convert xml1 option allows developers to seamlessly convert files to XML format.

Explanation:

  • plutil: The command to execute the property list utility.
  • -convert xml1: This option requests a conversion to XML format (version 1).
  • file1.plist file2.plist ...: These are the files you want to convert. The conversion is applied in-place, meaning the original files are overwritten with their XML equivalents.

Example output:

After using the command, file1.plist in an XML editor would display as:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Version</key>
    <string>1.0</string>
    <key>Name</key>
    <string>SampleApp</string>
    <key>Settings</key>
    <dict>
        <key>Theme</key>
        <string>dark</string>
        <key>Notifications</key>
        <true/>
    </dict>
</dict>
</plist>

Use case 3: Convert one or more plist files to binary format, overwriting the original files in-place

Code:

plutil -convert binary1 file1.plist file2.plist ...

Motivation:

Binary plist files are more compact and faster to parse compared to XML plist files, making them ideal for production environments where performance is critical. Developers may choose to convert XML plist files to binary format before deploying an application to reduce its footprint and improve load times.

Explanation:

  • plutil: Initiates the property list utility command.
  • -convert binary1: This option indicates the conversion to a binary plist format.
  • file1.plist file2.plist ...: The targeted plist files for conversion. The original files will be replaced by their respective binary versions.

Example output:

Binary output is not directly human-readable, but using a hex editor the file might appear as random binary data, indicating a successful conversion.

Use case 4: Convert a plist file to a different format, writing to a new file

Code:

plutil -convert xml1|binary1|json|swift|objc path/to/file.plist -o path/to/new_file.plist

Motivation:

When there is a need to preserve the original plist file while converting it to a different format, perhaps for experimentation, analysis, or submission to a different system, this command is invaluable. It allows one to safely create a copy of the file in a new format without modifying the original.

Explanation:

  • plutil: The property list utility command.
  • -convert xml1|binary1|json|swift|objc: Specifies the desired output format. You can choose between XML, binary, JSON, Swift, or Objective-C representations.
  • path/to/file.plist: The source plist file to be converted.
  • -o path/to/new_file.plist: Specifies the output file where the converted result will be stored.

Example output:

For instance, converting to JSON might yield an output file (new_file.plist) with content like:

{
  "Version": "1.0",
  "Name": "SampleApp",
  "Settings": {
    "Theme": "dark",
    "Notifications": true
  }
}

Use case 5: Convert a plist file to a different format, writing to stdout

Code:

plutil -convert xml1|binary1|json|swift|objc path/to/file.plist -o -

Motivation:

Seeing the immediate result of a conversion directly in the terminal is useful for quick checks or integration into scripts where further processing of the plist content is needed without creating intermediate files. This command allows one to pipe the output into other tools or files for continuous workflows.

Explanation:

  • plutil: The property list utility command for execution.
  • -convert xml1|binary1|json|swift|objc: Specifies the target conversion format.
  • path/to/file.plist: The original file to be converted.
  • -o -: The - option signifies that the output should be sent to stdout, making it appear directly in the terminal window.

Example output:

Executing a JSON conversion would print:

{
  "Version": "1.0",
  "Name": "SampleApp",
  "Settings": {
    "Theme": "dark",
    "Notifications": true
  }
}

Conclusion:

The plutil command is a versatile tool for handling plist files on macOS. Whether you need to read, convert, validate, or edit these files, plutil provides a comprehensive command set to meet various needs, from simple viewing tasks to more complex format conversions. Understanding these use cases can greatly enhance the way developers and administrators manage configuration data within the macOS ecosystem.

Tags :

Related Posts

How to Use the Command 'toolbox help' (with examples)

How to Use the Command 'toolbox help' (with examples)

The toolbox help command is an essential utility for users working with the Toolbox, a tool that facilitates the creation and management of containerized environments.

Read More
How to Use the Command 'radeontop' (with Examples)

How to Use the Command 'radeontop' (with Examples)

‘radeontop’ is a command-line utility designed to monitor the utilization of AMD GPUs on a system.

Read More
How to Use the Command 'mate-search-tool' (with examples)

How to Use the Command 'mate-search-tool' (with examples)

The mate-search-tool is a powerful search utility designed for users of the MATE desktop environment.

Read More